View Full Version : time() function - is that GMT?!
Simon
05-02-2003, 08:50 PM
I was pretty sure that the time() function responded with a Unix timestamp of the computer's current time, not the current GMT. But when I posted a note on the PHP documentation some other guy responded that I was inaccurate - what do you guys think?
Using this code on my home computer (XP Pro) and on IH servers seems to render the computer's current time.
<?= strftime("%H:%M", time()); ?>
http://au.php.net/manual/en/function.time.php (My post was on 10-Apr)
< Simon >
pete3005
05-03-2003, 12:17 AM
Try this:
<?php echo date("D jS F Y h:i a",(time()));?>
and change your PC clock and time zone locally.
Test on the server too.
time() is GMT time and not server time.
strftime() sets time based on local time settings which is why you are seeing what you are seeing. Rather like inserting a date in MySQL with NOW().
Pete :)
Simon
05-05-2003, 04:07 PM
I thought date() gave me a similar response (I think that was how I originally found the problem). I'll check when I'm back on my home computer.
And from the manual, it seems that strftime() uses your local time only when you don't specify a timestamp, but since I specify "time()" shouldn't it work with that time I've given it (GMT).
< Simon >
pete3005
05-05-2003, 04:51 PM
strftime gives local time depending on what setlocale() is set to.
time() whether on its own or with date() gives the server time, not GMT time.
So running:
strftime: <?php strftime("%H:%M", time()); ?>
<br />
date: <?php echo date("D jS F Y h:i a",(time()));?>
<br />
time <?php echo time();?>
locally will give GMT time and running the same on my server gives different time.... - 5 hours on one server and -8 on IH servers.
Its almost 2am and I'm not at my best right now but I am sure that is correct, if not please let me know. As in my previous posts I use a constant in my config files to set the time difference.
So you were correct to start with BUT on my local machine when I change my clocks time zone, I get the correct GMT time, even though I set windows clock to EST time..... hmmm weird... I'll have to look into this but perhaps that 's a weird windows thing.
To summarise, yes you are correct, time() is computer time and nothing to do with GMT, well not on my servers anyway but a different result appears on Windows locally.... time to sleep!
Pete
on my local machine when I change my clocks time zone, I get the correct GMT time, even though I set windows clock to EST time..... hmmm weird...
echo 'date:' . date("jS F Y h:i:s a", time()) . '<br />';
echo 'strftime:' . strftime("%d %b %G %I:%M:%S %p", time()). '<br />';
The above code produces the correct GMT time even after I change my local Win2k machine from PST to HST (Hawaii).
It also displays the correct GMT time from an IH server (http://mtheaded.net/time.php).
Sounds like the manual is correct.
Although one question. The manual says the following regarding strftime:
"Returns a string formatted according to the given timestamp format string using the given timestamp or the current local time if no timestamp is given. "
What does "current local time" mean? I did not provide a timestamp argument such as time(), but it still displayed the GMT time. Souds like strftime, like date, defaults to time().
pete3005
05-05-2003, 06:22 PM
Originally posted by g808
The above code produces the correct GMT time even after I change my local Win2k machine from PST to HST (Hawaii).
It also displays the correct GMT time from an IH server (http://mtheaded.net/time.php).
The code does work fine on my local machine, GMT but not on the server:
http://www.petehawkes.co.uk/t.php
That is -8 hours which is server time, that is not GMT. So locally on Windows XP I get one result, on any server outside GMT I get another result.
Pete
pete3005
05-05-2003, 06:24 PM
I think we are talking about two different things here, time() is the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT), but the time is calculated from the computers time, PHP doesn't connect to any outside source so it determines GMT from the current time of the computer. The current time could be anything but the time is still calculated from the Unix Epoch whether that time is incorrect or not.
Windows does change the PHP time if you change your clocks time but NOT your PC clock time zone.
So the answer is yes time() is calculated to GMt but it doesn't return GMT time, that depends on the server or PC time settings, if they are wrong your time is wrong.
Hope that makes sense.
Pete :)
Simon
05-05-2003, 10:35 PM
<QUOTE>
So the answer is yes time() is calculated to GMt but it doesn't return GMT time, that depends on the server or PC time settings, if they are wrong your time is wrong.
</QUOTE>
How can time() be calculated to GMT but not return GMT time? Could you rephrase - I'm getting quite confused.
< Simon >
Simon
05-05-2003, 10:50 PM
I ran this code...
strftime: <?php echo strftime("%H:%M", time()); ?>
<br />
date: <?php echo date("D jS F Y h:i a",(time()));?>
<br />
time <?php echo time();?>
And on my home machine (XP Pro) the first two strings print my local time twice (GMT +10 hours).
On my IH server here (http://simoneast.net/test_time.php), it prints what looks to be the server's local time (GMT -7 hours I think).
Both strftime() and date() seem to act the same and both are printing the computer's current time, not GMT.
...However, the Unix timestamps were nearly identical when I ran the script locally and on the server within a few seconds of each other. I'm not sure what time it represents, but it seems to be GMT (if they're both the same). So perhaps the epoch is in GMT, but the two functions which print the time convert to local time, even when you feed the function the epoch returned from time(). That sounds pretty weird!
< Simon >
pete3005
05-06-2003, 12:05 AM
Originally posted by Simon
<QUOTE>
So the answer is yes time() is calculated to GMt but it doesn't return GMT time, that depends on the server or PC time settings, if they are wrong your time is wrong.
</QUOTE>
How can time() be calculated to GMT but not return GMT time? Could you rephrase - I'm getting quite confused.
< Simon >
time() is the UNIX Epoch so it calculates the seconds since 1970 GMT, but if your PC's clock or server clock is wrong then PHP doesn't know that unless you use setlocale() to set the time offset.
I realise it sounds confusing but that's because it is!
Pete :)
pete3005
05-06-2003, 12:09 AM
Just to add, I use this rule. Do all your time and date input/calculations as normal but when on a server that is not the same as the local time you want, define a constant like:
define("TIME_DIFF",28800);
then when displaying time convert any non Unix timestamps such as MySQL Datetime using strtotime() and then +TIME_DIFF.
It may be worth digging insomething like PHP to see how they handle it, but I imagine its the same way.
Pete
vBulletin® v3.8.4, Copyright ©2000-2012, Jelsoft Enterprises Ltd.