哪个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 2016 03:12:46 PM
echo date('l jS \of F Y h:i:s A');

// Prints: July 1, 2016 is on a Saturday
echo "July 1, 2016 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2016));

/* 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: 2016-07-01T00:00:00+00:00
echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));

其他回答

我们可以使用date函数设置默认时区:

<?php
    date_default_timezone_set("Asia/Kolkata");
    echo "Today is " . date("Y/m/d") . "<br>";
    echo "Today is " . date("Y.m.d") . "<br>";
    echo "Today is " . date("Y-m-d") . "<br>";
    echo "Today is " . date("l");
    echo "The time is " . date("h:i:sa");
?>

非常简单的

date_default_timezone_set('Asia/Kolkata');
$date = date('m/d/Y H:i:s', time());

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

// 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(format, timestamp)

date函数返回一个根据给定格式字符串使用给定整数时间戳格式化的字符串,如果没有给出时间戳则返回当前时间。换句话说,timestamp是可选的,默认值为time()。

参数为-

format -必选。指定时间戳格式 timestamp -时间戳(可选)。默认为当前日期和时间

如何获得一次简单的约会

date()函数所需的format参数指定如何格式化日期(或时间)。

下面是一些常用来表示日期的字符:

d -表示一个月中的第一天(01 ~ 31) m -代表一个月(01 ~ 12) Y -年份(四位数) l(小写“l”)-代表星期几

还可以在字符之间插入其他字符,如“/”、“。”或“-”,以添加额外的格式。

下面的例子用三种不同的方式格式化今天的日期:

<?php
    echo "Today is " . date("Y/m/d") . "<br>";
    echo "Today is " . date("Y.m.d") . "<br>";
    echo "Today is " . date("Y-m-d") . "<br>";
    echo "Today is " . date("l");
?>

一些有用的链接

gmdate() -格式化a 格林尼治时间/ UTC日期/时间 idate() -格式化a 本地时间/日期为整数 getdate() -获取 日期/时间信息 getlastmod () - 获取最后一页修改的时间 mktime() -获取Unix 日期的时间戳 strftime() -格式 根据区域设置的本地时间/日期 time() -返回电流 Unix时间戳 strtotime () 解析任何英文文本日期时间描述到Unix 时间戳 预定义的DateTime 常量