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?

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.