Date.php
5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
namespace AC\Helper;
use DateTime;
use DateTimeZone;
use Exception;
class Date
{
/**
* @param string|int $date
*/
public function strtotime($date): ?int
{
if ( ! is_scalar($date) ||
empty($date) ||
in_array($date, ['0000-00-00 00:00:00', '0000-00-00', '00:00:00'], true)
) {
return null;
}
// some plugins store dates in a jquery timestamp format, format is in ms since The Epoch.
// See http://api.jqueryui.com/datepicker/#utility-formatDate
if (is_numeric($date)) {
$length = strlen(trim($date));
// Dates before / around September 8th, 2001 are saved as 9 numbers * 1000 resulting in 12 numbers to store the time.
// Dates after September 8th are saved as 10 numbers * 1000, resulting in 13 numbers.
// For example the ACF Date and Time Picker uses this format.
// credits: Ben C
if (12 === $length || 13 === $length) {
return (int)round($date / 1000); // remove the ms
}
// Date format: yyyymmdd ( often used by ACF ) must start with 19xx or 20xx and is 8 long
// in theory a numeric string of 8 can also be a unix timestamp; no conversion would be needed
if (8 === $length && (strpos($date, '20') === 0 || strpos($date, '19') === 0)) {
return strtotime($date) ?: null;
}
return (int)$date;
}
return strtotime($date) ?: null;
}
/**
* @param string $date
* @param string $format
*
* @return int|false
*/
public function get_timestamp_from_format($date, $format)
{
if ( ! $date) {
return false;
}
// Already a timestamp
if ('U' === $format) {
return $date;
}
$_date = DateTime::createFromFormat($format, $date);
return $_date
? $_date->format('U')
: false;
}
/**
* @param string $date PHP Date format
* @param string $display_format Date display format
*
* @return string Formatted date
* @since 1.3.1
*/
public function date($date, $display_format = '')
{
$timestamp = $this->strtotime($date);
return $this->date_by_timestamp($timestamp, $display_format);
}
/**
* @param $timestamp
* @param string $display_format Date display format
*
* @return string Formatted date
* @since 3.0
*/
public function date_by_timestamp($timestamp, $display_format = '')
{
if ( ! $timestamp) {
return false;
}
switch ($display_format) {
case 'wp_date' :
$display_format = get_option('date_format');
break;
case 'wp_date_time' :
$display_format = get_option('date_format') . ' ' . get_option('time_format');
break;
}
// Get date format from the General Settings
if ( ! $display_format) {
$display_format = get_option('date_format');
}
// Fallback in case the date format from General Settings is empty
if ( ! $display_format) {
$display_format = 'F j, Y';
}
return $this->format_date($display_format, $timestamp);
}
public function format_date($format, $timestamp = null, DateTimeZone $timezone = null)
{
if ( ! function_exists('wp_date')) {
return date_i18n($format, $timestamp);
}
if (null === $timezone) {
$timezone = new DateTimeZone(date_default_timezone_get());
}
// since WP 3.5
return wp_date($format, $timestamp, $timezone);
}
/**
* @return DateTimeZone|null
*/
public function timezone()
{
if ( ! function_exists('wp_timezone')) {
try {
return new DateTimeZone(get_option('timezone_string'));
} catch (Exception $e) {
return null;
}
}
// since WP 3.5
return wp_timezone();
}
/**
* @param string $date
* @param string $format
*
* @return string Formatted time
* @since 1.3.1
*/
public function time($date, $format = '')
{
$timestamp = ac_helper()->date->strtotime($date);
if ( ! $format) {
$format = get_option('time_format');
}
if ( ! $timestamp) {
return false;
}
return $this->format_date($format, $timestamp);
}
/**
* Translate a jQuery date format to the PHP date format
*
* @param string $format jQuery date format
*
* @return string PHP date format
* @since 1.1
*/
public function parse_jquery_dateformat($format)
{
$replace = [
'^dd^d' => 'j',
'dd' => 'd',
'DD' => 'l',
'o' => 'z',
'MM' => 'F',
'^mm^m' => 'n',
'mm' => 'm',
'yy' => 'Y',
];
$replace_from = [];
$replace_to = [];
foreach ($replace as $from => $to) {
$replace_from[] = '/' . $from . '/';
$replace_to[] = $to;
}
return preg_replace($replace_from, $replace_to, $format);
}
}