| @@ 1324-1366 (lines=43) @@ | ||
| 1321 | * |
|
| 1322 | * @return string |
|
| 1323 | */ |
|
| 1324 | function format_date( $date_gmt, $date = null ) { |
|
| 1325 | $timestamp_gmt = strtotime( "$date_gmt+0000" ); |
|
| 1326 | ||
| 1327 | if ( null === $date ) { |
|
| 1328 | $timestamp = $timestamp_gmt; |
|
| 1329 | $hours = $minutes = $west = 0; |
|
| 1330 | } else { |
|
| 1331 | $date_time = date_create( "$date+0000" ); |
|
| 1332 | if ( $date_time ) { |
|
| 1333 | $timestamp = date_format( $date_time, 'U' ); |
|
| 1334 | } else { |
|
| 1335 | $timestamp = 0; |
|
| 1336 | } |
|
| 1337 | ||
| 1338 | // "0000-00-00 00:00:00" == -62169984000 |
|
| 1339 | if ( -62169984000 == $timestamp_gmt ) { |
|
| 1340 | // WordPress sets post_date=now, post_date_gmt="0000-00-00 00:00:00" for all drafts |
|
| 1341 | // WordPress sets post_modified=now, post_modified_gmt="0000-00-00 00:00:00" for new drafts |
|
| 1342 | ||
| 1343 | // Try to guess the correct offset from the blog's options. |
|
| 1344 | $timezone_string = get_option( 'timezone_string' ); |
|
| 1345 | ||
| 1346 | if ( $timezone_string && $date_time ) { |
|
| 1347 | $timezone = timezone_open( $timezone_string ); |
|
| 1348 | if ( $timezone ) { |
|
| 1349 | $offset = $timezone->getOffset( $date_time ); |
|
| 1350 | } |
|
| 1351 | } else { |
|
| 1352 | $offset = 3600 * get_option( 'gmt_offset' ); |
|
| 1353 | } |
|
| 1354 | } else { |
|
| 1355 | $offset = $timestamp - $timestamp_gmt; |
|
| 1356 | } |
|
| 1357 | ||
| 1358 | $west = $offset < 0; |
|
| 1359 | $offset = abs( $offset ); |
|
| 1360 | $hours = (int) floor( $offset / 3600 ); |
|
| 1361 | $offset -= $hours * 3600; |
|
| 1362 | $minutes = (int) floor( $offset / 60 ); |
|
| 1363 | } |
|
| 1364 | ||
| 1365 | return (string) gmdate( 'Y-m-d\\TH:i:s', $timestamp ) . sprintf( '%s%02d:%02d', $west ? '-' : '+', $hours, $minutes ); |
|
| 1366 | } |
|
| 1367 | ||
| 1368 | /** |
|
| 1369 | * Parses a date string and returns the local and GMT representations |
|
| @@ 94-136 (lines=43) @@ | ||
| 91 | * |
|
| 92 | * @return string |
|
| 93 | */ |
|
| 94 | function format_date( $date_gmt, $date = null ) { |
|
| 95 | $timestamp_gmt = strtotime( "$date_gmt+0000" ); |
|
| 96 | ||
| 97 | if ( null === $date ) { |
|
| 98 | $timestamp = $timestamp_gmt; |
|
| 99 | $hours = $minutes = $west = 0; |
|
| 100 | } else { |
|
| 101 | $date_time = date_create( "$date+0000" ); |
|
| 102 | if ( $date_time ) { |
|
| 103 | $timestamp = date_format( $date_time, 'U' ); |
|
| 104 | } else { |
|
| 105 | $timestamp = 0; |
|
| 106 | } |
|
| 107 | ||
| 108 | // "0000-00-00 00:00:00" == -62169984000 |
|
| 109 | if ( -62169984000 == $timestamp_gmt ) { |
|
| 110 | // WordPress sets post_date=now, post_date_gmt="0000-00-00 00:00:00" for all drafts |
|
| 111 | // WordPress sets post_modified=now, post_modified_gmt="0000-00-00 00:00:00" for new drafts |
|
| 112 | ||
| 113 | // Try to guess the correct offset from the blog's options. |
|
| 114 | $timezone_string = get_option( 'timezone_string' ); |
|
| 115 | ||
| 116 | if ( $timezone_string && $date_time ) { |
|
| 117 | $timezone = timezone_open( $timezone_string ); |
|
| 118 | if ( $timezone ) { |
|
| 119 | $offset = $timezone->getOffset( $date_time ); |
|
| 120 | } |
|
| 121 | } else { |
|
| 122 | $offset = 3600 * get_option( 'gmt_offset' ); |
|
| 123 | } |
|
| 124 | } else { |
|
| 125 | $offset = $timestamp - $timestamp_gmt; |
|
| 126 | } |
|
| 127 | ||
| 128 | $west = $offset < 0; |
|
| 129 | $offset = abs( $offset ); |
|
| 130 | $hours = (int) floor( $offset / 3600 ); |
|
| 131 | $offset -= $hours * 3600; |
|
| 132 | $minutes = (int) floor( $offset / 60 ); |
|
| 133 | } |
|
| 134 | ||
| 135 | return (string) gmdate( 'Y-m-d\\TH:i:s', $timestamp ) . sprintf( '%s%02d:%02d', $west ? '-' : '+', $hours, $minutes ); |
|
| 136 | } |
|
| 137 | } |
|