InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Calculating Time with Time Zones and Daylight Saving Time in PHP
PHP

Calculating Time with Time Zones and Daylight Saving Time in PHP

InfinityCoder November 18, 2016

You need to calculate times in different time zones. For example, you want to give users information adjusted to their local time, not the local time of your server.

Use appropriate DateTimeZone objects when you build DateTime objects and PHP will do all the work for you, as in Example 3-22.

Example 3-22. Simple time zone usage

1
2
3
4
5
6
$nowInNewYork = new DateTime('now', new DateTimeZone('America/New_York'));
$nowInCalifornia = new DateTime('now', new DateTimeZone('America/Los_Angeles'));
 
printf("It's %s in New York but %s in California.",
       $nowInNewYork->format(DateTime::RFC850),
       $nowInCalifornia->format(DateTime::RFC850));

This prints:

1
2
It's Friday, 15-Feb-13 14:50:25 EST in New York but
Friday, 15-Feb-13 11:50:25 PST in California.

Note how not only is the time localized (the hours shown differ by three) but the time zone displayed is the locally appropriate one as well.

If a time zone you’re using observes daylight saving time, this is accounted for automatically.

PHP’s default time zone is set at request startup by the date.timezone configuration
parameter.

Change this by calling date_default_time_zone_set(); that time zone becomes
the new default until changed again or the end of the request.

Example 3-23 prints
the current time twice—once as appropriate for New York and once for Paris.

Example 3-23. Changing time zone with date_default_timezone_set()

1
2
3
4
5
6
7
$now = time();
date_default_timezone_set('America/New_York');
print date(DATE_RFC850, $now);
print "\n";
 
date_default_timezone_set('Europe/Paris');
print date(DATE_RFC850, $now);

Example 3-23 displays appropriately localized time values as well as time zones, just like Example 3-22.

Because DateTime objects cooperate with DateTimeZone objects (and other functions, such as date(), respect the system-set time zone) it is very easy to twiddle time zones and get appropriately formatted output.

The time zone information that PHP relies on incorporates daylight saving time transitions as well.

The time zones that PHP understands are listed in the PHP Manual.

The names of these time zones—such as America/New_York, Europe/Paris, and Africa/Dar_es_Salaam—mirror the structure of the popular zoneinfo database.

If you want to update your time zone database without updating your entire PHP installation, install (or update) the timezonedb extension from PECL. This packages the IANA-managed Time Zone Database for PHP.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Using an Accelerator in PHP
You want to increase performance of your PHP applications. Use …

Using an Accelerator in PHP

Establishing a Default Value in PHP
You want to assign a default value to a variable …

Establishing a Default Value in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

    Categories

    • Bash
    • PHP
    • Python
    • Uncategorized

    InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

    About Us

    Start learning your desired programming language with InfinityQuest.com.

    On our website you can access any tutorial that you want with video and code examples.

    We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

    Popular Tags

    binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

    Archives

    • June 2017
    • April 2017
    • February 2017
    • January 2017
    • December 2016
    • November 2016
    Copyright © 2019 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
    Programming Tutorials | Sitemap