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


当前回答

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

其他回答

下面是一些常用的表示时间的字符:

H -以前导零表示的12小时格式(01到12) i -前导0的分钟(00到59) s -前导0的秒(00到59) a -小写的子午线前后(am或pm)

确定你的时区

<?php
    date_default_timezone_set("America/New_York");
    echo "The time is " . date("h:i:sa");
?>

看看这个(可选)

<?php
    $d = mktime(11, 14, 54, 8, 12, 2014);
    echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>

为日期

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

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

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

Source-W3-Schools

我发现在PHP中获取当前时间的最简单方法是这样的。

//Prints out something like 10:00am Just be sure to set your timezone correctly.
date_default_timezone_set("America/Chicago");
$TIME = date('G:ia'); 

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

$day = date("d");
$month = date("m");
$year = date("y");
print "date" . $day . "-" . $month . "-" . $year;
<?php
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone

$today = date("F j, Y, g:i a");                 // March 10, 2001, 5:16 pm
$today = date("m.d.y");                         // 03.10.01
$today = date("j, n, Y");                       // 10, 3, 2001
$today = date("Ymd");                           // 20010310
$today = date('h-i-s, j-m-y, it is w Day');     // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.');   // it is the 10th day.
$today = date("D M j G:i:s T Y");               // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h');     // 17:03:18 m is month
$today = date("H:i:s");                         // 17:16:18
$today = date("Y-m-d H:i:s");                   // 2001-03-10 17:16:18 (the MySQL DATETIME format)
?>

参考:这里有一个链接

由于使用夏令时,这比简单地在时间戳中添加或减去一天或一个月中的秒数更可靠。

PHP代码

// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone

$today = date("F j, Y, g:i a");                 // March 10, 2001, 5:16 pm
$today = date("m.d.y");                         // 03.10.01
$today = date("j, n, Y");                       // 10, 3, 2001
$today = date("Ymd");                           // 20010310
$today = date('h-i-s, j-m-y, it is w Day');     // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.');   // it is the 10th day.
$today = date("D M j G:i:s T Y");               // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h');     // 17:03:18 m is month
$today = date("H:i:s");                         // 17:16:18
$today = date("Y-m-d H:i:s");                   // 2001-03-10 17:16:18 (the MySQL DATETIME format)