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


当前回答

您可以简单地使用这段代码来获取当前日期和时间

echo date('r', 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.

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

你也可以使用这种格式:

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

Or

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

非常简单 echo $date = date('Y-m-d H:i:s');

Use:

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

它的工作原理。

如果您想要不同的时间刻度,请使用:

$tomorrow  = mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"));
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"),   date("Y"));
$nextyear  = mktime(0, 0, 0, date("m"),   date("d"),   date("Y")+1);

date_default_timezone_set("Asia/Calcutta");
echo date("Y/m/d H:i:s");