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.
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.
Thanks for your suggestions, I believe I can settle for these results. Time for some Advil.
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: