everurssantosh Posted September 20, 2008 Share Posted September 20, 2008 Hi , I am working on a project where I need to find out difference between two dates in minutes. First souce is stored in mysql timestamp field and looks like : ]"2008-09-20 06:30:46" [/b Second Souce is a time which I generate at time of executing the cod in PHP looks like : 2008-09-20 08:30:22 which I generate using the follwiing code in PHP $date_now = date("Y-m-d H:i:s ") ; I have tried lot of functions using PHP but I am not able to find the difference between two inputs in minutes. Kindly HELP Thanks Santosh Quote Link to comment https://forums.phpfreaks.com/topic/125070-solved-timestamp-difference-in-minutes/ Share on other sites More sharing options...
DamienRoche Posted September 20, 2008 Share Posted September 20, 2008 What you could do is put the minutes into a variable like so: $minutes1 = date("i"); $minutes2 = date("i"); //this would be the date now. Then compare. $difference = $minutes2 - $minutes1; It might not be the complete solution but I'm sure you could find a way to finish it off. I hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/125070-solved-timestamp-difference-in-minutes/#findComment-646357 Share on other sites More sharing options...
thebadbad Posted September 20, 2008 Share Posted September 20, 2008 Simple. Compare timestamps (seconds), knowing that 60 seconds is a minute: <?php $db_date = '2008-09-20 06:30:46'; $db_stamp = strtotime($db_date); $diff = round((time() - $db_stamp) / 60); ?> You can also do it directly in the MySQL query, I just can't remember how. Quote Link to comment https://forums.phpfreaks.com/topic/125070-solved-timestamp-difference-in-minutes/#findComment-646359 Share on other sites More sharing options...
everurssantosh Posted September 20, 2008 Author Share Posted September 20, 2008 Server time by setting timezone: 01:56:04 pm (20) I got this while accessing your page... but my query is still not solved. i need the difference in minutes.. if it is 2 days 3 hours 19 mins, then i need it in minutes... pleaes help Quote Link to comment https://forums.phpfreaks.com/topic/125070-solved-timestamp-difference-in-minutes/#findComment-646360 Share on other sites More sharing options...
waynew Posted September 20, 2008 Share Posted September 20, 2008 I have a function that you might like to use: function time_since($time){ $now = mktime(); $seconds_since = $now - $time; if($seconds_since < 60){ return $seconds_since.' seconds ago'; } else if($seconds_since > 59 && $seconds_since < 120){ return '1 minute ago'; } else if($seconds_since > 119 && $seconds_since < 3600){ $minutes = round($seconds_since / 60); return $minutes.' minutes ago'; } else if($seconds_since > 3599 && $seconds_since < 86400){ $hours = round($seconds_since / 3600); if($hours == 1){ return "1 hour ago"; } else{ return $hours.' hours ago'; } } else if($seconds_since > 86400 && $seconds_since < 604800){ $days = round($seconds_since/86400); if($days == 1){ return "1 day ago"; } else{ return $days.' days ago'; } } else if($seconds_since > 604799){ $weeks = round($seconds_since/604800); if($weeks == 1){ return "1 week ago"; } else{ return $weeks.' weeks ago'; } } } Not the best but does the job for me. Quote Link to comment https://forums.phpfreaks.com/topic/125070-solved-timestamp-difference-in-minutes/#findComment-646362 Share on other sites More sharing options...
everurssantosh Posted September 20, 2008 Author Share Posted September 20, 2008 Hi, I have seen three replies but I am not able to get the correct code yet. I have one time in database with mysql timestamp in a format :2008-09-20 07:30:46 The other comparision parameter is the current time of the server. I need the difference in minutes... the functions given doesnt take care of the date or year ... or the 24 hours date format.. . pleaes help Thanks Santosh Quote Link to comment https://forums.phpfreaks.com/topic/125070-solved-timestamp-difference-in-minutes/#findComment-646364 Share on other sites More sharing options...
Mchl Posted September 20, 2008 Share Posted September 20, 2008 SELECT TIMEDIFF(`timestampField`,NOW()) / 60 AS minutesFromTimestamp FROM table; Quote Link to comment https://forums.phpfreaks.com/topic/125070-solved-timestamp-difference-in-minutes/#findComment-646365 Share on other sites More sharing options...
thebadbad Posted September 20, 2008 Share Posted September 20, 2008 Go with Mchl's query solution, if it works. What's wrong with my code, though? <?php $db_date = '2008-09-20 15:00:00'; $db_stamp = strtotime($db_date); $diff = round((time() - $db_stamp) / 60); echo "$diff minutes"; ?> Outputs 75 minutes (It's 16:15 here). Quote Link to comment https://forums.phpfreaks.com/topic/125070-solved-timestamp-difference-in-minutes/#findComment-646367 Share on other sites More sharing options...
Mchl Posted September 20, 2008 Share Posted September 20, 2008 Should work as well Quote Link to comment https://forums.phpfreaks.com/topic/125070-solved-timestamp-difference-in-minutes/#findComment-646368 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.