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


当前回答

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

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

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

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

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

其他回答

echo date("d-m-Y H:i:sa");

这段代码将获得运行该代码的服务器的日期和时间。

日期格式也取决于:

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

您的国家时区:支持的时区列表

date_default_timezone_set('Asia/Kolkata');

$dateYmd = date('Y-m-d');
echo "Current Year Month Day: $dateYmd";

本年月日:2022-01-03

$datehms = date('h:i:s');
echo "Current Hour Minute Second: $datehms";

当前小时分秒:11:05:38

// 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(“Y-m-d H:i:s”),这会给你你的日期和时间,比如“2020-08-22 12:20:30”。 在date()函数之前添加date_default_timezone_set("your timezone ")来获取你所在区域/区域的时间日期。 在这里你可以找到你所在的时区