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


当前回答

设置时区:

date_default_timezone_set('Asia/Calcutta');

然后调用日期函数

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

其他回答

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

$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");

Use:

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

它的工作原理。

通常,date的这个函数对每个人都有用:date("Y/m/d");

但是时间是不同的,因为时间函数取决于PHP版本或系统日期。

所以可以像这样使用它来获得我们自己的时区:

$date = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
echo $date->format('H:m:s');

这个函数显示24小时时间。

如果你想获得日期,如12-3-2016,分开每一天,月和年的值,然后复制粘贴以下代码:

$day = date("d");
$month = date("m");
$year = date("y");
print "date" . $day . "-" . $month . "-" . $year;
// 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.

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