RON_ron Posted September 11, 2012 Share Posted September 11, 2012 I've got the user signup time stored in a database formatted to my timezone. Can someone help me to find the difference between the current time and the stored time. This is my code - no success though. $tzone = new DateTimeZone('Asia/Manila'); $date = new DateTime('now', $tzone); $currenttime = $date->format('Y-m-d g:ia'); $userTimeFromDB = "2012-09-10 6:20pm"; $difference= ($userTimeFromDB - $currenttime); echo ($difference); Quote Link to comment https://forums.phpfreaks.com/topic/268252-tzone-difference/ Share on other sites More sharing options...
Psycho Posted September 11, 2012 Share Posted September 11, 2012 I'm not really following you. If you stored the time in your timezone and want to know the difference then just calculate the difference between the time in the DB and your current time. How did you store the time based upon your timezone to begin with? You should be able to get what you want using DATEDIFF() to get the number of days difference. Or if, you want the difference in time use TIMEDIFF() SELECT DATEDIFF(NOW(), date_field) as difference FROM table But, if you want to work with times across timezones you should be storing it based upon GMT time - not your local time. Then translate the value to the appropriate offset when displaying it. Quote Link to comment https://forums.phpfreaks.com/topic/268252-tzone-difference/#findComment-1376936 Share on other sites More sharing options...
ManiacDan Posted September 11, 2012 Share Posted September 11, 2012 You can't simply subtract two strings and hope for the best. At the very least, you have to use the dateTime::diff function to calculate the difference. You could also convert them to timestamps and subtract them, but you can't subtract strings. Quote Link to comment https://forums.phpfreaks.com/topic/268252-tzone-difference/#findComment-1377036 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.