如何获取PHP中的当前日期和时间? [英] How do I get the current date and time in PHP?
本文介绍了如何获取PHP中的当前日期和时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
哪个PHP函数可以返回当前日期/时间?
Which PHP function can return the current date/time?
推荐答案
时间将取决于您的服务器时间.一个简单的解决方法是使用 date()
或 time()
函数.
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();
所以像这样:
$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 :)
这篇关于如何获取PHP中的当前日期和时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文