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


当前回答

<?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中获取当前时间的最简单方法是这样的。

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

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

您可以使用$_SERVER['REQUEST_TIME']变量(自PHP 5.1.0起可用)或time()函数来获取当前的Unix时间戳。

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

另一种简单的方法是获取当前日期和时间的时间戳。使用mktime()函数:

$now = mktime(); // Return timestamp of the current time

然后您可以将其转换为另一种日期格式:

//// Prints something like: Thursday 26th of January 2017 01:12:36 PM
echo date('l jS \of F Y h:i:s A',$now);

更多的日期格式在这里: http://php.net/manual/en/function.date.php