哪个PHP函数可以返回当前日期/时间?


当前回答

// 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.

有关时区参考,请参见支持的时区列表。

其他回答

日期格式也取决于:

echo date("d/m/Y H:i:sa"); // 13/04/2017 19:38:15pm

PHP的time()返回当前Unix时间戳。这样,您就可以使用date()函数将其格式化为您需要的格式。

$date = date('Format String', time());

正如Paolo在评论中提到的,第二个论点是多余的。下面的代码段相当于上面的代码段:

$date = date('Format String');

你也可以使用这种格式:

$date = date("d-m-Y");

Or

$date = date("Y-m-d H:i:s");

Use:

$date = date('m/d/Y h:i:s a', time());

它的工作原理。

如果你是孟加拉国人,如果你想知道达卡的时间,那么用这个:

$date = new DateTime();
$date->setTimeZone(new DateTimeZone("Asia/Dhaka"));
$get_datetime = $date->format('d.m.Y H:i:s');