NOW() function in PHP

2020-01-25 12:22发布

Is there a PHP function that returns the date and time in the same format as the MySQL function NOW()?

I know how to do it using date(), but I am asking if there is a function only for this.

For example, to return:

2009-12-01 00:00:00

16条回答
forever°为你锁心
2楼-- · 2020-01-25 12:36

Not besides:

date("Y-m-d H:i:s");
查看更多
霸刀☆藐视天下
3楼-- · 2020-01-25 12:36

With PHP version >= 5.4 DateTime can do this:-

echo (new \DateTime())->format('Y-m-d H:i:s');

See it working.

查看更多
我只想做你的唯一
4楼-- · 2020-01-25 12:38

Or you can use DateTime constants:

echo date(DateTime::W3C); // 2005-08-15T15:52:01+00:00

Here's the list of them:

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

For debugging I prefer a shorter one though (3v4l.org):

echo date('ymd\THisP'); // 180614T120708+02:00
查看更多
走好不送
5楼-- · 2020-01-25 12:41

Use this function:

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');
}
查看更多
女痞
6楼-- · 2020-01-25 12:41

One more answer I find easy to use:

echo date('c');

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

This is ISO 8601 date (added in PHP 5) which MySQL uses

Edit

MySQL 5.7 do not allow timezone in the datetime by default. You can disable the error with SQL_MODE=ALLOW_INVALID_DATES. See the answer here for more details: https://stackoverflow.com/a/35944059/2103434. But that also means that the timezone will be lost when saving to the database!

By default MySQL uses the system's timezone, and as long as PHP uses the same timezone you should be okay. In my case CET / UTC+2.

That means that if I insert 2015-07-27T00:00:00+02:00 to the database, only 2015-07-27T00:00:00 will be stored (but that is the correct local time!).

When I load the time back in to PHP,

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

it will automatically assume it's +02:00 timezone since it's the default. Printing this will be correct again:

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

To be safe, always use UTC on the server, specify it in MySQL and PHP, and then only convert it to your user's locale when displaying the date:

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
查看更多
Ridiculous、
7楼-- · 2020-01-25 12:43
date('Y-m-d H:i:s')

Look here for more details: http://pl.php.net/manual/en/function.date.php

查看更多
登录 后发表回答