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

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

例如,返回:

2009-12-01 00:00:00

当前回答

不久

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中,与MySQL的now()函数等价的逻辑是time()。

但是time()返回的Unix时间戳与MySQL DATETIME不同。

因此,必须将time()返回的Unix时间戳转换为MySQL格式。

你可以这样做: 日期(“Y-m-d H:我:”);

但是date()函数中的time()在哪里呢?它是第二个参数:事实上,你应该为date()提供一个时间戳作为第二个参数,但如果省略它,它将默认为time()。

这是我能想到的最完整的答案。

问候。

你可以使用date函数:

date("Y-m-d H:i:s");
date('Y-m-d H:i:s')

更多详情请点击这里:http://pl.php.net/manual/en/function.date.php

我喜欢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");
}

不久

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