rwahdan1978 Posted April 26 Share Posted April 26 Hi I have a feild in DB that have time() like 2025-04-26 20:30:00. I want to check and get difference between curremt time and the time in DB. example: time in DB is 2025-04-26 20:30:00 current time is 2025-04-26 20:40:00 the time allowed is 15 minutes so from the above example its only passing 10 minutes and have 5 minutes left, how to calculate that? Quote Link to comment https://forums.phpfreaks.com/topic/327533-calculate-time/ Share on other sites More sharing options...
maxxd Posted April 26 Share Posted April 26 Check out https://dev.mysql.com/doc/refman/8.4/en/date-and-time-functions.html#function_datediff Quote Link to comment https://forums.phpfreaks.com/topic/327533-calculate-time/#findComment-1653446 Share on other sites More sharing options...
Barand Posted April 26 Share Posted April 26 The DATEDIFF() function will give the difference in days. Use TIMESTAMPDIFF() which lets you the specify the units for the result. mysql> SELECT NOW(), timestampdiff(SECOND, '2025-04-26 20:30:00', NOW()) as diff; +---------------------+------+ | NOW() | diff | +---------------------+------+ | 2025-04-26 21:34:51 | 3891 | +---------------------+------+ 1 row in set (0.00 sec) mysql> SELECT NOW(), timestampdiff(MINUTE, '2025-04-26 20:30:00', NOW()) as diff; +---------------------+------+ | NOW() | diff | +---------------------+------+ | 2025-04-26 21:35:22 | 65 | +---------------------+------+ 1 row in set (0.00 sec) Quote Link to comment https://forums.phpfreaks.com/topic/327533-calculate-time/#findComment-1653454 Share on other sites More sharing options...
maxxd Posted April 26 Share Posted April 26 Well, crap - I meant to link to the page as a whole, not the anchor tag. Thanks for the correction! Quote Link to comment https://forums.phpfreaks.com/topic/327533-calculate-time/#findComment-1653456 Share on other sites More sharing options...
Suyadi Posted Thursday at 03:44 PM Share Posted Thursday at 03:44 PM $timezone = 'Asia/Jakarta'; // Change this $dbTime = '2025-04-26 20:30:00'; $dbTime = DateTime::createFromFormat('Y-m-d H:i:s', $dbTime, new DateTimeZone($timezone)); $now = new DateTime('now', new DateTimeZone($timezone)); print_r($dbTime->diff($now)); print_r($now->diff($dbTime)); Result: DateInterval Object ( [y] => 0 [m] => 0 [d] => 5 [h] => 2 [i] => 13 [s] => 10 [f] => 0.828 [invert] => 0 [days] => 5 [from_string] => ) DateInterval Object ( [y] => 0 [m] => 0 [d] => 5 [h] => 2 [i] => 13 [s] => 10 [f] => 0.828 [invert] => 1 [days] => 5 [from_string] => ) Quote Link to comment https://forums.phpfreaks.com/topic/327533-calculate-time/#findComment-1653573 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.