yoursurrogategod Posted February 20, 2013 Share Posted February 20, 2013 Hi, I have a small problem. We get output from a database where the time-stamp is in Greenwich time while we're in New England USA. There's a time discrepancy of about 5 hours. What I'd like to do in PHP is subtract 5 hours (no, it can't be done in the DB, I've asked). My current approach is to take the timestamp (which is consistent) and split that up into smaller pieces and turn that into a unix timestamp, then subtract the 5 hours equivalent and turn that into a timestamp. But, is there a better way? Quote Link to comment https://forums.phpfreaks.com/topic/274735-what-is-a-good-way-to-subtract-5-hours-from-a-timestamp/ Share on other sites More sharing options...
mweldan Posted February 20, 2013 Share Posted February 20, 2013 (edited) How about strtotime() ? echo date('Y-m-d H:i:s a', strtotime("-5 hours")); Edited February 20, 2013 by mweldan Quote Link to comment https://forums.phpfreaks.com/topic/274735-what-is-a-good-way-to-subtract-5-hours-from-a-timestamp/#findComment-1413669 Share on other sites More sharing options...
Christian F. Posted February 20, 2013 Share Posted February 20, 2013 (edited) There are plenty of ways to substract 5 hours from a timestamp, but the best thing in this case is to change the timezone from GMT to your local timezone. You can do this in the database, when asking for the data, by using MySQL's CONVERT_TZ() function. In PHP I recommend using the DateTime class, combined with the DateTimeZone class. Create the DateTime object first, using GMT as the timezone, then set the TimeZone to your local, and the correct time will be automatically calculated for you. Edited February 20, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/274735-what-is-a-good-way-to-subtract-5-hours-from-a-timestamp/#findComment-1413672 Share on other sites More sharing options...
yoursurrogategod Posted February 20, 2013 Author Share Posted February 20, 2013 There are plenty of ways to substract 5 hours from a timestamp, but the best thing in this case is to change the timezone from GMT to your local timezone. That was the first thing that I wanted to do, but I can't. You can do this in the database, when asking for the data, by using MySQL's CONVERT_TZ() function.In PHP I recommend using the DateTime class, combined with the DateTimeZone class. Create the DateTime object first, using GMT as the timezone, then set the TimeZone to your local, and the correct time will be automatically calculated for you. Thanks, I'll have a look. Quote Link to comment https://forums.phpfreaks.com/topic/274735-what-is-a-good-way-to-subtract-5-hours-from-a-timestamp/#findComment-1413674 Share on other sites More sharing options...
yoursurrogategod Posted February 20, 2013 Author Share Posted February 20, 2013 How about strtotime() ? echo date('Y-m-d H:i:s a', strtotime("-5 hours")); I don't get it, how would that help me? The datetime is stored in the database and I have to convert it to 5 hours earlier, that would just get the current time. Quote Link to comment https://forums.phpfreaks.com/topic/274735-what-is-a-good-way-to-subtract-5-hours-from-a-timestamp/#findComment-1413676 Share on other sites More sharing options...
mweldan Posted February 20, 2013 Share Posted February 20, 2013 just what you want to do. 1. date time in db stored in greenwich time. 2. differences is about 5 hours $date_in_greenwich = '2013-02-21 10:10:10"; echo date('Y-m-d, strtotime($date_in_greenwich." -5 hours")); well.. There are plenty of ways to substract 5 hours from a timestamp, but the best thing in this case is to change the timezone from GMT to your local timezone. You can do this in the database, when asking for the data, by using MySQL's CONVERT_TZ() function. In PHP I recommend using the DateTime class, combined with the DateTimeZone class. Create the DateTime object first, using GMT as the timezone, then set the TimeZone to your local, and the correct time will be automatically calculated for you. just one example if you want to give a try. heheh Quote Link to comment https://forums.phpfreaks.com/topic/274735-what-is-a-good-way-to-subtract-5-hours-from-a-timestamp/#findComment-1413682 Share on other sites More sharing options...
Christian F. Posted February 20, 2013 Share Posted February 20, 2013 Just a little clarification to my previous post: When I said that the best thing would be to change the timezone, I didn't mean the timezone of the SQL server, nor changing it permanently. I meant casting the timezone upon retrieval, using the CONVERT_TZ() function. Dates should always be stored in GMT, as far as I am (and many others are) concerned. Quote Link to comment https://forums.phpfreaks.com/topic/274735-what-is-a-good-way-to-subtract-5-hours-from-a-timestamp/#findComment-1413686 Share on other sites More sharing options...
exeTrix Posted February 20, 2013 Share Posted February 20, 2013 OO solution: //create a new DateTime instance with no value passed through to construct, equiv to date() $dateTimeObj = new DateTime(); //now we need to create an interval object for the number of hours we wish to deduct $intervalObj = DateInterval::createFromDateString( '5 hours' ); //subtract the interval from the DateTime object $dateTimeObj->sub( $intervalObj ); //format and output our date and time with the 5 hours subtracted echo $dateTimeObj->format( 'Y-m-d H:i:s a' ); //and if you want to add same process except you use the add method $dateTimeObj->add( $intervalObj ); //format and output our date and time with the 5 hours added echo $dateTimeObj->format( 'Y-m-d H:i:s a' ); Hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/274735-what-is-a-good-way-to-subtract-5-hours-from-a-timestamp/#findComment-1413690 Share on other sites More sharing options...
Christian F. Posted February 20, 2013 Share Posted February 20, 2013 The reason why I recommend converting the timezone, instead of subtracting (or adding) a number of hours is simple: Summer/winter time. If you simply convert the timezone PHP will automatically adjust for summer/winter time. If you manually change the number of hours, you have to write the logic to take this into account as well. Not to mention leap time. Quote Link to comment https://forums.phpfreaks.com/topic/274735-what-is-a-good-way-to-subtract-5-hours-from-a-timestamp/#findComment-1413701 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.