Created
December 7, 2016 20:23
-
-
Save thenbrent/2c827cbe86a6673221a8ddd82b7e497f to your computer and use it in GitHub Desktop.
Demonstrate impact of date_default_timezone_set()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$date_string = '2015-11-13 09:59:00'; | |
// Normal WordPress Way | |
date_default_timezone_set( 'UTC' ); | |
$date_utc = new DateTime( $date_string ); | |
// Rare Occassions | |
date_default_timezone_set( 'America/Los_Angeles' ); | |
$date_pdt = new DateTime( $date_string ); | |
if ( $date_utc->format( 'U' ) === $date_utc->format( 'U' ) ) { | |
error_log( 'Horray! The timestamp of our date is the same, because they are both UTC.' ); | |
} else { | |
error_log( 'Oh no! The timestamp of our date is different, because it was not UTC.' ); | |
} | |
// Normal WordPress Way | |
date_default_timezone_set( 'UTC' ); | |
$timestamp_utc = strtotime( $date_string ); | |
// Rare Occassions | |
date_default_timezone_set( 'America/Los_Angeles' ); | |
$timestamp_pdt = strtotime( $date_string ); | |
if ( $timestamp_utc === $timestamp_pdt ) { | |
error_log( 'Horray! The timestamp of our date is the same, because they are both UTC.' ); | |
} else { | |
error_log( 'Oh no! The timestamp of our date is different, because it was not UTC.' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment