哪个PHP函数可以返回当前日期/时间?
当前回答
PHP的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)
其他回答
// 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获取当前日期时间(NOW)这篇文章,有两种常见的方法来获取当前日期。要在PHP中获取当前的日期时间(now),您可以在任何PHP版本中使用date类,或者在PHP >= 5.2中使用datetime类。
这里有各种日期格式表达式。
使用日期的示例
该表达式将以Y-m-d H:i:s格式返回NOW。
<?php
echo date('Y-m-d H:i:s');
?>
使用datetime类的示例
该表达式将以Y-m-d H:i:s格式返回NOW。
<?php
$dt = new DateTime();
echo $dt->format('Y-m-d H:i:s');
?>
对于新的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'
你可以在这里了解更多
希望这对你有所帮助,如果你知道任何其他获取日期和时间的方法,请随意编辑答案。
获取当前时间和日期的最佳方法是使用PHP中的date函数:
$date = date('FORMAT'); // FORMAT E.g.: Y-m-d H:i:s
$current_date = date('Y-m-d H:i:s');
使用Unix时间戳:
$now_date = date('FORMAT', time()); // FORMAT Eg : Y-m-d H:i:s
设置服务器时区。
date_default_timezone_set('Asia/Calcutta');
这里有一个不同的时区列表。
echo date('y-m-d'); // Today
这将返回今天的日期。
推荐文章
- 如何实现一个好的脏话过滤器?
- PHP中的三个点(…)是什么意思?
- Guzzlehttp -如何从guzzle6得到响应的正文?
- 移动一个文件到服务器上的另一个文件夹
- Laravel中使用雄辩的ORM进行批量插入
- PHP 5.4调用时引用传递-容易修复可用吗?
- 格式化字节到千字节,兆字节,千兆字节
- 如何在PowerShell格式化日期时间
- 如何在PHP中获得变量名作为字符串?
- 在c#中解析字符串为日期时间
- 用“+”(数组联合运算符)合并两个数组如何工作?
- Laravel PHP命令未找到
- 如何修复从源代码安装PHP时未发现xml2-config的错误?
- 如何在Python中获取UTC时间?
- 在PHP中对动态变量名使用大括号