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
Finding the Difference of Two Dates in PHP
PHP

Finding the Difference of Two Dates in PHP

InfinityCoder November 18, 2016

 

 

You want to find the elapsed time between two dates. For example, you want to tell a user how long it’s been since she last logged on to your site.

Create DateTime objects for each date. Then use the DateTime::diff() method to obtain a DateInterval object that describes the difference between the dates.

Example 3-14 displays the difference in weeks, days, hours, minutes, and seconds.

Example 3-14. Calculating the difference between two dates

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 7:32:56 pm on May 10, 1965
$first = new DateTime("1965-05-10 7:32:56pm",
         new DateTimeZone('America/New_York'));
// 4:29:11 am on November 20, 1962
$second = new DateTime("1962-11-20 4:29:11am",
          new DateTimeZone('America/New_York'));
$diff = $second->diff($first);
printf("The two dates have %d weeks, %s days, " .
 
       "%d hours, %d minutes, and %d seconds " .
       "elapsed between them.",
       floor($diff->format('%a') / 7),
       $diff->format('%a') % 7,
       $diff->format('%h'),
       $diff->format('%i'),
       $diff->format('%s'));

Example 3-14 prints:

1
The two dates have 128 weeks, 6 days, 15 hours, 3 minutes, and 45 seconds elapsed between them.

There are a few subtleties about computing date differences that you should be aware of. First of all, 1962 and 1965 precede the beginning of the Unix epoch.

Because of the 600-billion year range of PHP’s built-in time library, however, this isn’t a problem.’

Next, note that the results of DateTime::diff() produce what a clock would say is the time difference, not necessarily the absolute amount of elapsed time.

The two dates in Example 3-14 are on different sides of a DST switch, so the actual amount of elapsed time between them is an hour less (due to the repeating clock-hour in the fall switch to standard time) than what’s shown in the output.

To compute elapsed time difference, build DateTime objects out of the epoch timestamps from each local timestamp, then apply DateTime::diff() to those objects, as shown in Example 3-15.

Example 3-15. Calculating the elapsed-time difference between two dates

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 7:32:56 pm on May 10, 1965
$first_local = new DateTime("1965-05-10 7:32:56pm",
                            new DateTimeZone('America/New_York'));
// 4:29:11 am on November 20, 1962
$second_local = new DateTime("1962-11-20 4:29:11am",
                             new DateTimeZone('America/New_York'));
$first = new DateTime('@' . $first_local->getTimestamp());
$second = new DateTime('@' . $second_local->getTimestamp());
 
$diff = $second->diff($first);
 
printf("The two dates have %d weeks, %s days, " .
       "%d hours, %d minutes, and %d seconds " .
       "elapsed between them.",
       floor($diff->format('%a') / 7),
       $diff->format('%a') % 7,
       $diff->format('%h'),
       $diff->format('%i'),
       $diff->format('%s'));

Example 3-15 prints:

1
The two dates have 128 weeks, 6 days, 14 hours, 3 minutes, and 45 seconds elapsed between them.

This, as you can see, is an hour different from the output of Example 3-14.

The Date Time objects created with a format string of @ plus an epoch timestamp always have a time zone of UTC, so their difference is not affected by any daylight saving time or other local time adjustments.

At the time of writing, PHP Bug 52480 is outstanding, which affects some rare date interval calculations with certain hour values and time zone offsets.

You can work around this bug by using UTC as the time zone for interval calculations.

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Sorting in a Locale-Aware Order in PHP
You need to sort text in a way that respects …

Sorting in a Locale-Aware Order in PHP

Making an OAuth 1.0 Request in PHP
You want to make an OAuth 1.0 signed request. Use …

Making an OAuth 1.0 Request 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