Reputation: 371231
I'm setting a daily countdown to a deadline date. The code works fine.
The only issue is that, according to the PHP Manual, the function will use the default time zone:
Each parameter of this function uses the default time zone unless a time zone is specified in that parameter.
I need to be able to specify the time zone.
The sections I've read in the manual are not entirely clear how to do this.
Here's my code:
<?php
$date = strtotime("September 30, 2016 11:59 PM");
$remaining = $date - time(); // number of seconds remaining
$days_remaining = floor($remaining / 86400);
$hours_remaining = floor(($remaining % 86400) / 3600); // display final 5 days only
echo "$days_remaining Days Remaining";
?>
Is there a simple way to integrate the US Pacific Time Zone to the code above?
My guess is to make this the first line of code:
date_default_timezone_set('America/Los_Angeles');
But I'm not sure this will work and I don't want to change the time zone for other scripts.
I'm open to alternative methods for achieving this goal.
Upvotes: 1
Views: 3344
Reputation: 94662
You can capture the existing default timezone using date_default_timezone_get()
So save the current timezone, set the new one, and then do you calc's and then replace the original timezone like this
$save_zone = date_default_timezone_get();
date_default_timezone_set('America/Los_Angeles');
$date = strtotime("September 30, 2016 11:59 PM");
$remaining = $date - time(); // number of seconds remaining
$days_remaining = floor($remaining / 86400);
$hours_remaining = floor(($remaining % 86400) / 3600); // display final 5 days only
echo "$days_remaining Days Remaining";
date_default_timezone_set($save_zone);
list of valid timezone settings
Upvotes: 2
Reputation: 34978
Why not appending the time zone to the string?
$inputDate = "September 30, 2016 11:59 PM";
$date = strtotime($inputDate . ' +0100');
Upvotes: 4