Page 1 of 1
Adding timezone to timestamp
Posted: Sun Feb 19, 2006 7:33 pm
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.
Posted: Sun Feb 19, 2006 7:43 pm
by feyd
the first parameter/argument for date() is the format string, not a timestamp.

You do the math in the second parameter/argument.
Posted: Sun Feb 19, 2006 7:45 pm
by Benjamin
Like this?
Code: Select all
$zone = $timezone * 60;
$timestamp = date(YmdHis, $timestamp+$zone);
Posted: Sun Feb 19, 2006 7:51 pm
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;
Posted: Sun Feb 19, 2006 7:57 pm
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.
Posted: Sun Feb 19, 2006 8:13 pm
by feyd
Code: Select all
$formatted = date('YmdHis', $timestamp + $timezone * 60);
Posted: Sun Feb 19, 2006 8:23 pm
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 = ??
Posted: Sun Feb 19, 2006 8:41 pm
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
Posted: Sun Feb 19, 2006 9:42 pm
by Benjamin
Ok, that worked. Thank you.