哪个PHP函数可以返回当前日期/时间?
时间将按照您的服务器时间流逝。一个简单的解决方法是在调用date()
或time()
函数之前,使用date_default_timezone_set
手动设置时区。
我在澳大利亚的墨尔本,所以我的设置如下:
date_default_timezone_set('Australia/Melbourne');
还有一个例子是洛杉矶 - 美国:
date_default_timezone_set('America/Los_Angeles');
您还可以通过以下方式查看服务器当前所处的时区:
date_default_timezone_get();
所以像这样:
$timezone = date_default_timezone_get();
echo "The current server timezone is: " . $timezone;
所以,对于你的问题的简短答案是:
// Change the line below to your timezone!
date_default_timezone_set('Australia/Melbourne');
$date = date('m/d/Y h:i:s a', time());
那么所有时间都将是你刚设置的时区的时间:)
// 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.
如需时区参考,请参见支持的时区列表。
自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
并且要指定timezone
:
$now = new DateTime(null, new DateTimeZone('America/New_York'));
$now->setTimezone(new DateTimeZone('Europe/London')); // Another way
echo $now->getTimezone();
Datatime
命名空间的问题;我不记得在2013年时是否是这样,并且多年来我也没有编写过 PHP 代码。无论如何,您的建议似乎是正确的,再次感谢。 - Mahdi参考资料:这里是链接
相对于仅仅添加或减去一天或一个月的秒数到时间戳来说,这种方法更加可靠,因为它考虑了夏令时的影响。
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)
您可以使用 $_SERVER['REQUEST_TIME']
变量(自 PHP 5.1.0 起可用)或 time()
函数 来获取当前的Unix时间戳。
time()
函数返回的时间戳与时区无关。(因此,在调用 date_default_timezone_set("your-particular-timezone");
之前设置时区将不起作用。) - ban-geoengineering$_SERVER['REQUEST_TIME']
变量或 time()
函数。这两个函数都返回 Unix 时间戳。$_SERVER['REQUEST_TIME']
返回最近服务器请求的时间戳,而 time()
返回当前时间。这可能会在精度上产生微小差异,具体取决于您的应用程序,但对于大多数情况,这两种解决方案都应该足够。1232659628
因此,为了得到可用的内容,您可以使用 date()
函数来格式化它。date()
函数的方法的良好参考资料位于PHP 手册中。01/22/2009 04:35:00 pm
:echo date("m/d/Y h:i:s a", time());
$_SERVER['REQUEST_TIME']
? - PacerierPHP的date函数可以完成这项工作。
描述:
string date(string $format [, int $timestamp = time()])
$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)
对于新的PHP程序员来说,可能会困惑为什么有很多方法可以获取当前日期和时间,以及在他们的项目中应该使用哪个。
1. date
方法(PHP 4、PHP 5、PHP 7)
这是在php中获取日期和时间非常常见且最简单的方法。
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
// Prints something like: Monday
echo date("l");
// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A');
// Prints: July 1, 2000 is on a Saturday
echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));
/* use the constants in the format parameter */
// prints something like: Wed, 25 Sep 2013 15:28:57 -0700
echo date(DATE_RFC2822);
// prints something like: 2000-07-01T00:00:00+00:00
echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
您可以在这里了解更多。
2. DateTime
类(PHP 5 >= 5.2.0,PHP 7)
当您想要使用PHP面向对象编程时,这是获取日期和时间的最佳方法。
<?php
// Specified date/time in your computer's time zone.
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:sP') . "\n";
// Specified date/time in the specified time zone.
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
// Current date/time in your computer's time zone.
$date = new DateTime();
echo $date->format('Y-m-d H:i:sP') . "\n";
// Current date/time in the specified time zone.
$date = new DateTime(null, new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
// Using a UNIX timestamp. Notice the result is in the UTC time zone.
$date = new DateTime('@946684800');
echo $date->format('Y-m-d H:i:sP') . "\n";
// Non-existent values roll over.
$date = new DateTime('2000-02-30');
echo $date->format('Y-m-d H:i:sP') . "\n";
?>
您可以在这里了解更多。
3. Carbon 日期时间包
如果你正在使用Composer,Laravel,Symfony或任何一种框架,这是获取日期和时间的最佳方法。此外,此包扩展了PHP中的DateTime类,因此您可以使用Datetime类中的所有方法。 这个包已经内置在像Laravel这样的框架中,所以您不必单独安装它。
printf("Right now is %s", Carbon::now()->toDateTimeString());
printf("Right now in Vancouver is %s", Carbon::now('America/Vancouver')); // automatically converted to string
$tomorrow = Carbon::now()->addDay();
$lastWeek = Carbon::now()->subWeek();
// Carbon embed 823 languages:
echo $tomorrow->locale('fr')->isoFormat('dddd, MMMM Do YYYY, h:mm');
echo $tomorrow->locale('ar')->isoFormat('dddd, MMMM Do YYYY, h:mm');
$officialDate = Carbon::now()->toRfc2822String();
$howOldAmI = Carbon::createFromDate(1975, 5, 21)->age;
$noonTodayLondonTime = Carbon::createFromTime(12, 0, 0, 'Europe/London');
$internetWillBlowUpOn = Carbon::create(2038, 01, 19, 3, 14, 7, 'GMT');
if (Carbon::now()->isWeekend()) {
echo 'Party!';
}
echo Carbon::now()->subMinutes(2)->diffForHumans(); // '2 minutes ago'
您可以在此处了解更多信息。
希望这有所帮助,如果您知道任何其他获取日期和时间的方法,请随意编辑答案。
使用:
$date = date('m/d/Y h:i:s a', time());
它运行正常。
$date = date('m/d/Y h:i:s a', time());
翻译:将当前时间戳格式化为 "月/日/年 时:分:秒 上午/下午" 的形式,赋值给变量 $date。 - I am the Most Stupid Person