Jump to content

PHP - Need to convert Time and Date to Total Hours and Minutes Difference


dumbnoob

Recommended Posts

A quick summary.

 

I use JS to pull a time and date off of a page on an external site, and dump it into a mysql database using PHP.

 

The date is in the format  DD/MM/YYYY with the '/'s as it's dividers, and the time HH:MM:SS

 

Now, I need to be able to use the dates and times pulled from the database, to compare against another date and time set, pulled from the same page at a different time.

 

At present, what I have, is a javascript alert, that outputs something like...

 

----------------------------------------------------------------------------------------------

Your EVENT {17605244} Occurred sometime between

 

EVENT ID:17603454  -  Occured at  -  20:25:49  23/01/2010

 

EVENT ID:17608883  -  Occured at  -  0:08:55  24/01/2010

----------------------------------------------------------------------------------------------

 

I need to be able to add a line to that, that uses yet another time date (Present time date from another server (I already am able to get that)) to give an output of something like...

 

----------------------------------------------------------------------------------------------

Your EVENT {17605244} Occurred sometime between

 

EVENT ID:17603454  -  Occured at  -  20:25:49  23/01/2010

Approximately 32 Hours and 58 Minutes ago

 

EVENT ID:17608883  -  Occured at  -  0:08:55  24/01/2010

Approximately 27 Hours and 43 Minutes ago

----------------------------------------------------------------------------------------------

 

 

How would I go about using the values I have, to assign the values I need.

 

It's occurred to me that I maybe able to do it with unix timestamps, as I am tracking them also, though I would prefer to do it using the date and time variables I pull from the other server.

Convert both times to a unix time stamp.  A unix time stamp is how many seconds have passed since 00:00:00 UTC on 1 January 1970.  then you can easily do a math problem of Time1 - time 2.  after you have done this convert it back to a readable format.

 

<?php
//lets call the difference between the two unix time stamps $time_between for this example.
//There are 60 seconds in every minute.
//and 60 minutes in every hour.
$total_minutes = $time_between / '60';
$hours = $total_minutes / '60';
$minutes = $hours % '60';  // % is the modulus function to get the remainder of minutes
echo $hours . " " . $minutes;
?>

 

I hope this helps

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.