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

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

例如,返回:

2009-12-01 00:00:00

当前回答

试试这个:

date("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

使用这个函数:

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');
}

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

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

不久

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的等效函数是time(): http://php.net/manual/en/function.time.php