Matic Posted September 1, 2013 Share Posted September 1, 2013 I want to subtract database timestamp from current server time. The formatted time that is returned to me from my database is "2013-09-01 22:05:39" and I use function date("Y-m-d H:i:s"); so that my server time I get in php is in the same format. How can I get a result in hours? Example: 2013-09-01 22:05:39 minus 2013-09-02 22:05:39 equals 24 hours ??? Quote Link to comment https://forums.phpfreaks.com/topic/281760-subtract-timestamp-time-from-current-time/ Share on other sites More sharing options...
jazzman1 Posted September 1, 2013 Share Posted September 1, 2013 This is not unix timestamp format, the "timestamp" is just a big integer. You can "google" how to convert this data/time format to unix timestamp and then apply this subtraction. You could also consider using Data/Time MySQL Functions Quote Link to comment https://forums.phpfreaks.com/topic/281760-subtract-timestamp-time-from-current-time/#findComment-1447696 Share on other sites More sharing options...
Matic Posted September 1, 2013 Author Share Posted September 1, 2013 Thing is when I convert both formats with strtotime(); and then substract those values the result is off Quote Link to comment https://forums.phpfreaks.com/topic/281760-subtract-timestamp-time-from-current-time/#findComment-1447699 Share on other sites More sharing options...
Matic Posted September 1, 2013 Author Share Posted September 1, 2013 OK I solved my problem like this: $current_time = date_create(date("Y-m-d H:i:s")); $last_time = date_create(date("Y-m-d H:i:s", strtotime(get_last_update_time($user_name)))); $result = date_diff($current_time, $last_time); echo $result->format('%Y-%m-%d %H:%i:%s'); but it echoes my result like so: "00-0-0 03:40:56", with all the days etc.. How do I format this so that it only show hours, lets say like so: "232156". With all the days, months years, formatted to hours? Quote Link to comment https://forums.phpfreaks.com/topic/281760-subtract-timestamp-time-from-current-time/#findComment-1447712 Share on other sites More sharing options...
Solution jazzman1 Posted September 1, 2013 Solution Share Posted September 1, 2013 (edited) Wow....this is so complex for my old head Everything you need to do is converting the data/time string to an unix timestamp integer number. I was thinking for something simple like: $cur_TimeStr="2013-09-02 22:05:39"; $last_TimeStr = "2013-09-01 22:05:39"; $TimeInt = strtotime($cur_TimeStr) - strtotime($last_TimeStr); $hrs = $TimeInt / 3600; echo $hrs; // 24 Edited September 1, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/281760-subtract-timestamp-time-from-current-time/#findComment-1447721 Share on other sites More sharing options...
Matic Posted September 2, 2013 Author Share Posted September 2, 2013 great, I get the part where you convert to strtotime format which is the number of seconds since January 1 1970 00:00:00 UTC etc... Then you divide by 3600 to get hours... Problem is I don't know hot to convert this to my subtracted result back... As it is now, it only shows the hours since January 1 1970 00:00:00... Quote Link to comment https://forums.phpfreaks.com/topic/281760-subtract-timestamp-time-from-current-time/#findComment-1447743 Share on other sites More sharing options...
Matic Posted September 2, 2013 Author Share Posted September 2, 2013 Oh sorry looks like you get the correct result! Thanks for help! Quote Link to comment https://forums.phpfreaks.com/topic/281760-subtract-timestamp-time-from-current-time/#findComment-1447745 Share on other sites More sharing options...
jcbones Posted September 2, 2013 Share Posted September 2, 2013 OR, $sql = "SELECT TIMESTAMPDIFF(HOUR,`date`,NOW()) as hours FROM `table`"; Quote Link to comment https://forums.phpfreaks.com/topic/281760-subtract-timestamp-time-from-current-time/#findComment-1447747 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.