Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

timezone with DST handling by PHP

Tags:

I am working on a calendar application. In this users from different time-zones create/view events. I am thinking to follow below method to save event time in UTC and display them in user's local time. Note: user has their preferred timezone setting.

To save event start time as UTC timestamp:

$timezone = new DateTimeZone( $user_timezone_name ); $utcOffset = $timezone->getOffset( new DateTime( $event_start_time_string_local ) ); $event_start_timestamp_local = maketime( $event_start_time_string_local ); //Now add/subtract $utcOffset to/from $event_start_timestamp_local to get event's start  //time  timestamp in UTC and save this result in DB. 

To get event start time in user's timezone:

date_default_timezone_set( $user_timezone_name ); $eventTimeLocalTime = date("Y-m-d H:i:s", $event_start_timestamp_in_UTC ); 

Where:

user_timezone_name is user's timezone setting. event_start_time_string_local is event's start time string in local/civil time. event_start_timestamp_in_UTC is event's start time timestamp in UTC. 

My questions:

  • Whether the PHP APIs used above take care of DST for all regions?
  • The DST time itself changes in different years, how does PHP gets information about this? do we need to upgrade PHP? If so do we need to upgrade all or particular PHP library?

References: - does-phps-date-default-timezone-set-adjust-to-daylight-saving - get-timezone-offset-for-a-given-location

like image 515
Krishna Shetty Avatar asked Dec 04 '12 08:12

Krishna Shetty


People also ask

How does PHP handle daylight time?

One method would be to set the timezone in PHP to either GMT or UTC in php. ini or by using date_default_timezone_set() . The other approach would be to using gmdate() in place of date() . Same arguments, a few hours difference in the results.

What timezone does PHP use?

The default timezone for PHP is UTC regardless of your server's timezone. This is the timezone used by all PHP date/time functions in your scripts.

Does PYTZ handle DST?

pytz brings the Olson tz database into Python. This library allows accurate and cross platform timezone calculations using Python 2.4 or higher. It also solves the issue of ambiguous times at the end of daylight saving time, which you can read more about in the Python Library Reference ( datetime. tzinfo ).

How can I get timezone in PHP?

The date_default_timezone_get() function returns the default timezone used by all date/time functions in the script.


Video Answer


1 Answers

You're way too overcomplicating this. To convert between two timezones using DateTime, do this:

date_default_timezone_set('Asia/Kolkata'); // YOUR timezone, of the server  $date = new DateTime($input, new DateTimeZone('Asia/Tokyo')); // USER's timezone $date->setTimezone(new DateTimeZone('UTC')); echo $date->format('Y-m-d H:i:s'); 

This converts from a user's local timezone to UTC. To go the other way around, to display the time in the user's local time, swap the two timezones.

Yes, PHP takes care of DST. The necessary conversion rules are part of the PHP installation. You can keep them up to date by updating PHP, or by updating the timezonedb.

like image 52
deceze Avatar answered Oct 12 '22 14:10

deceze