How to get local system time in PHP?

I am writing a PHP system and I need to get the system time. Not GMT time or time zone specific time, but the same system time used by the CRON system. I have a CRON job that runs every day at midnight, and I want to show on the web page how long it will take before it starts up again.

For example: Now on my system clock - 6 hours. 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, although the correct one should be 18:00.

Thanks.

+7
php time cron
source share
10 answers

php 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, perhaps you better use a little javascript

+5
source share

There are many answers, but at the time of writing the letter was not even correct.

The PHP time() function does not return the system time, as most people believe, but returns the local PHP time, usually set using date.timezone in php.ini or setting with date_default_timezone_set() in a script.

For example, on one of my servers the PHP time was Europe/Rome and the UTC system time. I had an hour difference between system time and PHP time.

I am going to give you a solution that works for Linux, I don’t know for Windows. On Linux, the system time zone is set to /etc/timezone . Now this is usually outside the valid open_basedir parameter, but you can add :/etc/timezone to your list to read the file.

Then on top of scripts that want to get the system time, you can call the library function, which sets the script time zone to the system time zone. I believe this function is part of the 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 matters worse in PHP 5.3.3, β€œEtc / UTC” is not recognized, while β€œUTC” is, so I had to add an if to fix this.

Now you can happily call time() , and that will really give you the system time. I tested it because I needed it for myself, so I found this question now.

+5
source share

time()

will give you the current system timestamp.

Guide

Returns the current time, measured in seconds, since the Unix era (January 1, 1970 00:00:00 GMT).

0
source share

You can get the date / time of the server that PHP is running on using the time() function - it will return the timestamp corresponding to the current time and time.

This system time on this server is the same as cron uses.

0
source share

If you want to use GMT, you can use gmtstrftime() , which will give you the system time, but like in GMT. There is more information at http://us2.php.net/gmstrftime .

0
source share

If you are after a formatted date:

 date ('d/m/yh:i:s'); 

will perform the trick, there is no need to pass the time () to the date, since it will be the default current system time if the second parameter is not specified.

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

Otherwise, you can simply use

 time() 

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

0
source share

try the following:

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

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

0
source share

To get the current time of your system, you need to set the correct date.timezone in the php.ini file. For example, if you are from India, you should write:

 date.timezone = Asia/Calcutta 

For Germany, it will be:

 date.timezone = Europe/Berlin 

After that, date("Ymd H:i:s") will provide your current time. For a time zone, see the list of time zones supported by PHP .

0
source share

This is the easiest and most reliable way to do this:

 $sys_timestamp = strtotime(exec("date")); 

Do not try to fake it with php, let's just get real sys time;)

(Will work on any unix based system)

0
source share

The current local time can be used using Javascript and set it to any PHP variable than.

  <script> // For 24 hours var ct = new Date(); var hr = ct.getHours(); var mt = ct.getMinutes(); if (mt < 10) { mt = "0" + mt; } document.write("<b>" + hr + ":" + mt + " " + "</b>"); // For 12 hours (AM / PM) var ct = new Date(); var hr = ct.getHours(); var mt = ct.getMinutes(); var ampm = "AM"; if (hr >= 12) { ampm = "PM"; hr = hr - 12; } if (hr == 0) { hr = 12; } if (mt < 10) { mt = "0" + mt; } document.write("<b>" + hr + ":" + mt + " " + ampm + "</b>"); </script> 
0
source share

All Articles