| 1 |
<?php |
| 2 |
/** |
| 3 |
* Date handling |
| 4 |
* |
| 5 |
* Handles all date operations. |
| 6 |
* |
| 7 |
* @package System |
| 8 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Decalog\System; |
| 13 |
|
| 14 |
/** |
| 15 |
* Define the date functionality. |
| 16 |
* |
| 17 |
* Handles all date operations. |
| 18 |
* |
| 19 |
* @package System |
| 20 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 21 |
* @since 1.0.0 |
| 22 |
*/ |
| 23 |
class Date { |
| 24 |
|
| 25 |
/** |
| 26 |
* Converts an UTC date into the correct format. |
| 27 |
* |
| 28 |
* @param string $ts The UTC MySql datetime to be converted. |
| 29 |
* @param string $tz Optional. The timezone. |
| 30 |
* @param string $format Optional. The date format. |
| 31 |
* @return string Formatted date relative to the given timezone. |
| 32 |
* @since 1.0.0 |
| 33 |
*/ |
| 34 |
public static function get_date_from_mysql_utc($ts, $tz='', $format='-') { |
| 35 |
if ($format == '-') { |
| 36 |
$format = get_option('date_format'); |
| 37 |
} |
| 38 |
if ($tz != '') { |
| 39 |
$datetime = new \DateTime($ts, new \DateTimeZone('UTC')); |
| 40 |
$datetime->setTimezone(new \DateTimeZone($tz)); |
| 41 |
return date_i18n($format, strtotime($datetime->format('Y-m-d H:i:s'))); |
| 42 |
} |
| 43 |
else { |
| 44 |
return date_i18n($format, strtotime(get_date_from_gmt($ts))); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Get the difference between now and a date, in human readable style (like "8 minutes ago" or "currently"). |
| 50 |
* |
| 51 |
* @param string $from The UTC MySql datetime from which the difference must be computed (as today). |
| 52 |
* @return string Human readable time difference. |
| 53 |
* @since 1.0.0 |
| 54 |
*/ |
| 55 |
public static function get_positive_time_diff_from_mysql_utc($from) { |
| 56 |
if (strtotime($from) < time()) { |
| 57 |
return sprintf( decalog_esc_html__('%s ago', 'decalog'), human_time_diff(strtotime($from))); |
| 58 |
} |
| 59 |
else { |
| 60 |
return decalog_esc_html__('currently', 'decalog'); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Get the difference between now and a date, in human readable style (like "8 minutes ago" or "in 19 seconds"). |
| 66 |
* |
| 67 |
* @param string $from The UTC MySql datetime from which the difference must be computed (as today). |
| 68 |
* @return string Human readable time difference. |
| 69 |
* @since 1.0.0 |
| 70 |
*/ |
| 71 |
public static function get_time_diff_from_mysql_utc($from) { |
| 72 |
if (strtotime($from) < time()) { |
| 73 |
return sprintf( decalog_esc_html__('%s ago', 'decalog'), human_time_diff(strtotime($from))); |
| 74 |
} |
| 75 |
else { |
| 76 |
return sprintf( decalog_esc_html__('in %s', 'decalog'), human_time_diff(strtotime($from))); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Converts a integer number of seconds into an array. |
| 82 |
* |
| 83 |
* @param integer $age The age in seconds. |
| 84 |
* @param boolean $legend Optional. Add the legend. |
| 85 |
* @param boolean $abbrev Optional. Legend is abbreviated. |
| 86 |
* @return array Array of days, hours, minutes and seconds. |
| 87 |
* @since 1.0.0 |
| 88 |
*/ |
| 89 |
public static function get_age_array_from_seconds( $age, $legend = false, $abbrev = false ) { |
| 90 |
if ( $abbrev ) { |
| 91 |
$intervals = [ |
| 92 |
[ 60, _x( 'sec', 'Unit abbreviation - Stands for "second".', 'decalog' ), _x( 'sec', 'Unit abbreviation - Stands for "second".', 'decalog' ) ], |
| 93 |
[ 60, _x( 'min', 'Unit abbreviation - Stands for "minute".', 'decalog' ), _x( 'min', 'Unit abbreviation - Stands for "minute".', 'decalog' ) ], |
| 94 |
[ 100000, _x( 'hr', 'Unit abbreviation - Stands for "hour".', 'decalog' ), _x( 'hr', 'Unit abbreviation - Stands for "hour".', 'decalog' ) ], |
| 95 |
]; |
| 96 |
} else { |
| 97 |
$intervals = [ |
| 98 |
[ 60, _n( 'second', 'seconds', 1, 'decalog' ), _n( 'second', 'seconds', 2, 'decalog' ) ], |
| 99 |
[ 60, _n( 'minute', 'minutes', 1, 'decalog' ), _n( 'minute', 'minutes', 2, 'decalog' ) ], |
| 100 |
[ 100000, _n( 'hour', 'hours', 1, 'decalog' ), _n( 'hour', 'hours', 2, 'decalog' ) ], |
| 101 |
]; |
| 102 |
} |
| 103 |
$value = []; |
| 104 |
foreach ( $intervals as $interval ) { |
| 105 |
$val = $age % $interval[0]; |
| 106 |
$age = round( ( $age - $val ) / $interval[0], 0 ); |
| 107 |
if ( ( $val > 0 && $legend ) || ( $val >= 0 && ! $legend ) ) { |
| 108 |
$value[] = $val . ( $legend ? ' ' . $interval[ ( 1 === $val ? 1 : 2 ) ] : '' ); |
| 109 |
} |
| 110 |
} |
| 111 |
return array_reverse( $value ); |
| 112 |
} |
| 113 |
|
| 114 |
} |
| 115 |
|