Jump to content

Help with dates


manuelgod

Recommended Posts

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 |
+---------------------+-----------+
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.