| 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 OPcacheManager\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 |
} else { |
| 43 |
return date_i18n( $format, strtotime( get_date_from_gmt( $ts ) ) ); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Converts a date expressed in specific TZ into an UTC MySql datetime. |
| 49 |
* |
| 50 |
* @param string $ts The date to be converted. |
| 51 |
* @param string $tz The timezone. |
| 52 |
* @return string The UTC MySql datetime. |
| 53 |
* @since 1.0.0 |
| 54 |
*/ |
| 55 |
public static function get_mysql_utc_from_date( $ts, $tz ) { |
| 56 |
$datetime = new \DateTime( $ts, new \DateTimeZone( $tz ) ); |
| 57 |
$datetime->setTimezone( new \DateTimeZone( 'UTC' ) ); |
| 58 |
return $datetime->format( 'Y-m-d H:i:s' ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Get the difference between now and a date, in human readable style (like "8 minutes ago" or "currently"). |
| 63 |
* |
| 64 |
* @param string $from The UTC MySql datetime from which the difference must be computed (as today). |
| 65 |
* @return string Human readable time difference. |
| 66 |
* @since 1.0.0 |
| 67 |
*/ |
| 68 |
public static function get_positive_time_diff_from_mysql_utc( $from ) { |
| 69 |
if ( strtotime( $from ) < time() ) { |
| 70 |
return sprintf( esc_html__( '%s ago', 'opcache-manager' ), human_time_diff( strtotime( $from ) ) ); |
| 71 |
} else { |
| 72 |
return esc_html__( 'currently', 'opcache-manager' ); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Get the difference between now and a date, in human readable style (like "8 minutes ago" or "in 19 seconds"). |
| 78 |
* |
| 79 |
* @param string $from The UTC MySql datetime from which the difference must be computed (as today). |
| 80 |
* @return string Human readable time difference. |
| 81 |
* @since 1.0.0 |
| 82 |
*/ |
| 83 |
public static function get_time_diff_from_mysql_utc( $from ) { |
| 84 |
if ( strtotime( $from ) < time() ) { |
| 85 |
return sprintf( esc_html__( '%s ago', 'opcache-manager' ), human_time_diff( strtotime( $from ) ) ); |
| 86 |
} else { |
| 87 |
return sprintf( esc_html__( 'in %s', 'opcache-manager' ), human_time_diff( strtotime( $from ) ) ); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Verify if a date exists. |
| 93 |
* |
| 94 |
* @param string $date The date to check. |
| 95 |
* @param string $format Optional. The format of the date. |
| 96 |
* @return boolean True if the date exists, false otherwise. |
| 97 |
* @since 1.0.0 |
| 98 |
*/ |
| 99 |
public static function is_date_exists( $date, $format = 'Y-m-d H:i:s' ) { |
| 100 |
try { |
| 101 |
$datetime = new \DateTime( $date ); |
| 102 |
return $date === $datetime->format( $format ); |
| 103 |
} catch ( \Throwable $e ) { |
| 104 |
return false; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Converts a integer number of seconds into an array. |
| 110 |
* |
| 111 |
* @param integer $age The age in seconds. |
| 112 |
* @param boolean $legend Optional. Add the legend. |
| 113 |
* @param boolean $abbrev Optional. Legend is abbreviated. |
| 114 |
* @return array Array of days, hours, minutes and seconds. |
| 115 |
* @since 1.0.0 |
| 116 |
*/ |
| 117 |
public static function get_age_array_from_seconds( $age, $legend = false, $abbrev = false ) { |
| 118 |
if ( $abbrev ) { |
| 119 |
$intervals = [ |
| 120 |
[ 60, _x( 'sec', 'Unit abbreviation - Stands for "second".', 'opcache-manager' ), _x( 'sec', 'Unit abbreviation - Stands for "second".', 'opcache-manager' ) ], |
| 121 |
[ 60, _x( 'min', 'Unit abbreviation - Stands for "minute".', 'opcache-manager' ), _x( 'min', 'Unit abbreviation - Stands for "minute".', 'opcache-manager' ) ], |
| 122 |
[ 100000, _x( 'hr', 'Unit abbreviation - Stands for "hour".', 'opcache-manager' ), _x( 'hr', 'Unit abbreviation - Stands for "hour".', 'opcache-manager' ) ], |
| 123 |
]; |
| 124 |
} else { |
| 125 |
$intervals = [ |
| 126 |
[ 60, _n( 'second', 'seconds', 1, 'opcache-manager' ), _n( 'second', 'seconds', 2, 'opcache-manager' ) ], |
| 127 |
[ 60, _n( 'minute', 'minutes', 1, 'opcache-manager' ), _n( 'minute', 'minutes', 2, 'opcache-manager' ) ], |
| 128 |
[ 100000, _n( 'hour', 'hours', 1, 'opcache-manager' ), _n( 'hour', 'hours', 2, 'opcache-manager' ) ], |
| 129 |
]; |
| 130 |
} |
| 131 |
$value = []; |
| 132 |
foreach ( $intervals as $interval ) { |
| 133 |
$val = $age % $interval[0]; |
| 134 |
$age = round( ( $age - $val ) / $interval[0], 0 ); |
| 135 |
if ( ( $val > 0 && $legend ) || ( $val >= 0 && ! $legend ) ) { |
| 136 |
$value[] = $val . ( $legend ? ' ' . $interval[ ( 1 === $val ? 1 : 2 ) ] : '' ); |
| 137 |
} |
| 138 |
} |
| 139 |
return array_reverse( $value ); |
| 140 |
} |
| 141 |
|
| 142 |
} |
| 143 |
|