如何在 PHP 中获取当前日期和时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/470617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the current date and time in PHP?
提问by
Which PHP function can return the current date/time?
哪个 PHP 函数可以返回当前日期/时间?
回答by NJ.
The time would go by your server time. An easy workaround for this is to manually set the timezone by using date_default_timezone_setbefore the date()or time()functions are called to.
时间将按照您的服务器时间。一个简单的解决方法是在调用或函数date_default_timezone_set之前使用手动设置时区。date()time()
I'm in Melbourne, Australiaso I have something like this:
我在墨尔本,澳大利亚所以我有这样的事情:
date_default_timezone_set('Australia/Melbourne');
Or another example is LA - US:
或者另一个例子是 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 :)
那么所有的时间都会是你刚刚设置的时区:)
回答by Shadrack B. Orina
// Simply:
$date = date('Y-m-d H:i:s');
// Or:
$date = date('Y/m/d H:i:s');
// This would return the date in the following formats respectively:
$date = '2012-03-06 17:33:07';
// Or
$date = '2012/03/06 17:33:07';
/**
* This time is based on the default server time zone.
* If you want the date in a different time zone,
* say if you come from Nairobi, Kenya like I do, you can set
* the time zone to Nairobi as shown below.
*/
date_default_timezone_set('Africa/Nairobi');
// Then call the date functions
$date = date('Y-m-d H:i:s');
// Or
$date = date('Y/m/d H:i:s');
// date_default_timezone_set() function is however
// supported by PHP version 5.1.0 or above.
For a time-zone reference, see List of Supported Timezones.
有关时区参考,请参阅支持的时区列表。
回答by Mahdi
Since PHP 5.2.0you can use the DateTime()class:
由于 PHP,5.2.0您可以使用DateTime()该类:
use \Datetime;
$now = new DateTime();
echo $now->format('Y-m-d H:i:s'); // MySQL datetime format
echo $now->getTimestamp(); // Unix Timestamp -- Since PHP 5.3
And to specify the timezone:
并指定timezone:
$now = new DateTime(null, new DateTimeZone('America/New_York'));
$now->setTimezone(new DateTimeZone('Europe/London')); // Another way
echo $now->getTimezone();
回答by ShapCyber
Reference: Here's a link
参考:这是一个链接
This can be more reliable than simply adding or subtracting the number of seconds in a day or a month to a timestamp because of daylight saving time.
由于夏令时,这比简单地在时间戳中添加或减去一天或一个月中的秒数更可靠。
The PHP code
PHP代码
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone
$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
$today = date("m.d.y"); // 03.10.01
$today = date("j, n, Y"); // 10, 3, 2001
$today = date("Ymd"); // 20010310
$today = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 10th day.
$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month
$today = date("H:i:s"); // 17:16:18
$today = date("Y-m-d H:i:s"); // 2001-03-10 17:16:18 (the MySQL DATETIME format)
回答by Rob Hruska
PHP's time()returns a current Unix timestamp. With this, you can use the date()function to format it to your needs.
PHP 的time()返回当前的 Unix 时间戳。有了这个,您可以使用date()函数根据您的需要对其进行格式化。
$date = date('Format String', time());
As Paolo mentioned in the comments, the second argument is redundant. The following snippet is equivalent to the one above:
正如 Paolo 在评论中提到的,第二个论点是多余的。以下代码段等效于上面的代码段:
$date = date('Format String');
回答by Gumbo
You can either use the $_SERVER['REQUEST_TIME']variable(available since PHP 5.1.0) or the time()functionto get the current Unix timestamp.
您可以使用$_SERVER['REQUEST_TIME']变量(自 PHP 5.1.0 起可用)或time()函数来获取当前的 Unix 时间戳。
回答by Grizzly Peak Software
You can use both the $_SERVER['REQUEST_TIME']variable or the time()function. Both of these return a Unix timestamp.
您可以使用$_SERVER['REQUEST_TIME']变量或time()函数。这两个都返回一个 Unix 时间戳。
Most of the time these two solutions will yield the exact same Unix Timestamp. The difference between these is that $_SERVER['REQUEST_TIME']returns the time stamp of the most recent server request and time()returns the current time. This may create minor differences in accuracy depending on your application, but for most cases both of these solutions should suffice.
大多数情况下,这两种解决方案将产生完全相同的Unix Timestamp。它们之间的区别在于$_SERVER['REQUEST_TIME']返回最近服务器请求的时间戳并time()返回当前时间。这可能会根据您的应用程序在准确性上产生细微差异,但对于大多数情况,这两种解决方案都足够了。
Based on your example code above, you are going to want to format this information once you obtain the Unix Timestamp. Unformatted Unix timelooks like: 1232659628
根据您上面的示例代码,您将需要在获得Unix Timestamp 后格式化此信息。未格式化的Unix 时间如下所示:1232659628
So in order to get something that will work, you can use the date()function to format it.
因此,为了获得有用的东西,您可以使用该date()函数对其进行格式化。
A good reference for ways to use the date()function is located in the PHP Manual.
有关使用该date()函数的方法的一个很好的参考位于PHP 手册 中。
As an example, the following code returns a date that looks like this: 01/22/2009 04:35:00 pm:
例如,以下代码返回一个如下所示的日期01/22/2009 04:35:00 pm:
echo date("m/d/Y h:i:s a", time());
回答by Subodh Ghulaxe
PHP's datefunction can do this job.
PHP 的日期函数可以完成这项工作。
Description:
描述:
string date(string $format [, int $timestamp = time()])
Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given.
返回根据给定格式字符串格式化的字符串,使用给定的整数时间戳或当前时间(如果没有给定时间戳)。
Examples:
例子:
$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
$today = date("m.d.y"); // 03.10.01
$today = date("j, n, Y"); // 10, 3, 2001
$today = date("Ymd"); // 20010310
$today = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 10th day.
$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month
$today = date("H:i:s"); // 17:16:18
$today = date("Y-m-d H:i:s"); // 2001-03-10 17:16:18 (the MySQL DATETIME format)
回答by Kailas
$date = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
echo $date->format('d-m-Y H:i:s');
Update
更新
//Also get am/pm in datetime:
echo $date->format('d-m-Y H:i:s a'); // output 30-12-2013 10:16:15 am
For the date format, PHP date() Functionis useful.
对于日期格式,PHP date() 函数很有用。
回答by Juned Ansari
its very simple
echo $date = date('Y-m-d H:i:s');
它非常简单
echo $date = date('Ymd H:i:s');

