| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 |
/** |
| 3 |
* WPCOM_JSON_API_Date class. |
| 4 |
* |
| 5 |
* @package automattic/jetpack |
| 6 |
*/ |
| 7 |
/** |
| 8 |
* Base class for WPCOM_JSON_API_Date. |
| 9 |
*/ |
| 10 |
class WPCOM_JSON_API_Date { |
| 11 |
/** |
| 12 |
* Returns ISO 8601 formatted datetime: 2011-12-08T01:15:36-08:00 |
| 13 |
* |
| 14 |
* @param string $date_gmt GMT datetime string. |
| 15 |
* @param string $date Optional. Used to calculate the offset from GMT. |
| 16 |
* |
| 17 |
* @return string |
| 18 |
*/ |
| 19 |
public static function format_date( $date_gmt, $date = null ) { |
| 20 |
$offset = null; |
| 21 |
$timestamp_gmt = strtotime( "$date_gmt+0000" ); |
| 22 |
|
| 23 |
if ( null === $date ) { |
| 24 |
$timestamp = $timestamp_gmt; |
| 25 |
$west = 0; |
| 26 |
$minutes = 0; |
| 27 |
$hours = 0; |
| 28 |
} else { |
| 29 |
$date_time = date_create( "$date+0000" ); |
| 30 |
if ( $date_time ) { |
| 31 |
$timestamp = date_format( $date_time, 'U' ); |
| 32 |
} else { |
| 33 |
$timestamp = 0; |
| 34 |
} |
| 35 |
|
| 36 |
// "0000-00-00 00:00:00" == -62169984000 |
| 37 |
if ( -62169984000 === $timestamp_gmt ) { |
| 38 |
// WordPress sets post_date=now, post_date_gmt="0000-00-00 00:00:00" for all drafts |
| 39 |
// WordPress sets post_modified=now, post_modified_gmt="0000-00-00 00:00:00" for new drafts. |
| 40 |
|
| 41 |
// Try to guess the correct offset from the blog's options. |
| 42 |
$timezone_string = get_option( 'timezone_string' ); |
| 43 |
|
| 44 |
if ( $timezone_string && $date_time ) { |
| 45 |
$timezone = timezone_open( $timezone_string ); |
| 46 |
if ( $timezone ) { |
| 47 |
$offset = $timezone->getOffset( $date_time ); |
| 48 |
} |
| 49 |
} else { |
| 50 |
$offset = 3600 * get_option( 'gmt_offset' ); |
| 51 |
} |
| 52 |
} else { |
| 53 |
$offset = $timestamp - $timestamp_gmt; |
| 54 |
} |
| 55 |
|
| 56 |
$west = $offset < 0; |
| 57 |
$offset = abs( $offset ); |
| 58 |
$hours = (int) floor( $offset / 3600 ); |
| 59 |
$offset -= $hours * 3600; |
| 60 |
$minutes = (int) floor( $offset / 60 ); |
| 61 |
} |
| 62 |
|
| 63 |
return (string) gmdate( 'Y-m-d\\TH:i:s', $timestamp ) . sprintf( '%s%02d:%02d', $west ? '-' : '+', $hours, $minutes ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Returns ISO 8601 formatted duration interval: P0DT1H10M0S |
| 68 |
* |
| 69 |
* @param string $time Duration in minutes or hours. |
| 70 |
* |
| 71 |
* @return null|string |
| 72 |
*/ |
| 73 |
public static function format_duration( $time ) { |
| 74 |
$timestamp = strtotime( $time, 0 ); |
| 75 |
|
| 76 |
// Bail early if we don't recognize a date. |
| 77 |
if ( empty( $timestamp ) ) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
$days = floor( $timestamp / 86400 ); |
| 82 |
$timestamp = $timestamp % 86400; |
| 83 |
|
| 84 |
$hours = floor( $timestamp / 3600 ); |
| 85 |
$timestamp = $timestamp % 3600; |
| 86 |
|
| 87 |
$minutes = floor( $timestamp / 60 ); |
| 88 |
$timestamp = $timestamp % 60; |
| 89 |
|
| 90 |
return (string) sprintf( |
| 91 |
'P%dDT%dH%dM%dS', |
| 92 |
$days, |
| 93 |
$hours, |
| 94 |
$minutes, |
| 95 |
$timestamp |
| 96 |
); |
| 97 |
} |
| 98 |
} |
| 99 |
|