What is now function in php?

Short answer

$now = date_create()->format('Y-m-d H:i:s');

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.

// relative date
$now = date_create('now')->format('Y-m-d H:i:s'); // works in php 5.2 and higher  
$now = date_create()->format('Y-m-d H:i:s'); // also works in php 5.2
$now = new DateTime('now')->format('Y-m-d H:i:s'); // syntax error!!!
$now = (new DateTime('now'))->format('Y-m-d H:i:s'); // works in php 5.4 and higher   
$now = date('Y-m-d H:i:s'); // Slightly higher performance, but less usable for date/time manipulations

// From Unix timestamp
// Using date_create() with a Unix timestamp will give you a FALSE,  
// and if you try to invoke format() on a FALSE then you'll get a: 
//     Fatal error: Call to a member function format() on boolean 
// So if you work with Unix timestamps then you could use: date_create_from_format().
$unixTimeStamp = 1420070400; // 01/01/2015 00:00:00
$y2015 = date_create_from_format('U', $unixTimeStamp, timezone_open('Europe/Amsterdam'))->format('Y-m-d H:i:s');
$y2015 = date('Y-m-d H:i:s', $unixTimeStamp);

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 than date('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:

Variable_name     Value     
date_format       %Y-%m-%d
datetime_format   %Y-%m-%d %H:%i:%s

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 the datetime_format variable.




Advantages of using date_create()->format() instead date() summary

The favorable facts of date_create('now')->format('Y-m-d H:i:s') over date('Y-m-d H:i:s') are:

  • O.O.P.
  • easier to handle time manipulations
  • easier to handle timezones

Disadvanteages of using date_create()->format() instead date() summary

  • date() performs slightly better



Advantages of using date_create()->format() instead date() detailed

Read on for the detailed explanation.

O.O.P.

The function date_create() is a constructor function for the DateTime object. The documentation of that Object gives the programmer a quick view that describes the possibilities. Documentation: https://www.php.net/manual/en/class.datetime.php

The function date() just gives you a string.

My taste for usability lies in O.O.P.

easier to handle time manipulations

date_create() accepts a relative date/time format (like now, yesterday or +1 day) see this link, example:

$tomorrow = date_create('+1 day')->format('Y-m-d H:i:s'); 

date() accepts a relative date/time format as well, like this:

$tomorrow = date('Y-m-d H:i:s', strtotime('+1 day'));
$tomorrow = date('Y-m-d H:i:s', (time() + 86400)); // 86400 seconds = 1 day

easier to handle timezones

When timezones matter then the usage of date_create()->format() makes a lot more sense then date() because date() uses the default time zone which is configured in php.ini at the date.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 with timezone_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. Does it matter? Well... yes if you run an international e-commerce platform and do something bound to local time, for example: calculate delivery date-time.

$nowLongyearbyen = date_create('now', timezone_open('Arctic/Longyearbyen'))->format('Y-m-d H:i:s');

See a list of all supported timezones: http://php.net/manual/en/timezones.php.

date_create() VS new DateTime()

The favorable facts of date_create() over new DateTime() are:

  • Namespaces

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:

namespace my_namespace;

// The backslash must be used if you are in a namespace.
// Forgetting about the backslash results in a fatal error.
$dt = new \DateTime();

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.

$dt = date_create(); // in or not in a namespace it works in both situations



Disadvantages of using date_create()->format() instead date()

The function date() has a slightly better performance than date_create()->format(). However, it's only visible if you would instantiate DateTime millions of times. Which might never happen in a real-life situation. However, here is the benchmark test:

$start = time();
for ($i = 0; $i <= 5000000; $i++) {
    $a = date_create('now')->format('Y-m-d H:i:s');
}
$end = time();                  
$elapsedTimeA = $end - $start;
 
echo 'Case date_create(), elapsed time in seconds: ' . $elapsedTimeA;
echo '<br>';
 
$start = time();
for ($i = 0; $i <= 5000000; $i++) {
    $b = date('Y-m-d H:i:s');
}
$end = time();                   
$elapsedTimeB = $end - $start;
 
echo 'Case date(), elapsed time in seconds: ' . $elapsedTimeB;
echo '<br>';
// OUTPUT
Case date_create(), elapsed time in seconds: 31
Case date(), elapsed time in seconds: 14



Examples of date_create()->format()

I use this approach for my projects if I have to fill an array. Like this:

$array = array(
    'name' => 'John',
    'date_time' => date_create('now')->format('Y-m-d H:i:s'), // uses the default timezone
    'date_time_japan' => date_create('now', timezone_open('Asia/Tokyo'))->format('Y-m-d H:i:s'),
);

What is the now () function in JavaScript?

The NOW() function returns the current date and time. Note:The date and time is returned as "YYYY-MM-DD HH-MM-SS" (string) or as YYYYMMDDHHMMSS.uuuuuu (numeric). Syntax NOW()

What is the MySQL now () function?

The MySQL NOW() function returns the current date and time in the configured time zone as a string or a number in the 'YYYY-MM-DD HH:MM:DD' or 'YYYYMMDDHHMMSS.uuuuuu' format. The returned type of the NOW() function depends on the context where it is used.

What is the return type of now () function?

The NOW() function returns the current date and time. Note:The date and time is returned as "YYYY-MM-DD HH-MM-SS" (string) or as YYYYMMDDHHMMSS.uuuuuu (numeric).

What is the difference between timetime() and now() in PHP?

time () is the PHP function that corresponds to the MySQL method now (). The date and time function has replaced the now () method in modern programming. Date and time are also built-in functions, with the date function used to format the date and time according to the user’s preferences.

What is now () in MySQL?

The NOW() function returns the current date and time. Note: The date and time is returned as "YYYY-MM-DD HH-MM-SS" (string) or as YYYYMMDDHHMMSS. uuuuuu (numeric).

What is a timestamp in PHP?

PHP provides several date-time functions to perform required operations with temporal data. Now, we are going to see about PHP timestamp functions. The timestamp is the value represented as seconds calculated, since UNIX Epoch, January 1, 1970, and also called as UNIX timestamp.

How can I get current timestamp in PHP?

The time() function returns the current time in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

What is Unix timestamp in PHP?

Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). Note: Unix timestamps do not contain any information with regards to any local timezone.