tags:

views:

381

answers:

6

Hello,

I'm writing a PHP system and I need to get the system time. Not the GMT time or the time specific to a timezone, but the same system time that is used by the CRON system. I have a CRON job that runs every day at midnight and I want to show on a webpage how long will it take before it runs again.

For example: Right now it is 6pm on my system clock. I run the code:

$timeLeftUntilMidnight = date("H:i", strtotime("tomorrow") - strtotime("now"));

The result, however, is "3:00" instead of "6:00". If I run

date("H:i", strtotime("tomorrow"));

It returns 0:00, which is correct. But if I run

date("H:i", strtotime("now"));

It returns 21:00, even though the correct should be 18:00.

Thanks.

+5  A: 

php's time will return the system time. you can format it with date

if you just want to display the time in the local time of the visitor maybe you're better off using a little javascript

knittl
I tried this, date("H:i", time()) is returning the GMT time, not the local system time.
JohnWithoutArms
are you sure your server time is not set to GMT? are your cron jobs run on the same server as your php scripts?
knittl
The result from the date("H:i", time()) call is the same on both my Windows machine (with system clock set to local time) and on a Linux machine (checked the time with "top", it is the same local time as my Windows machine). The result is the (correct) GMT time, not the local time.
JohnWithoutArms
The CRON jobs are run on the Linux machine where the application is actually deployed. System time is local, but the code still returns GMT time.
JohnWithoutArms
A: 

time()

will give you the current system timestamp.

Manual

Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

Pekka
A: 

You can get the date/time of the server on which PHP is running using the time() function -- it'll return a timestamp, that corresponds to the current datetime.

It's the system time, on that server -- the same as used by cron.

Pascal MARTIN
A: 

How about using the localtime function.

Sarfraz
localtime() differs from time() only in the structure returned
JohnWithoutArms
@JohnWithoutArms: but it also returns system time, right?
Sarfraz
It gives me the same result as the time() function which, when formatted with date(), is giving me a time that is actually GMT, which is 3 hours ahead of my system time.
JohnWithoutArms
you can see the documentation of localtime by clicking on it and there various people have proposed the solution for that in comments section, check that out too.
Sarfraz
A: 

If you want the GMT time you may want to use gmtstrftime(), which will give you the system time but as in GMT. There's more info at http://us2.php.net/gmstrftime.

Peeps
The time() function IS giving me the GMT time; I don't want the GMT time, I want the current system time.
JohnWithoutArms
A: 

If you are after a formatted date:

date ('d/m/y h:i:s'); 

will do the trick, there is no need to pass time() into date as it will default to the current system time if no second parameter is supplied.

for more formatting options see here: http://php.net/manual/en/function.date.php

Otherwise you can just use

time()

To get you the current unix timestamp as others have mentioned.

Jake Worrell
I understand that, the problem is that that date() call is giving me the GMT time, which is 3 hours ahead of my system time.
JohnWithoutArms
@JohnWithoutArms, are you sure your server system is not set to GMT? Date() without arguments should give you the current timestamp of the system, no timezone fiddling involved.
Pekka
Yes... I was testing on a Windows machine, and the time on the tray is indeed my local time, and not GMT. Thinking this could be a Windows problem, I got my code on a Linux server, checked the time with "top" (it was correct, local time), ran the code and the results were the same.
JohnWithoutArms