How do I get the current date and time in PHP?

后端 未结 30 2138
孤城傲影
孤城傲影 2020-11-22 08:19

Which PHP function can return the current date/time?

30条回答
  •  执念已碎
    2020-11-22 09:04

    The time would go by your server time. An easy workaround for this is to manually set the timezone by using date_default_timezone_set before the date() or time() functions are called to.

    I'm in Melbourne, Australia so I have something like this:

    date_default_timezone_set('Australia/Melbourne');
    

    Or another example is LA - US:

    date_default_timezone_set('America/Los_Angeles');
    

    You can also see what timezone the server is currently in via:

    date_default_timezone_get();
    

    So something like:

    $timezone = date_default_timezone_get();
    echo "The current server timezone is: " . $timezone;
    

    So the short answer for your question would be:

    // Change the line below to your timezone!
    date_default_timezone_set('Australia/Melbourne');
    $date = date('m/d/Y h:i:s a', time());
    

    Then all the times would be to the timezone you just set :)

提交回复
热议问题