rugzo Posted June 13, 2009 Share Posted June 13, 2009 Hi All, i am using a time format '2009-05-14 22:34' $now= date ('Y m d H:i') ; $Date = '2009-05-06 10:34' ; How can i get the time difference between $DAte and $now in recalculated in minutes like 3453min. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/162032-solved-time-diff/ Share on other sites More sharing options...
.josh Posted June 13, 2009 Share Posted June 13, 2009 strtotime the two strings, subtract one from the other, divide by 60. Quote Link to comment https://forums.phpfreaks.com/topic/162032-solved-time-diff/#findComment-854979 Share on other sites More sharing options...
Maq Posted June 13, 2009 Share Posted June 13, 2009 strtotime isn't recognizing the format, 'Y m d H:i', do you have to use that for '$now'? Quote Link to comment https://forums.phpfreaks.com/topic/162032-solved-time-diff/#findComment-854981 Share on other sites More sharing options...
.josh Posted June 13, 2009 Share Posted June 13, 2009 well don't format $now in the first place, seeing as how you're wanting to turn around and strtotime it. Just use time() instead of date() for $now. Quote Link to comment https://forums.phpfreaks.com/topic/162032-solved-time-diff/#findComment-854983 Share on other sites More sharing options...
rugzo Posted June 14, 2009 Author Share Posted June 14, 2009 Thanks for the answers. Well i have a table like below --> ticket id receive time submit time sla (service level agreement) 112121 2009-06-14 13:29:55 2009-06-14 13:29:55 a request comes in and a ticket is being created. The time of the request is the receive time and the ticket creation time is the submit time. The diffrence between them can be max. 120min. So for each request you have to create a ticket within 120min. The receive time is being entered manually(copy paste). The submit time is the time where the employee submits the ticket in his tool. The submit time is automatically generated when he clicks submit which takes the current timestamp like the format above. I just have to calculate the sla for each entry which doesnt work now. I tried your suggestion with now() but it gives me a strange time when i type echo now() ; ---> 1244980096 Quote Link to comment https://forums.phpfreaks.com/topic/162032-solved-time-diff/#findComment-855552 Share on other sites More sharing options...
.josh Posted June 14, 2009 Share Posted June 14, 2009 if by now() you mean time(), that's called a unix timestamp. It's a numeric representation of time, in seconds, since the unix epoch. You would convert your time strings into their timestamps and subtract. The result would be in seconds, so you would want to divide by 60 to get minutes. Quote Link to comment https://forums.phpfreaks.com/topic/162032-solved-time-diff/#findComment-855575 Share on other sites More sharing options...
rugzo Posted June 14, 2009 Author Share Posted June 14, 2009 dateA 2009-06-14 17:00 dateB 2009-06-14 17:50 So if i understand you right, to find the time diffrence in minutes between dateB and dateB, i have to convert both timestamps into unix timestamp and divide through 60. I am a little bit confused can you please explain me in details with an example? Thanks... Quote Link to comment https://forums.phpfreaks.com/topic/162032-solved-time-diff/#findComment-855606 Share on other sites More sharing options...
.josh Posted June 14, 2009 Share Posted June 14, 2009 You mostly understood right. okay to be clear: "2009-06-14 17:50" is not a timestamp. It is a string that represents the date, that is readable by humans. As far as php (or computers in general) is concerned, it's just some arbitrary string. Computers keep track of time via timestamps. And a timestamp is that long number, which is the number of seconds since the unix epoch. So if you want to find out the number of minutes between two strings that you interpret as a date/time, you would first convert them into their timestamp equivalent so php can understand it. Then subtract the two. The result would be the difference in seconds. Since you want it to be in minutes, you would then divide by 60, since there are 60 seconds in a minute. So for your example times, you would do this: edit: woops, accidentally hit post instead of preview... example: $dateA = strtotime('2009-06-14 17:00'); $dateB = strtotime('2009-06-14 17:50'); $diff = ($dateB - $dateA) / 60; echo $diff . " minutes difference"; I'd also like to just throw out there that if both of these date/times are in sql, you can have sql perform these operations and just return you the difference. Not really sure about your setup though, so that may not be applicable. Quote Link to comment https://forums.phpfreaks.com/topic/162032-solved-time-diff/#findComment-855611 Share on other sites More sharing options...
PFMaBiSmAd Posted June 14, 2009 Share Posted June 14, 2009 Edit: Or see the last paragraph in the edit above ^^^ Or since those are already in a database, just use the mysql TIMEDIFF() function, followed by the mysql TIME_TO_SEC() function, and then divide by 60. Quote Link to comment https://forums.phpfreaks.com/topic/162032-solved-time-diff/#findComment-855613 Share on other sites More sharing options...
rugzo Posted June 14, 2009 Author Share Posted June 14, 2009 Thanks so many much, now i got it and it worked perfectly. Quote Link to comment https://forums.phpfreaks.com/topic/162032-solved-time-diff/#findComment-855701 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.