Find Difference between two dates in PHP
By: David Sklar in PHP Tutorials on 2008-12-01
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 onto your site.
Convert both dates to epoch timestamps and subtract one from the other. Use this code to separate the difference into weeks, days, hours, minutes, and seconds:
// 7:32:56 pm on May 10, 1965 $epoch_1 = mktime(19,32,56,5,10,1965); // 4:29:11 am on November 20, 1962 $epoch_2 = mktime(4,29,11,11,20,1962); $diff_seconds = $epoch_1 - $epoch_2; $diff_weeks = floor($diff_seconds/604800); $diff_seconds -= $diff_weeks * 604800; $diff_days = floor($diff_seconds/86400); $diff_seconds -= $diff_days * 86400; $diff_hours = floor($diff_seconds/3600); $diff_seconds -= $diff_hours * 3600; $diff_minutes = floor($diff_seconds/60); $diff_seconds -= $diff_minutes * 60; print "The two dates have $diff_weeks weeks, $diff_days days, "; print "$diff_hours hours, $diff_minutes minutes, and $diff_seconds "; print "seconds elapsed between them."; The two dates have 128 weeks, 6 days, 14 hours, 3 minutes, and 45 seconds elapsed between them.
Note that the difference isn't divided into larger chunks than weeks (i.e., months or years) because those chunks have variable length and wouldn't give an accurate count of the time difference calculated.
There are a few strange things going on here that you should be aware of. First of all, 1962 and 1965 precede the beginning of the epoch. Fortunately, mktime() fails gracefully here and produces negative epoch timestamps for each. This is okay because the absolute time value of either of these questionable timestamps isn't necessary, just the difference between the two. As long as epoch timestamps for the dates fall within the range of a signed integer, their difference is calculated correctly.
Next, a wall clock (or calendar) reflects a slightly different amount of time change between these two dates, because they are on different sides of a DST switch. The result subtracting epoch timestamps gives is the correct amount of elapsed time, but the perceived human time change is an hour off. For example, on the Sunday morning in April when DST is activated, what's the difference between 1:30 A.M. and 4:30 A.M.? It seems like three hours, but the epoch timestamps for these two times are only 7,200 seconds apart - two hours. When a local clock springs forward an hour (or falls back an hour in October), the steady march of epoch timestamps takes no notice. Truly, only two hours have passed, although our clock manipulations make it seem like three.
If you want to measure actual elapsed time (and you usually do), this method is fine. If you're more concerned with the difference in what a clock says at two points in time, use Julian days to compute the interval.
To tell a user the elapsed time since her last login, you need to find the difference between the login time and her last login time:
$epoch_1 = time(); $r = mysql_query("SELECT UNIX_TIMESTAMP(last_login) AS login FROM user WHERE id = $id") or die(); $ob = mysql_fetch_object($r); $epoch_2 = $ob->login; $diff_seconds = $epoch_1 - $epoch_2; $diff_weeks = floor($diff_seconds/604800); $diff_seconds -= $diff_weeks * 604800; $diff_days = floor($diff_seconds/86400); $diff_seconds -= $diff_days * 86400; $diff_hours = floor($diff_seconds/3600); $diff_seconds -= $diff_hours * 3600; $diff_minutes = floor($diff_seconds/60); $diff_seconds -= $diff_minutes * 60; print "You last logged in $diff_weeks weeks, $diff_days days, "; print "$diff_hours hours, $diff_minutes minutes, and $diff_seconds ago.";
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Send push notifications using Expo tokens in PHP
PHP convert string to lower case
A Basic Example using PHP in AWS (Amazon Web Services)
Different versions of PHP - History and evolution of PHP
PHP code to write to a CSV file for Microsoft Applications
PHP code to write to a CSV file from MySQL query
PHP code to import from CSV file to MySQL
Password must include both numeric and alphabetic characters - Magento
Resume or Pause File Uploads in PHP
PHP file upload prompts authentication for anonymous users
PHP file upload with IIS on windows XP/2000 etc
Comments