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
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
Short answer
Read below for the long answer.
The mimicry of the MySQL NOW() function in PHP
Here is a list of ways in PHP that mimic the MySQL
NOW()
function.I think that
date_create()->format('Y-m-d H:i:s')
is the best way because this approach allows you to handle time/time-zone manipulations easier thandate('Y-m-d H:i:s')
and it works since php 5.2.MySQL NOW() function
The MySQL function
NOW()
gives the dateTime value in this format:'YYYY-MM-DD HH:MM:SS'
. See here: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_now.An interesting fact is that it's possible to get the datetime format by running this query:
SHOW VARIABLES LIKE 'd%e_format'
, the result could be something like this:The variables up here are read-only variables. So you can't change it.
I guess the MySQL
NOW()
function gets it's format from thedatetime_format
variable.date_create()->format() VS date()
The favorable facts of
date_create('now')->format('Y-m-d H:i:s')
overdate('Y-m-d H:i:s')
are:easier to handle time manipulations
date_create()
accepts a relative date/time format (likenow
,yesterday
or+1 day
) see this link, example:date()
accepts a relative date/time format as well, like this:easier to handle timezones
When timezones matter then the usage of
date_create()->format()
makes a lot more sense thendate()
becausedate()
uses the default time zone which is configured inphp.ini
at thedate.timezone
directive. Link: http://php.net/manual/en/datetime.configuration.php#ini.date.timezone .It is possible to change the timezone during run-time. Example:
date_default_timezone_set('Asia/Tokyo');
.The downside of that is that it will affect all date/time functions. This problem doesn't exists if you are using
date_create()->format()
in combination withtimezone_open()
.PHP supports major timezones. The funny thing is that it even supports the Arctic circle, and Antarctica. Have you ever heard about
Longyearbyen
? If not, then don't worry, neither did I until I read the official PHP documentation.See a list of all supported timezones: http://php.net/manual/en/timezones.php.
o.o.p.
O.O.P. uses state-full Objects. So I prefer to think in this way:
Then to think in this way:
Therefore I would say that the
date_create()->format()
approach is more readable to me thendate()
.date_create() VS new DateTime()
The favorable facts of
date_create()
overnew DateTime()
are:Namespaces
If you work in a namespace and want to initialise a DateTime object with the new keyword, then you have to do it like this:
There is nothing wrong with this, but the downside of the above is that people forget sporadically about the backslash. By using the
date_create()
constructor function you don't have to worry about namespaces.Example of date_create()->format()
I use this approach for my projects if I have to fill an array. Like this:
There is no built-in PHP
now()
function, but you can do it usingdate()
.Example
You can use
date_default_timezone_set()
if you need to change timezone.Otherwise you can make use of Carbon - A simple PHP API extension for DateTime.
You can use simplePHP class to do this:
This class also provides many useful methods for date addition, subtraction and comparison. You can check the tutorials page for more examples.
The PHP equivalent is
time()
: http://php.net/manual/en/function.time.phpI was looking for the same answer, and I have come up with this solution for PHP 5.3 or later:
I like the solution posted by user1786647, and I've updated it a little to change the timezone to a function argument and add optional support for passing either a Unix time or datetime string to use for the returned datestamp.
It also includes a fallback for "setTimestamp" for users running version lower than PHP 5.3: