PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 3.1.2
Admin Columns v3.1.2
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
Array.php 8 years ago Date.php 8 years ago File.php 8 years ago Html.php 8 years ago Icon.php 8 years ago Image.php 8 years ago Media.php 8 years ago Network.php 8 years ago Post.php 8 years ago String.php 8 years ago Taxonomy.php 8 years ago User.php 8 years ago
Date.php
182 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 class AC_Helper_Date {
8
9 /**
10 * @param string $date
11 *
12 * @return int|false
13 */
14 public function strtotime( $date ) {
15 if ( empty( $date ) || in_array( $date, array( '0000-00-00 00:00:00', '0000-00-00', '00:00:00' ) ) || ! is_scalar( $date ) ) {
16 return false;
17 }
18
19 // some plugins store dates in a jquery timestamp format, format is in ms since The Epoch.
20 // See http://api.jqueryui.com/datepicker/#utility-formatDate
21 if ( is_numeric( $date ) ) {
22 $length = strlen( trim( $date ) );
23
24 // Dates before / around September 8th, 2001 are saved as 9 numbers * 1000 resulting in 12 numbers to store the time.
25 // Dates after September 8th are saved as 10 numbers * 1000, resulting in 13 numbers.
26 // For example the ACF Date and Time Picker uses this format.
27 // credits: Ben C
28 if ( 12 === $length || 13 === $length ) {
29 $date = round( $date / 1000 ); // remove the ms
30 }
31
32 // Date format: yyyymmdd ( often used by ACF ) must start with 19xx or 20xx and is 8 long
33 // @todo: in theory a numeric string of 8 can also be a unix timestamp; no conversion would be needed
34 if ( 8 === $length && ( strpos( $date, '20' ) === 0 || strpos( $date, '19' ) === 0 ) ) {
35 $date = strtotime( $date );
36 }
37 } else {
38 $date = strtotime( $date );
39 }
40
41 return $date;
42 }
43
44 /**
45 * @param string $date
46 * @param string $format
47 *
48 * @return int|false
49 */
50 public function get_timestamp_from_format( $date, $format ) {
51 if ( ! $date ) {
52 return false;
53 }
54
55 // Already a timestamp
56 if ( 'U' === $format ) {
57 return $date;
58 }
59
60 $timestamp = false;
61
62 // since PHP 5.3.0
63 // Create timestamp from a specific date format
64 if ( function_exists( 'date_create_from_format' ) && $format ) {
65 if ( $date = date_create_from_format( $format, $date ) ) {
66 $timestamp = date_format( $date, 'U' );
67 }
68 } // before PHP 5.3.0
69 else {
70 $timestamp = $this->strtotime( $date );
71 }
72
73 return $timestamp;
74 }
75
76 /**
77 * @since 1.3.1
78 *
79 * @param string $date PHP Date format
80 * @param string $display_format Date display format
81 *
82 * @return string Formatted date
83 */
84 public function date( $date, $display_format = '' ) {
85 $timestamp = ac_helper()->date->strtotime( $date );
86
87 return $this->date_by_timestamp( $timestamp, $display_format );
88 }
89
90 /**
91 * @since 3.0
92 *
93 * @param string $date PHP Date format
94 * @param string $display_format Date display format
95 *
96 * @return string Formatted date
97 */
98 public function date_by_timestamp( $timestamp, $display_format = '' ) {
99 if ( ! $timestamp ) {
100 return false;
101 }
102
103 switch ( $display_format ) {
104
105 case 'wp_date' :
106 $display_format = get_option( 'date_format' );
107
108 break;
109 case 'wp_date_time' :
110 $display_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' );
111
112 break;
113 }
114
115 // Get date format from the General Settings
116 if ( ! $display_format ) {
117 $display_format = get_option( 'date_format' );
118 }
119
120 // Fallback in case the date format from General Settings is empty
121 if ( ! $display_format ) {
122 $display_format = 'F j, Y';
123 }
124
125 return date_i18n( $display_format, $timestamp );
126 }
127
128 /**
129 * @since 1.3.1
130 *
131 * @param string $date
132 *
133 * @return string Formatted time
134 */
135 public function time( $date, $format = '' ) {
136 $timestamp = ac_helper()->date->strtotime( $date );
137
138 if ( ! $format ) {
139 $format = get_option( 'time_format' );
140 }
141
142 if ( ! $timestamp ) {
143 return false;
144 }
145
146 return date_i18n( $format, $timestamp );
147 }
148
149 /**
150 * Translate a jQuery date format to the PHP date format
151 *
152 * @since 1.1
153 *
154 * @param string $format jQuery date format
155 *
156 * @return string PHP date format
157 */
158 public function parse_jquery_dateformat( $format ) {
159 $replace = array(
160 '^dd^d' => 'j',
161 'dd' => 'd',
162 'DD' => 'l',
163 'o' => 'z',
164 'MM' => 'F',
165 '^mm^m' => 'n',
166 'mm' => 'm',
167 'yy' => 'Y',
168 );
169
170 $replace_from = array();
171 $replace_to = array();
172
173 foreach ( $replace as $from => $to ) {
174 $replace_from[] = '/' . $from . '/';
175 $replace_to[] = $to;
176 }
177
178 return preg_replace( $replace_from, $replace_to, $format );
179 }
180
181 }
182