Jump to content

What is a good way to subtract 5 hours from a timestamp?


yoursurrogategod

Recommended Posts

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?

Link to comment
Share on other sites

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 by Christian F.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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. ;)

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.