是否有一个PHP函数,返回日期和时间在相同的格式MySQL函数NOW()?
我知道如何使用date()来做到这一点,但我在问是否有一个函数仅用于此。
例如,返回:
2009-12-01 00:00:00
是否有一个PHP函数,返回日期和时间在相同的格式MySQL函数NOW()?
我知道如何使用date()来做到这一点,但我在问是否有一个函数仅用于此。
例如,返回:
2009-12-01 00:00:00
当前回答
MySQL函数NOW()返回当前时间戳。我发现PHP的唯一方法是使用下面的代码。
$curr_timestamp = date('Y-m-d H:i:s');
其他回答
我喜欢user1786647发布的解决方案,我对它进行了一些更新,将timezone更改为一个函数参数,并添加了可选的支持,可以传递Unix时间或datetime字符串来用于返回的日期戳。
它还为运行PHP 5.3以下版本的用户提供了"setTimestamp"的回退:
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");
}
你可以使用date函数:
date("Y-m-d H:i:s");
不久
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 5.3或更高版本:
$dtz = new DateTimeZone("Europe/Madrid"); //Your timezone
$now = new DateTime(date("Y-m-d"), $dtz);
echo $now->format("Y-m-d H:i:s");
或者你可以使用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