Home » Php » How do I get the local system time in PHP?

How do I get the local system time in PHP?

Posted by: admin July 12, 2020 Leave a comment


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.


How to&Answers:

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


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';

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.


This is the easiest and most foolproof way to do it:

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

Let’s not try to spoof it with php, let’s just get the real sys time 😉

(Will work on any unix based system)



will give you the current system timestamp.


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


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.


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.


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


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:

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.


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.


You can current local time via below Javascript and set it in any PHP variable than.


        // 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>");