How do I get the local system time in PHP?

2019-02-13 13:55发布

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.

标签: php time cron
10条回答
啃猪蹄的小仙女
2楼-- · 2019-02-13 14:00

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.

查看更多
男人必须洒脱
3楼-- · 2019-02-13 14:01

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 with date.timezone in php.ini, or set with date_default_timezone_set() within a script.

For instance in one of my servers, PHP time was set to Europe/Romeand system time to UTC. 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 allowed open_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:

static function setSystemTz() {
    $systemTz = trim(file_get_contents("/etc/timezone"));
    if ($systemTz == 'Etc/UTC') $systemTz = 'UTC';
    date_default_timezone_set($systemTz);
}

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.

查看更多
Deceive 欺骗
4楼-- · 2019-02-13 14:02

try this one:

$time=date("h:i:s A", strtotime("now"-14));
echo $time;

You can adjust the time by changing the number 14 above.

查看更多
叼着烟拽天下
5楼-- · 2019-02-13 14:05

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.

查看更多
疯言疯语
6楼-- · 2019-02-13 14:11

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:

date.timezone = Asia/Calcutta

For Germany, it would be:

date.timezone = Europe/Berlin

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.

查看更多
家丑人穷心不美
7楼-- · 2019-02-13 14:14

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.

查看更多
登录 后发表回答