Jump to content

difference between two datetime values from mysql


galvin

Recommended Posts

How can I calculate the EXACT difference (to the minute) from two DATETIME values stored in a MySQL database.

 

For example, the difference (in minutes) between..

 

2010-02-01 10:01:00

 

AND

 

2010-02-08 04:34:00

 

Anyone know the simplest way to do this in PHP?

if the date values are stored in timestamp format then you can just use a simple subtract operation in php to get the difference then you can convert the timestamp to a human-readable format. Otherwise, convert the date to timestamp, do the subtraction then convert back to a human-readable date.

 

To see numerous examples on what I have just explained just google "convert to time stamp php" "time difference php"

Sorry I'm such a newbie, but I see an example like...

 

 mysql> SELECT TIMESTAMPDIFF(MINUTE,'2003-02-01','2003-05-01 12:05:55');
-> 128885

 

Where do I put that in my code?  Anywhere within PHP?

 

And how can I echo that result (i.e. 128885) number somewhere else on the page?

 

 

You put mysql statements -

... in your query ...

 

You have an existing query that is SELECTing the data. You just add the TIMESTAMPDIFF() statement to the list of items being SELECTed. You would normally use an alias name for the result of that statement so that you can simply refer to the alias name as the index name when you fetch the data in your php code.

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.