|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class WPCOM_JSON_API_Date { |
|
4
|
|
|
/** |
|
5
|
|
|
* Returns ISO 8601 formatted datetime: 2011-12-08T01:15:36-08:00 |
|
6
|
|
|
* |
|
7
|
|
|
* @param $date_gmt (string) GMT datetime string. |
|
8
|
|
|
* @param $date (string) Optional. Used to calculate the offset from GMT. |
|
9
|
|
|
* |
|
10
|
|
|
* @return string |
|
11
|
|
|
*/ |
|
12
|
|
|
static function format_date( $date_gmt, $date = null ) { |
|
13
|
|
|
$timestamp_gmt = strtotime( "$date_gmt+0000" ); |
|
14
|
|
|
|
|
15
|
|
|
if ( null === $date ) { |
|
16
|
|
|
$timestamp = $timestamp_gmt; |
|
17
|
|
|
$hours = $minutes = $west = 0; |
|
18
|
|
|
} else { |
|
19
|
|
|
$date_time = date_create( "$date+0000" ); |
|
20
|
|
|
if ( $date_time ) { |
|
21
|
|
|
$timestamp = date_format( $date_time, 'U' ); |
|
22
|
|
|
} else { |
|
23
|
|
|
$timestamp = 0; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
// "0000-00-00 00:00:00" == -62169984000 |
|
27
|
|
|
if ( - 62169984000 == $timestamp_gmt ) { |
|
28
|
|
|
// WordPress sets post_date=now, post_date_gmt="0000-00-00 00:00:00" for all drafts |
|
29
|
|
|
// WordPress sets post_modified=now, post_modified_gmt="0000-00-00 00:00:00" for new drafts |
|
30
|
|
|
|
|
31
|
|
|
// Try to guess the correct offset from the blog's options. |
|
32
|
|
|
$timezone_string = get_option( 'timezone_string' ); |
|
33
|
|
|
|
|
34
|
|
|
if ( $timezone_string && $date_time ) { |
|
35
|
|
|
$timezone = timezone_open( $timezone_string ); |
|
36
|
|
|
if ( $timezone ) { |
|
37
|
|
|
$offset = $timezone->getOffset( $date_time ); |
|
38
|
|
|
} |
|
39
|
|
|
} else { |
|
40
|
|
|
$offset = 3600 * get_option( 'gmt_offset' ); |
|
41
|
|
|
} |
|
42
|
|
|
} else { |
|
43
|
|
|
$offset = $timestamp - $timestamp_gmt; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$west = $offset < 0; |
|
|
|
|
|
|
47
|
|
|
$offset = abs( $offset ); |
|
48
|
|
|
$hours = (int) floor( $offset / 3600 ); |
|
49
|
|
|
$offset -= $hours * 3600; |
|
50
|
|
|
$minutes = (int) floor( $offset / 60 ); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return (string) gmdate( 'Y-m-d\\TH:i:s', $timestamp ) . sprintf( '%s%02d:%02d', $west ? '-' : '+', $hours, $minutes ); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Returns ISO 8601 formatted duration interval: P0DT1H10M0S |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $time Duration in minutes or hours. |
|
60
|
|
|
* |
|
61
|
|
|
* @return null|string |
|
62
|
|
|
*/ |
|
63
|
|
|
static function format_duration( $time ) { |
|
64
|
|
|
$timestamp = strtotime( $time, 0 ); |
|
65
|
|
|
|
|
66
|
|
|
// Bail early if we don't recognize a date. |
|
67
|
|
|
if ( empty( $timestamp ) ) { |
|
68
|
|
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$days = floor( $timestamp / 86400 ); |
|
72
|
|
|
$timestamp = $timestamp % 86400; |
|
73
|
|
|
|
|
74
|
|
|
$hours = floor( $timestamp / 3600 ); |
|
75
|
|
|
$timestamp = $timestamp % 3600; |
|
76
|
|
|
|
|
77
|
|
|
$minutes = floor( $timestamp / 60 ); |
|
78
|
|
|
$timestamp = $timestamp % 60; |
|
79
|
|
|
|
|
80
|
|
|
return (string) sprintf( |
|
81
|
|
|
'P%dDT%dH%dM%dS', |
|
82
|
|
|
$days, |
|
83
|
|
|
$hours, |
|
84
|
|
|
$minutes, |
|
85
|
|
|
$timestamp |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: