PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 11.7.2
Jetpack – WP Security, Backup, Speed, & Growth v11.7.2
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / sal / class.json-api-date.php

class.json-api-date.php in Jetpack – WP Security, Backup, Speed, & Growth 11.7.2, at sal/class.json-api-date.php

98 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $timestamp_gmt = strtotime( "$date_gmt+0000" );
21
22 if ( null === $date ) {
23 $timestamp = $timestamp_gmt;
24 $west = 0;
25 $minutes = 0;
26 $hours = 0;
27 } else {
28 $date_time = date_create( "$date+0000" );
29 if ( $date_time ) {
30 $timestamp = date_format( $date_time, 'U' );
31 } else {
32 $timestamp = 0;
33 }
34
35 // "0000-00-00 00:00:00" == -62169984000
36 if ( -62169984000 === $timestamp_gmt ) {
37 // WordPress sets post_date=now, post_date_gmt="0000-00-00 00:00:00" for all drafts
38 // WordPress sets post_modified=now, post_modified_gmt="0000-00-00 00:00:00" for new drafts.
39
40 // Try to guess the correct offset from the blog's options.
41 $timezone_string = get_option( 'timezone_string' );
42
43 if ( $timezone_string && $date_time ) {
44 $timezone = timezone_open( $timezone_string );
45 if ( $timezone ) {
46 $offset = $timezone->getOffset( $date_time );
47 }
48 } else {
49 $offset = 3600 * get_option( 'gmt_offset' );
50 }
51 } else {
52 $offset = $timestamp - $timestamp_gmt;
53 }
54
55 $west = $offset < 0;
56 $offset = abs( $offset );
57 $hours = (int) floor( $offset / 3600 );
58 $offset -= $hours * 3600;
59 $minutes = (int) floor( $offset / 60 );
60 }
61
62 return (string) gmdate( 'Y-m-d\\TH:i:s', $timestamp ) . sprintf( '%s%02d:%02d', $west ? '-' : '+', $hours, $minutes );
63 }
64
65 /**
66 * Returns ISO 8601 formatted duration interval: P0DT1H10M0S
67 *
68 * @param string $time Duration in minutes or hours.
69 *
70 * @return null|string
71 */
72 public static function format_duration( $time ) {
73 $timestamp = strtotime( $time, 0 );
74
75 // Bail early if we don't recognize a date.
76 if ( empty( $timestamp ) ) {
77 return;
78 }
79
80 $days = floor( $timestamp / 86400 );
81 $timestamp = $timestamp % 86400;
82
83 $hours = floor( $timestamp / 3600 );
84 $timestamp = $timestamp % 3600;
85
86 $minutes = floor( $timestamp / 60 );
87 $timestamp = $timestamp % 60;
88
89 return (string) sprintf(
90 'P%dDT%dH%dM%dS',
91 $days,
92 $hours,
93 $minutes,
94 $timestamp
95 );
96 }
97 }
98