哪个PHP函数可以返回当前日期/时间?
当前回答
PHP的time()返回当前Unix时间戳。这样,您就可以使用date()函数将其格式化为您需要的格式。
$date = date('Format String', time());
正如Paolo在评论中提到的,第二个论点是多余的。下面的代码段相当于上面的代码段:
$date = date('Format String');
其他回答
对于新的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)
当你想在OOP中使用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.碳日期时间套餐
如果你正在使用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'
你可以在这里了解更多
希望这对你有所帮助,如果你知道任何其他获取日期和时间的方法,请随意编辑答案。
// 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.
有关时区参考,请参见支持的时区列表。
Linux服务器时间与PHP time()时区差异如下:
<?php
putenv("TZ=Asia/Kabul");
$t = time();
echo date('d/m/Y H:i:sa', $t);
?>
另一种简单的方法是获取当前日期和时间的时间戳。使用mktime()函数:
$now = mktime(); // Return timestamp of the current time
然后您可以将其转换为另一种日期格式:
//// Prints something like: Thursday 26th of January 2017 01:12:36 PM
echo date('l jS \of F Y h:i:s A',$now);
更多的日期格式在这里: http://php.net/manual/en/function.date.php
日期格式也取决于:
echo date("d/m/Y H:i:sa"); // 13/04/2017 19:38:15pm
推荐文章
- 在foreach中通过引用传递
- 如何触发命令行PHP脚本的XDebug分析器?
- 如何找出如果你使用HTTPS没有$_SERVER['HTTPS']
- 更好的方法检查变量为null或空字符串?
- 当使用Composer的开发/生产开关时,如何正确部署?
- 自动删除Laravel (Eloquent ORM)中的相关行
- 如何减去X天从一个日期对象在Java?
- 在取消设置元素后重新设置数组键
- 如何修剪空白的数组值在php
- PHP中的双not(!!)操作符
- 如何在Python中从毫秒创建datetime ?
- 在PHP5中创建单例设计模式
- MySQL中两个日期之间的差异
- Sql Server字符串到日期的转换
- 阻止人们入侵基于php的Flash游戏高分表的最佳方法是什么