PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 4.4.1
Admin Columns v4.4.1
7.0.19 2.3.5 2.4 2.4.1 2.4.10 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.6.1 2.5.6.2 2.5.6.3 2.5.6.4 3.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.7 3.1 3.1.1 3.1.10 3.1.2 3.1.3 3.1.5 3.2.3 3.2.7 3.3.1 3.4.1 3.4.6 3.4.8 4.0.1 4.0.3 4.1.6 4.2.2 4.2.5 4.3 4.3.2 4.4.1 4.4.4 4.4.5 4.5.5 4.6.1 4.7.18 4.7.19 4.7.20 4.7.7 7.0.13 7.0.14 7.0.16 trunk 1.0 1.1 1.1.3 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.5.1 1.4.6 1.4.6.1 1.4.6.2 1.4.6.3 1.4.6.4 1.4.7 1.4.8 1.4.9 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.1.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.5.1 2.2.6 2.2.6.1 2.2.6.2 2.2.6.3 2.2.6.4 2.2.7 2.2.8 2.2.8.1 2.2.9 2.3.1 2.3.2 2.3.3
codepress-admin-columns / classes / Helper / Date.php
codepress-admin-columns / classes / Helper Last commit date
Select 4 years ago Arrays.php 4 years ago Date.php 4 years ago File.php 4 years ago Html.php 4 years ago Icon.php 4 years ago Image.php 4 years ago Media.php 4 years ago Menu.php 4 years ago Network.php 4 years ago Post.php 4 years ago Strings.php 4 years ago Taxonomy.php 4 years ago User.php 4 years ago
Date.php
200 lines
1 <?php
2
3 namespace AC\Helper;
4
5 use DateTime;
6 use DateTimeZone;
7 use Exception;
8
9 class Date {
10
11 /**
12 * @param string $date
13 *
14 * @return int|false
15 */
16 public function strtotime( $date ) {
17 if ( empty( $date ) || in_array( $date, [ '0000-00-00 00:00:00', '0000-00-00', '00:00:00' ] ) || ! is_scalar( $date ) ) {
18 return false;
19 }
20
21 // some plugins store dates in a jquery timestamp format, format is in ms since The Epoch.
22 // See http://api.jqueryui.com/datepicker/#utility-formatDate
23 if ( is_numeric( $date ) ) {
24 $length = strlen( trim( $date ) );
25
26 // Dates before / around September 8th, 2001 are saved as 9 numbers * 1000 resulting in 12 numbers to store the time.
27 // Dates after September 8th are saved as 10 numbers * 1000, resulting in 13 numbers.
28 // For example the ACF Date and Time Picker uses this format.
29 // credits: Ben C
30 if ( 12 === $length || 13 === $length ) {
31 $date = round( $date / 1000 ); // remove the ms
32 }
33
34 // Date format: yyyymmdd ( often used by ACF ) must start with 19xx or 20xx and is 8 long
35 // in theory a numeric string of 8 can also be a unix timestamp; no conversion would be needed
36 if ( 8 === $length && ( strpos( $date, '20' ) === 0 || strpos( $date, '19' ) === 0 ) ) {
37 $date = strtotime( $date );
38 }
39 } else {
40 $date = strtotime( $date );
41 }
42
43 return $date;
44 }
45
46 /**
47 * @param string $date
48 * @param string $format
49 *
50 * @return int|false
51 */
52 public function get_timestamp_from_format( $date, $format ) {
53 if ( ! $date ) {
54 return false;
55 }
56
57 // Already a timestamp
58 if ( 'U' === $format ) {
59 return $date;
60 }
61
62 $_date = DateTime::createFromFormat( $format, $date );
63
64 return $_date
65 ? $_date->format( 'U' )
66 : false;
67 }
68
69 /**
70 * @param string $date PHP Date format
71 * @param string $display_format Date display format
72 *
73 * @return string Formatted date
74 * @since 1.3.1
75 */
76 public function date( $date, $display_format = '' ) {
77 $timestamp = $this->strtotime( $date );
78
79 return $this->date_by_timestamp( $timestamp, $display_format );
80 }
81
82 /**
83 * @param $timestamp
84 * @param string $display_format Date display format
85 *
86 * @return string Formatted date
87 * @since 3.0
88 */
89 public function date_by_timestamp( $timestamp, $display_format = '' ) {
90 if ( ! $timestamp ) {
91 return false;
92 }
93
94 switch ( $display_format ) {
95
96 case 'wp_date' :
97 $display_format = get_option( 'date_format' );
98
99 break;
100 case 'wp_date_time' :
101 $display_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' );
102
103 break;
104 }
105
106 // Get date format from the General Settings
107 if ( ! $display_format ) {
108 $display_format = get_option( 'date_format' );
109 }
110
111 // Fallback in case the date format from General Settings is empty
112 if ( ! $display_format ) {
113 $display_format = 'F j, Y';
114 }
115
116 return $this->format_date( $display_format, $timestamp );
117 }
118
119 public function format_date( $format, $timestamp = null, DateTimeZone $timezone = null ) {
120 if ( ! function_exists( 'wp_date' ) ) {
121 return date_i18n( $format, $timestamp );
122 }
123
124 if ( null === $timezone ) {
125 $timezone = new DateTimeZone( date_default_timezone_get() );
126 }
127
128 // since WP 3.5
129 return wp_date( $format, $timestamp, $timezone );
130 }
131
132 /**
133 * @return DateTimeZone|null
134 */
135 public function timezone() {
136 if ( ! function_exists( 'wp_timezone' ) ) {
137 try {
138 return new DateTimeZone( get_option( 'timezone_string' ) );
139 } catch ( Exception $e ) {
140 return null;
141 }
142 }
143
144 // since WP 3.5
145 return wp_timezone();
146 }
147
148 /**
149 * @param string $date
150 * @param string $format
151 *
152 * @return string Formatted time
153 * @since 1.3.1
154 */
155 public function time( $date, $format = '' ) {
156 $timestamp = ac_helper()->date->strtotime( $date );
157
158 if ( ! $format ) {
159 $format = get_option( 'time_format' );
160 }
161
162 if ( ! $timestamp ) {
163 return false;
164 }
165
166 return $this->format_date( $format, $timestamp );
167 }
168
169 /**
170 * Translate a jQuery date format to the PHP date format
171 *
172 * @param string $format jQuery date format
173 *
174 * @return string PHP date format
175 * @since 1.1
176 */
177 public function parse_jquery_dateformat( $format ) {
178 $replace = [
179 '^dd^d' => 'j',
180 'dd' => 'd',
181 'DD' => 'l',
182 'o' => 'z',
183 'MM' => 'F',
184 '^mm^m' => 'n',
185 'mm' => 'm',
186 'yy' => 'Y',
187 ];
188
189 $replace_from = [];
190 $replace_to = [];
191
192 foreach ( $replace as $from => $to ) {
193 $replace_from[] = '/' . $from . '/';
194 $replace_to[] = $to;
195 }
196
197 return preg_replace( $replace_from, $replace_to, $format );
198 }
199
200 }