是否有一种PHP函数可以像MySQL函数NOW()一样返回日期和时间,以相同的格式?
我知道可以使用date()来完成,但我想知道是否有专门用于此目的的函数。
例如,返回:
2009-12-01 00:00:00
或者你可以使用DateTime
常量:
echo date(DateTime::W3C); // 2005-08-15T15:52:01+00:00
ATOM = "Y-m-d\TH:i:sP" ; // -> 2005-08-15T15:52:01+00:00
COOKIE = "l, d-M-Y H:i:s T" ; // -> Monday, 15-Aug-2005 15:52:01 UTC
ISO8601 = "Y-m-d\TH:i:sO" ; // -> 2005-08-15T15:52:01+0000
RFC822 = "D, d M y H:i:s O" ; // -> Mon, 15 Aug 05 15:52:01 +0000
RFC850 = "l, d-M-y H:i:s T" ; // -> Monday, 15-Aug-05 15:52:01 UTC
RFC1036 = "D, d M y H:i:s O" ; // -> Mon, 15 Aug 05 15:52:01 +0000
RFC1123 = "D, d M Y H:i:s O" ; // -> Mon, 15 Aug 2005 15:52:01 +0000
RFC2822 = "D, d M Y H:i:s O" ; // -> Mon, 15 Aug 2005 15:52:01 +0000
RFC3339 = "Y-m-d\TH:i:sP" ; // -> 2005-08-15T15:52:01+00:00 ( == ATOM)
RFC3339_EXTENDED = "Y-m-d\TH:i:s.vP" ; // -> 2005-08-15T15:52:01.000+00:00
RSS = "D, d M Y H:i:s O" ; // -> Mon, 15 Aug 2005 15:52:01 +0000
W3C = "Y-m-d\TH:i:sP" ; // -> 2005-08-15T15:52:01+00:00
为了调试,我更喜欢一个更短的网站(3v4l.org):
echo date('ymd\THisP'); // 180614T120708+02:00
function DateStamp($strDateTime = null, $strTimeZone = "Europe/London") {
$objTimeZone = new DateTimeZone($strTimeZone);
$objDateTime = new DateTime();
$objDateTime->setTimezone($objTimeZone);
if (!empty($strDateTime)) {
$fltUnixTime = (is_string($strDateTime)) ? strtotime($strDateTime) : $strDateTime;
if (method_exists($objDateTime, "setTimestamp")) {
$objDateTime->setTimestamp($fltUnixTime);
}
else {
$arrDate = getdate($fltUnixTime);
$objDateTime->setDate($arrDate['year'], $arrDate['mon'], $arrDate['mday']);
$objDateTime->setTime($arrDate['hours'], $arrDate['minutes'], $arrDate['seconds']);
}
}
return $objDateTime->format("Y-m-d H:i:s");
}
echo date("Y-m-d H:i:s");
我的答案多余,但如果你有强迫症,注重视觉效果并且 必须在代码中看到那个现在关键字,请使用:
date( 'Y-m-d H:i:s', strtotime( 'now' ) );
简要地
echo date('Y-m-d H:i:s');
现在学习PHP高级课程,包括类的额外功能,如addMinute、addYear和addHour等。
<?php /** @noinspection PhpUnhandledExceptionInspection */
/**
* Class Now
* @author dılo sürücü <berxudar@gmail.com>
*/
class Now
{
/**
* @var DateTime
*/
private $dateTime;
/**
* Now constructor.
* @throws Exception
*/
public function __construct()
{
$this->dateTime = new DateTime('now');
}
/**
* @param int $year
* @return Now
* @throws Exception
* @noinspection PhpUnused
*/
public function addYear(int $year): self
{
$this->dateTime->add(new DateInterval('P' . $year . 'Y'));
return $this;
}
/**
* @noinspection PhpUnused
* @param int $month
* @return Now
* @throws Exception
* @noinspection PhpUnused
*/
public function addMonth(int $month):self
{
$this->dateTime->add(new DateInterval('P' . $month . 'M'));
return $this;
}
/**
* @param int $day
* @return $this
* @throws Exception
*/
public function addDay(int $day): self
{
$this->dateTime->add(new DateInterval('P' . $day . 'D'));
return $this;
}
/**
* @noinspection PhpUnused
* @param int $week
* @return $this
* @throws Exception
*/
public function addWeek(int $week): self
{
return $this->addDay($week * 7);
}
/**
* @noinspection PhpUnused
* @param int $second
* @return $this
* @throws Exception
*/
public function addSecond(int $second): self
{
$this->dateTime->add(new DateInterval('PT' . $second . 'S'));
return $this;
}
/**
* @param int $minute
* @return $this
* @throws Exception
*/
public function addMinute(int $minute): self
{
$this->dateTime->add(new DateInterval('PT' . $minute . 'M'));
return $this;
}
/**
* @param int $hour
* @return $this
* @throws Exception
*/
public function addHour(int $hour): self
{
$this->dateTime->add(new DateInterval('PT' . $hour . 'H'));
return $this;
}
/**
* @return string
*/
public function get(): string
{
return $this->dateTime->format('Y-m-d H:i:s');
}
/**
* @return string
*/
public function __toString()
{
return $this->get();
}
}
/**
* @return Now
* @throws Exception
*/
function now()
{
return new Now();
}
echo now(); //2020-03-10 22:10
echo now()->addDay(1); //2020-03-11 22:10
echo now()->addDay(1)->addHour(1); // //2020-03-11 23:10
echo now()->addDay(1)->addHour(1)->addMinute(30); // //2020-03-11 23:40
echo now()->addDay(1)->addHour(1)->addMinute(30)->addSecond(10); // //2020-03-11 23:50
//or u can use get method for example
echo now()->addDay(1)->addHour(1)->addMinute(30)->get(); // //2020-03-11 23:40
PHP没有内置的now()
函数,但是你可以使用date()
来实现。
示例
function now() {
return date('Y-m-d H:i:s');
}
如果您需要更改时区,可以使用date_default_timezone_set()
。
否则,您可以使用Carbon——一个简单的PHP API扩展来操作日期和时间。
你可能会觉得这很有帮助
new \DateTime()
如果您想获取包括上午/下午的当前时间
<?php
$time_now = date("Y-m-d h:i:s a");
echo $time_now;
?>
它输出 2020年5月1日 下午5:45:28
或者
<?php
$time_now = date("Y-m-d h:i:s A");
echo $time_now;
?>
它输出 2020-05-01 05:45:28 PM
PHP的等效函数是time()
:http://php.net/manual/en/function.time.php
time()
返回自 Unix 纪元(1970年1月1日00:00:00 GMT)以来经过的秒数,表示当前时间。 - Daniel R.
DateTimeInterface
,而不是DateTime
。 - Anton DuzenkoDateTimeInterface:W3C
,只有从v^8.0.1 (在线评估)开始支持,而DateTime::W3C
则支持v8.0.0以下版本直到v5.4.0 (评估). - CPHPythonDateTimeInterface:W3C
会触发致命错误:“未定义类常量'W3C”...由于DateTime::W3C
不会被弃用,与多个站点一起工作的开发人员可能只需使用它。 - CPHPython