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.
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.There are many answers, however there is not even one correct at the time of writing.
PHP
time()
function doesn't return the system time, like most folks believe, but it return the PHP localtime, normally set withdate.timezone
in php.ini, or set withdate_default_timezone_set()
within a script.For instance in one of my servers, PHP time was set to
Europe/Rome
and system time toUTC
. I had a difference of one hour between system time and PHP time.I'm going to give you a solution that works for Linux, I don't know for Windows. In Linux the system timezone is set in
/etc/timezone
. Now, this is normally outside my allowedopen_basedir
setting, but you can add:/etc/timezone
to your list to be able to read the file.Then, on top of the scripts, that want to get the system time, you can call a library function that sets the script timezone to the system timezone. I suppose that this function is part of a class, so I use static:
To make the matter worse in PHP 5.3.3 'Etc/UTC' is not recognized, while 'UTC' is, so I had to add an if to fix that.
Now you can happily call
time()
and it will really give you the system time. I've tested it, because I needed it for myself, that's why I found this question now.try this one:
You can adjust the time by changing the number
14
above.If you are after a formatted date:
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
To get you the current unix timestamp as others have mentioned.
For getting the current time of your system you need to set the correct
date.timezone
in your php.ini file. For example if you are from India then you would write:For Germany, it would be:
After doing this,
date("Y-m-d H:i:s")
will give your current time. For getting your timezone see the list of timezones supported by PHP.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.