| @@ 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 |
|
| @@ 145-187 (lines=43) @@ | ||
| 142 | * |
|
| 143 | * @return string |
|
| 144 | */ |
|
| 145 | function format_date( $date_gmt, $date = null ) { |
|
| 146 | $timestamp_gmt = strtotime( "$date_gmt+0000" ); |
|
| 147 | ||
| 148 | if ( null === $date ) { |
|
| 149 | $timestamp = $timestamp_gmt; |
|
| 150 | $hours = $minutes = $west = 0; |
|
| 151 | } else { |
|
| 152 | $date_time = date_create( "$date+0000" ); |
|
| 153 | if ( $date_time ) { |
|
| 154 | $timestamp = date_format( $date_time, 'U' ); |
|
| 155 | } else { |
|
| 156 | $timestamp = 0; |
|
| 157 | } |
|
| 158 | ||
| 159 | // "0000-00-00 00:00:00" == -62169984000 |
|
| 160 | if ( - 62169984000 == $timestamp_gmt ) { |
|
| 161 | // WordPress sets post_date=now, post_date_gmt="0000-00-00 00:00:00" for all drafts |
|
| 162 | // WordPress sets post_modified=now, post_modified_gmt="0000-00-00 00:00:00" for new drafts |
|
| 163 | ||
| 164 | // Try to guess the correct offset from the blog's options. |
|
| 165 | $timezone_string = get_option( 'timezone_string' ); |
|
| 166 | ||
| 167 | if ( $timezone_string && $date_time ) { |
|
| 168 | $timezone = timezone_open( $timezone_string ); |
|
| 169 | if ( $timezone ) { |
|
| 170 | $offset = $timezone->getOffset( $date_time ); |
|
| 171 | } |
|
| 172 | } else { |
|
| 173 | $offset = 3600 * get_option( 'gmt_offset' ); |
|
| 174 | } |
|
| 175 | } else { |
|
| 176 | $offset = $timestamp - $timestamp_gmt; |
|
| 177 | } |
|
| 178 | ||
| 179 | $west = $offset < 0; |
|
| 180 | $offset = abs( $offset ); |
|
| 181 | $hours = (int) floor( $offset / 3600 ); |
|
| 182 | $offset -= $hours * 3600; |
|
| 183 | $minutes = (int) floor( $offset / 60 ); |
|
| 184 | } |
|
| 185 | ||
| 186 | return (string) gmdate( 'Y-m-d\\TH:i:s', $timestamp ) . sprintf( '%s%02d:%02d', $west ? '-' : '+', $hours, $minutes ); |
|
| 187 | } |
|
| 188 | } |
|
| 189 | ||