Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

big_malk

macrumors 6502a
Original poster
Aug 7, 2005
557
1
Scotland
I'm using a shared Linux server running PHP / MySQL and it is in the US.
Currently, mktime() is giving a time almost 10am, but because I'm in the UK I'm trying to use gmmktime(), but it's giving a time of 5am, while it's actually just gone 4pm!

Since it's a shared server, I don't think there's much I can do about the time settings? Is it time to ask the server support people for advice, or am I missing something?
 
That function produces a Unix timestamp based on GMT time of the server in the United States, not the local time set there. This timestamp is good for database storage if that's what you're really trying to do. After retreival from the database, simply convert the GMT timestamp to local by adding or subtracting the number of hours offset from GMT based on their local time zone. Be sure to account for DST as well, the extra hour matters in certain parts of the year. The user will then see their local time and zone.

From the PHP docs:

Identical to mktime() except the passed parameters represents a GMT date. gmmktime() internally uses mktime() so only times valid in derived local time can be used.

So it is important the local time be correct, of course since the GMT calculations are based on local time. I doubt that's the issue - alot of users would be complaining, big time.

-jim
 
I'm using a shared Linux server running PHP / MySQL and it is in the US.
Currently, mktime() is giving a time almost 10am, but because I'm in the UK I'm trying to use gmmktime(), but it's giving a time of 5am, while it's actually just gone 4pm!

Since it's a shared server, I don't think there's much I can do about the time settings? Is it time to ask the server support people for advice, or am I missing something?

I found this on the PHP manual on php.net:

gmmktime() should ONLY be used to create a timestamp when specifying a specific GMT date and time.

If you want to make a valid time stamp for the current date & time, use mktime() instead.

UNIX timestamps, by definition, store the GMT time relative to the UNIX epoch.

gmmktime() (without any parameters specified) will effectively use the computer's LOCAL time values just the same as if they were explicit parameters, and the resulting time stamp will be incorrect. (The resulting timestamp will actually be offset in the OPPOSITE direction of the local timezone offset from GMT!)


And this will probably also help:

Beware that despite the documentation which states is_dst is ignored, with PHP 5.2 at least, it is not actually ignored and will cause a 1 hour offset on the UTC time returned.

This caused some interesting bugs, especially with the tzdelta function shown from previous posts below - you need to make the final parameter a 0 instead of $ar[8] otherwise you get an off-by-1-hour as a result.

As a result, I now use :

<?php
function tzdelta ( $iTime = 0 ) {
if ( 0 == $iTime ) { $iTime = time(); }
$ar = localtime ( $iTime );
$ar[5] += 1900; $ar[4]++;
$iTztime = gmmktime ( $ar[2], $ar[1], $ar[0], $ar[4], $ar[3], $ar[5], 0);
return ( $iTztime - $iTime );
}
?>


Hope it helps anyways.... Fabian
 
I found this on the PHP manual on php.net:

gmmktime() should ONLY be used to create a timestamp when specifying a specific GMT date and time.

If you want to make a valid time stamp for the current date & time, use mktime() instead.

UNIX timestamps, by definition, store the GMT time relative to the UNIX epoch.

gmmktime() (without any parameters specified) will effectively use the computer's LOCAL time values just the same as if they were explicit parameters, and the resulting time stamp will be incorrect. (The resulting timestamp will actually be offset in the OPPOSITE direction of the local timezone offset from GMT!)


And this will probably also help:

Beware that despite the documentation which states is_dst is ignored, with PHP 5.2 at least, it is not actually ignored and will cause a 1 hour offset on the UTC time returned.

This caused some interesting bugs, especially with the tzdelta function shown from previous posts below - you need to make the final parameter a 0 instead of $ar[8] otherwise you get an off-by-1-hour as a result.

As a result, I now use :

<?php
function tzdelta ( $iTime = 0 ) {
if ( 0 == $iTime ) { $iTime = time(); }
$ar = localtime ( $iTime );
$ar[5] += 1900; $ar[4]++;
$iTztime = gmmktime ( $ar[2], $ar[1], $ar[0], $ar[4], $ar[3], $ar[5], 0);
return ( $iTztime - $iTime );
}
?>


Hope it helps anyways.... Fabian

Actually, that just confused me and I'm not certain what it's trying to do lol

I have discovered the wonders of of gmdate(), which I should have found before (duh) but I'm still not getting the accurate time, it's 1 hour behind. gmdate('I') is returning 0, indicating that daylight saving (aka 'british summertime') is not active, which would explain it!

I can't find anything I can do to change this?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.