PluginProbe
Sessions / 2.3.1
Sessions v2.3.1
2.1.0 2.10.0 2.11.0 2.12.0 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.2.0 2.3.0 2.3.1 2.4.0 2.4.1 2.5.0 2.6.0 2.6.1 2.6.2 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.1.0 3.1.1 All 39 releases
sessions / includes / system / class-date.php

class-date.php in Sessions 2.3.1, at includes/system/class-date.php

146 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 POSessions\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', 'sessions' ), human_time_diff( strtotime( $from ) ) );
71 } else {
72 return esc_html__( 'currently', 'sessions' );
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', 'sessions' ), human_time_diff( strtotime( $from ) ) );
86 } else {
87 return sprintf( esc_html__( 'in %s', 'sessions' ), 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 ( $age < 0 ) {
119 $age = 0 - $age;
120 }
121 if ( $abbrev ) {
122 $intervals = [
123 [ 60, _x( 'sec', 'Unit abbreviation - Stands for "second".', 'sessions' ), _x( 'sec', 'Unit abbreviation - Stands for "second".', 'sessions' ) ],
124 [ 60, _x( 'min', 'Unit abbreviation - Stands for "minute".', 'sessions' ), _x( 'min', 'Unit abbreviation - Stands for "minute".', 'sessions' ) ],
125 [ 100000, _x( 'hr', 'Unit abbreviation - Stands for "hour".', 'sessions' ), _x( 'hr', 'Unit abbreviation - Stands for "hour".', 'sessions' ) ],
126 ];
127 } else {
128 $intervals = [
129 [ 60, _n( 'second', 'seconds', 1, 'sessions' ), _n( 'second', 'seconds', 2, 'sessions' ) ],
130 [ 60, _n( 'minute', 'minutes', 1, 'sessions' ), _n( 'minute', 'minutes', 2, 'sessions' ) ],
131 [ 100000, _n( 'hour', 'hours', 1, 'sessions' ), _n( 'hour', 'hours', 2, 'sessions' ) ],
132 ];
133 }
134 $value = [];
135 foreach ( $intervals as $interval ) {
136 $val = $age % $interval[0];
137 $age = round( ( $age - $val ) / $interval[0], 0 );
138 if ( ( $val > 0 && $legend ) || ( $val >= 0 && ! $legend ) ) {
139 $value[] = $val . ( $legend ? ' ' . $interval[ ( 1 === $val ? 1 : 2 ) ] : '' );
140 }
141 }
142 return array_reverse( $value );
143 }
144
145 }
146