PHP calculate date differences

software development

#1

I’m trying to figure out the difference in time between two timestamps using PHP in the YEAR, MONTH, DAY, HOUR, MINUTE, SECOND format.

For example (using YYYYMMDDHHMMSS format)
timestamp1 = 20020502080000
timestamp2 = 20020501080000

this would result in 0 MONTHS 1 DAY 0 HOURS 0 MINUTES 0 SECONDS

Perhaps the solution is so obvious it just escapes me. I can figure this out using Javascript but I’m not sure what the equivalent would be using PHP.


#2

It would help if we knew what the JavaScript is that you are talking about, as there are two different methods.

The first one is to convert each timestamp to a UNIX timestamp value, do a subtraction, and then convert to a formatted string. Just go to http://www.php.net/, click and
search for “date” in “list of functions” and you’ll find the appropiate functions as well as examples.

You can probably use substr() or sscanf() to get the individual components out of the string, and pass them to mktime() to get the UNIX timestamp, do the subtraction, and then to sprintf() to get a formatted string.

On the other hand, perhaps you want to calculate the difference as calendar time, where midnight February 1st through midnight March 1 is exactly 1 month, and midnight April 1 through midnight May 1 is exactly 1 month. It would take a bit more work to do that, because if you did it using the first method, you’d have to assume each month has the same number of days, and that there are no leap days and seconds.


#3

My javascript method is as follows:

[code]function timeDifference(laterdate,earlierdate) {
var difference = laterdate.getTime() - earlierdate.getTime();

var daysDifference = Math.floor(difference/1000/60/60/24);
difference -= daysDifference1000606024
var hoursDifference = Math.floor(difference/1000/60/60);
difference -= hoursDifference10006060
var minutesDifference = Math.floor(difference/1000/60);
difference -= minutesDifference
1000*60
var secondsDifference = Math.floor(difference/1000);

document.write(‘difference = ’ + daysDifference + ’ days ’ + hoursDifference +
’ hours ’ + minutesDifference + ’ minutes ’ + secondsDifference + ’ seconds ');
}

var laterdate = new Date(2003,0,1,0,0,0);
var earlierdate = new Date(2003,2,13,0,0,0);

timeDifference(laterdate,earlierdate);
[/code]The Dates are in Y, M, D, H, Min, S… It seems easy enough to duplicate this in PHP if I knew how to duplicate Javascript’s getTime().


#4

Though if you want to get months and years in the difference as well, you run into problems of varying-length months and leap years, as noted above.

– Dan


#5

I’m not necessarily concerned with differences in years and months, moreso hours, minutes and seconds since the differences in times I’m working with won’t go beyond a week or two at most.

The javascript method, I’ve learned, formats the date in seconds since the epoch. I’ve broken down the YYYYMMDDHHMMSS string in order to convert them into the Unix timestamp and formatted them with gmdate(“U”, $timestamp). A lot of substr extraction. Sometimes, though, the result varies in PHP and javascript, resulting in different epoch timestamps, even if the date strings are identical. I notice it does this when there’s a gap in months rather than just days.

Thanks for your suggestions, I believe I can settle for these results. Time for some Advil.


#6

First of all, you need to determine if your date represented by “YYYYMMDDHHMMSS” is local or GMT. For example, DreamHost’s MySQL servers are in California - and so the date will be local to the Pacific timezone.

Now second, get the parts into variables: $hour, $minutes, $seconds, $month, $date, $year.

Then if the date was GMT -
$unix_timestamp = gmmktime($hour, $minutes, $seconds, $month, $date, $year);
If the date was local -
$unix_timestamp = mktime($hour, $minutes, $seconds, $month, $date, $year);

You can skip the gmdate(“U”, $timestamp) call.

If you are getting the timestamp from a MySQL database, then there is a MySQL function to do this for you -

SELECT UNIX_TIMESTAMP(timestamp) AS `unix_timestamp` ...Also, if you insert dates into a database, make sure they are translated to the same timezone as the server. For example, if you had a UNIX timestamp, to insert it into the database:

INSERT timestamp=FROM_UNIXTIME($unix_timestamp) ...Otherwise, in PHP, you’d have to do:

Hope this helps the problem with the gaps.