Skip to content

Instantly share code, notes, and snippets.

@thenbrent
Created December 7, 2016 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thenbrent/2c827cbe86a6673221a8ddd82b7e497f to your computer and use it in GitHub Desktop.
Save thenbrent/2c827cbe86a6673221a8ddd82b7e497f to your computer and use it in GitHub Desktop.
Demonstrate impact of date_default_timezone_set()
<?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