Adding timezone to timestamp

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Adding timezone to timestamp

Post by Benjamin »

I am trying to add a timezone to a timestamp (or subtract it).

Timezone is minutes.

Code: Select all

$zone = $timezone * 60;
$timestamp = date($timestamp, $zone);
This doesn't seem to work.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the first parameter/argument for date() is the format string, not a timestamp. ;) You do the math in the second parameter/argument.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Like this?

Code: Select all

$zone = $timezone * 60;
$timestamp = date(YmdHis, $timestamp+$zone);
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Maybe this is it. I just need to add them not display them.

Code: Select all

$zone = time() + ($timezone * 60);
  $timestamp = $timestamp + $zone;
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I am lost.

I have a timestamp, not a current timestamp, but a timestamp which could be anything. I need to add a timezone to it. I don't have the slightest clue how to do this.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$formatted = date('YmdHis', $timestamp + $timezone * 60);
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

feyd wrote:

Code: Select all

$formatted = date('YmdHis', $timestamp + $timezone * 60);
Here are the results, something isn't correct.

TimeStamp = : 20060101010000 = 2006 January 1st 1am no minutes or seconds
TimeZone = : 780 = add 780 minutes
New TimeStamp = : 19160529205944 = ??
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Well there's the source of confusion. That is a numeric string from a database such as MySQL. You need to translate that into a unix timestamp first. date() works off of unix timestamps only.

Either convert it in the database during selection (often preferred) or convert it in php after selection.

Code: Select all

$timestamp = mktime(substr($timestamp,8,2),substr($timestamp,10,2),substr($timestamp,12,2),substr($timestamp,4,2),substr($timestamp,6,2),substr($timestamp,0,4)); // untested
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Ok, that worked. Thank you.
Post Reply