是否有一个PHP函数,返回日期和时间在相同的格式MySQL函数NOW()?
我知道如何使用date()来做到这一点,但我在问是否有一个函数仅用于此。
例如,返回:
2009-12-01 00:00:00
是否有一个PHP函数,返回日期和时间在相同的格式MySQL函数NOW()?
我知道如何使用date()来做到这一点,但我在问是否有一个函数仅用于此。
例如,返回:
2009-12-01 00:00:00
当前回答
你可能会发现这很有用
new \DateTime()
其他回答
试试这个:
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 now()函数,但可以使用date()来实现。
例子
function now() {
return date('Y-m-d H:i:s');
}
如果需要更改时区,可以使用date_default_timezone_set()。
否则,您可以使用Carbon -一个简单的PHP API扩展DateTime。
在PHP中,与MySQL的now()函数等价的逻辑是time()。
但是time()返回的Unix时间戳与MySQL DATETIME不同。
因此,必须将time()返回的Unix时间戳转换为MySQL格式。
你可以这样做: 日期(“Y-m-d H:我:”);
但是date()函数中的time()在哪里呢?它是第二个参数:事实上,你应该为date()提供一个时间戳作为第二个参数,但如果省略它,它将默认为time()。
这是我能想到的最完整的答案。
问候。
我的回答是多余的,但如果你是强迫症,视觉导向,你只需要在你的代码中看到now关键字,使用:
date( 'Y-m-d H:i:s', strtotime( 'now' ) );