是否有一个PHP函数,返回日期和时间在相同的格式MySQL函数NOW()?

我知道如何使用date()来做到这一点,但我在问是否有一个函数仅用于此。

例如,返回:

2009-12-01 00:00:00

当前回答

使用这个函数:

function getDatetimeNow() {
    $tz_object = new DateTimeZone('Brazil/East');
    //date_default_timezone_set('Brazil/East');

    $datetime = new DateTime();
    $datetime->setTimezone($tz_object);
    return $datetime->format('Y\-m\-d\ h:i:s');
}

其他回答

你可能会发现这很有用

new \DateTime()

使用这个函数:

function getDatetimeNow() {
    $tz_object = new DateTimeZone('Brazil/East');
    //date_default_timezone_set('Brazil/East');

    $datetime = new DateTime();
    $datetime->setTimezone($tz_object);
    return $datetime->format('Y\-m\-d\ h:i:s');
}

试试这个:

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

还有一个我觉得很好用的答案:

echo date('c');

// 2015-07-27T00:00:00+02:00

这是MySQL使用的ISO 8601日期(在PHP 5中添加)

Edit

MySQL 5.7默认不允许timezone在datetime中。您可以使用SQL_MODE=ALLOW_INVALID_DATES禁用该错误。详情请点击这里:https://stackoverflow.com/a/35944059/2103434。但这也意味着保存到数据库时时区将丢失!

默认情况下,MySQL使用系统的时区,只要PHP使用相同的时区就可以了。在我的例子中,CET / UTC+2。

这意味着如果我将2015-07-27T00:00:00+02:00插入到数据库中,只有2015-07-27T00:00:00将被存储(但这是正确的本地时间!)

当我把时间加载回PHP时,

$importedDate = new \DateTime('2015-07-27T00:00:00')

它会自动假设它是+02:00时区,因为它是默认的。打印这个将是正确的:

echo $importedDate->format('c');
// 2015-07-27T00:00:00+02:00

为了安全起见,总是在服务器上使用UTC,在MySQL和PHP中指定它,然后只在显示日期时将其转换为用户的语言环境:

date_default_timezone_set('UTC');
$importedDate = new \DateTime('2015-07-27T00:00:00+02:00');
echo $importedDate->format('c');
// 2015-07-27T00:00:00+02:00

$importedDate->setTimezone(new \DateTimeZone("America/New_York"));
echo $importedDate->format('c');
// 2015-07-26T18:00:00-04:00