galvin Posted February 18, 2010 Share Posted February 18, 2010 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? Quote Link to comment Share on other sites More sharing options...
khr2003 Posted February 18, 2010 Share Posted February 18, 2010 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" Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 18, 2010 Share Posted February 18, 2010 Just use the mysql TIMESTAMPDIFF() function in your query with the unit parameter set to MINUTE - http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff Quote Link to comment Share on other sites More sharing options...
galvin Posted February 18, 2010 Author Share Posted February 18, 2010 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? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 18, 2010 Share Posted February 18, 2010 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. Quote Link to comment Share on other sites More sharing options...
galvin Posted February 18, 2010 Author Share Posted February 18, 2010 Cool thanks for clarifying. Sorry I missed "in your query" the first time Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.