class-date-time.php
4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
namespace um\core;
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! class_exists( 'um\core\Date_Time' ) ) {
/**
* Class Date_Time
* @package um\core
*/
class Date_Time {
/**
* Date_Time constructor.
*/
function __construct() {
}
/**
* Display time in specific format
*
* @param $format
*
* @return int|string
*/
function get_time( $format ) {
return current_time( $format );
}
/**
* Show a cool time difference between 2 timestamps
*
* @param int $from
* @param int $to
*
* @return string
*/
function time_diff( $from, $to = '' ) {
$since = '';
if ( empty( $to ) ) {
$to = time();
}
$diff = (int) abs( $to - $from );
if ( $diff < 60 ) {
$since = __( 'just now', 'ultimate-member' );
} elseif ( $diff < HOUR_IN_SECONDS ) {
$mins = round( $diff / MINUTE_IN_SECONDS );
if ( $mins <= 1 ) {
$mins = 1;
}
// translators: %s: min time.
$since = sprintf( _n( '%s min', '%s mins', $mins, 'ultimate-member' ), $mins );
} elseif ( $diff < DAY_IN_SECONDS && $diff >= HOUR_IN_SECONDS ) {
$hours = round( $diff / HOUR_IN_SECONDS );
if ( $hours <= 1 ) {
$hours = 1;
}
// translators: %s: hours.
$since = sprintf( _n( '%s hr', '%s hrs', $hours, 'ultimate-member' ), $hours );
} elseif ( $diff < WEEK_IN_SECONDS && $diff >= DAY_IN_SECONDS ) {
$days = round( $diff / DAY_IN_SECONDS );
if ( $days <= 1 ) {
$days = 1;
}
if ( $days == 1 ) {
// translators: %s: time.
$since = sprintf( __( 'Yesterday at %s', 'ultimate-member' ), date_i18n( get_option( 'time_format' ), $from ) );
} else {
// translators: %1$s is a date; %2$s is a time.
$since = sprintf( __( '%1$s at %2$s', 'ultimate-member' ), date_i18n( 'F d', $from ), date_i18n( get_option( 'time_format' ), $from ) );
}
} elseif ( $diff < 30 * DAY_IN_SECONDS && $diff >= WEEK_IN_SECONDS ) {
// translators: %1$s is a date; %2$s is a time.
$since = sprintf( __( '%1$s at %2$s', 'ultimate-member' ), date_i18n( 'F d', $from ), date_i18n( get_option( 'time_format' ), $from ) );
} elseif ( $diff < YEAR_IN_SECONDS && $diff >= 30 * DAY_IN_SECONDS ) {
// translators: %1$s is a date; %2$s is a time.
$since = sprintf( __( '%1$s at %2$s', 'ultimate-member' ), date_i18n( 'F d', $from ), date_i18n( get_option( 'time_format' ), $from ) );
} elseif ( $diff >= YEAR_IN_SECONDS ) {
// translators: %1$s is a date; %2$s is a time.
$since = sprintf( __( '%1$s at %2$s', 'ultimate-member' ), date_i18n( get_option( 'date_format' ), $from ), date_i18n( get_option( 'time_format' ), $from ) );
}
/**
* UM hook
*
* @type filter
* @title um_human_time_diff
* @description Change human time string
* @input_vars
* [{"var":"$since","type":"string","desc":"Disable UM Cron?"},
* {"var":"$diff","type":"int","desc":"Difference in seconds"},
* {"var":"$from","type":"int","desc":"From Timestamp"},
* {"var":"$to","type":"int","desc":"To Timestamp"}]
* @change_log
* ["Since: 2.0"]
* @usage add_filter( 'um_human_time_diff', 'function_name', 10, 4 );
* @example
* <?php
* add_filter( 'um_human_time_diff', 'my_human_time_diff', 10, 4 );
* function my_human_time_diff( $since, $diff, $from, $to ) {
* // your code here
* return $since;
* }
* ?>
*/
return apply_filters( 'um_human_time_diff', $since, $diff, $from, $to );
}
/**
* Get age
*
* @param $then
*
* @return string
*/
function get_age( $then ) {
if ( ! $then ) {
return '';
}
$then_ts = strtotime( $then );
$then_year = date( 'Y', $then_ts );
$age = date( 'Y' ) - $then_year;
if ( strtotime( '+' . $age . ' years', $then_ts ) > current_time( 'timestamp' ) ) {
$age--;
}
if ( $age == 1 ) {
// translators: %s: age.
return sprintf( __( '%s year old', 'ultimate-member' ), $age );
}
if ( $age > 1 ) {
// translators: %s: age.
return sprintf( __( '%s years old', 'ultimate-member' ), $age );
}
if ( $age == 0 ) {
return __( 'Less than 1 year old', 'ultimate-member' );
}
return '';
}
/**
* Reformat dates
*
* @param $old
* @param $new
*
* @return string
*/
function format( $old, $new ) {
$datetime = new \DateTime( $old );
$output = $datetime->format( $new );
return $output;
}
/**
* Get last 30 days as array
*
* @param int $num
* @param bool $reverse
*
* @return array
*/
function get_last_days( $num = 30, $reverse = true ) {
$d = array();
for ( $i = 0; $i < $num; $i++ ) {
$d[ date('Y-m-d', strtotime( '-' . $i . ' days' ) ) ] = date( 'm/d', strtotime( '-' . $i . ' days' ) );
}
return ( $reverse ) ? array_reverse( $d ) : $d;
}
}
}