manuelgod Posted July 12, 2015 Share Posted July 12, 2015 Hi, I have been looking for a code that will display the number of minutes between two dates. I have a MySQLi database that has a field with as follows: `lastupdate` datetime NOT NULL, So I need to find the number of minutes only (not hours or days or seconds) and just minutes between that mysql "lastupdate" field which is a datetime. and a regular php time(). I can't seem to find a working solution. Any help will be appreciated. Manny Quote Link to comment https://forums.phpfreaks.com/topic/297264-help-with-dates/ Share on other sites More sharing options...
Ch0cu3r Posted July 12, 2015 Share Posted July 12, 2015 You can get the difference using PHP's DateTime diff function. Example code $mysqlDate = new DateTime($row['lastupdate']); // pass the variable that contains the lastupdate value from your query $todaysDate = new DateTime(); // leave blank for todays date $interval = $todaysDate->diff($mysqlDate); // calculates the difference between the two dates echo 'Difference is ' . $interval->format('%i Minutes'); // outputs the difference in minutes Quote Link to comment https://forums.phpfreaks.com/topic/297264-help-with-dates/#findComment-1516156 Share on other sites More sharing options...
Barand Posted July 12, 2015 Share Posted July 12, 2015 Or you can do it the SQL query SELECT lastupdate , timestampdiff(MINUTE, lastupdate, NOW()) as min_since FROM updates; +---------------------+-----------+ | lastupdate | min_since | +---------------------+-----------+ | 2015-07-12 13:20:10 | 14 | | 2015-03-12 13:30:10 | 175684 | | 2015-04-12 13:30:10 | 131044 | | 2015-05-12 13:30:10 | 87844 | | 2015-06-12 13:30:10 | 43204 | +---------------------+-----------+ Quote Link to comment https://forums.phpfreaks.com/topic/297264-help-with-dates/#findComment-1516159 Share on other sites More sharing options...
manuelgod Posted July 14, 2015 Author Share Posted July 14, 2015 Awesome guys!!! Thanks for all your help! Quote Link to comment https://forums.phpfreaks.com/topic/297264-help-with-dates/#findComment-1516288 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.