NOW() function 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. It returns the string representing the date in the specified format.

There is no built-in PHP now() function, but you can do it using date().

The time function returns the current time, which is measured in seconds as a Unix timestamp, to the user. The result returned by the time() function, which is identical to the now() method, depends on the default timezone.

However, the time() function returns a Unix timestamp that differs from a MySQL DATETIME. Therefore, it is important to convert the Unix timestamp returned by time() to the MySQL format.

Syntax of PHP now() function

The now() function is used to retrieve the date and time based on the user’s specifications.

The syntax for the now() method to obtain the date in PHP is as follows:

date(format, [ts]);

format: It specifies the format in which we wish to show the date based on certain specifications. For instance, the letter ‘Y’ is used to indicate the current year.

ts: This parameter is not required. It indicates the current date and time. If the user does not provide a timestamp, PHP will use the server’s current date and time.

PHP syntax for utilising the now() function to obtain the current time is as follows:

time();

How does PHP now() work?

Listed below are some of the most relevant details regarding the operation of the now() function in PHP:
The PHP.ini file contains the setting for the default timezone.

PHP also supports setting the timezone programmatically. The date default timezone set function is used to set the default time zone.

Using the listIdentifiers() function, which is a static method of the DateTimeZone class, we can retrieve a list of all the accessible time zones.

PHP’s mktime() method is used to return the timestamp in Unix format to the user.

mktime(hr, min, sec, mon, day, yr, is_dst);

mktime(…)” is the make PHP timestamp function
second” is valid; it is the number of seconds
minute” is optional; it is the number of minutes
hour” is optional; it is the number of hours

month” is optional; it is the number of the month
day” is optional; it is the number of the day
year” is okay; it is the number of the year
is dst” is optional; it is used to calculate the day saving time (DST). 1 indicates Daylight Savings Time, 0 does not, and -1 indicates an unknown value.

All date functions in PHP’s now() function utilise the default timezone.

function now() {
return date(‘Y-m-d H:i:s’);
}

date default timezone set() can be used to adjust the timezone.

The timestamp is the number of seconds between the current time and 1st Jan 1970 00:00:00 GMT. This is referred to as a UNIX timestamp.

Although PHP provides the programmer with the ability to set the time zone using PHP code, it is not commonly used.

Some of the most common Time arguments used by the now() method when showing the time are shown below.

// Sets the default time zone to use. Available since PHP 5.1

date_default_timezone_set('UTC');


// Showing something like : Monday
echo date("l");

// Showing something like : Monday 8th of August 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A');

// Attach: July 1, 2001 is on a Sunday
echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));

/* use the constants in the format parameter */
// Wed, 26 july 2015 14:28:57 -0700
echo date(DATE_RFC2822);

// use the constants in the format parameter : 2004-07-01T00:00:00+00:00
echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2004));

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

Conclusion

This section describes the now() method in PHP and how it works to retrieve the current date and time in the format specified by the user. PHP’s date and time function operates similarly to MySQL’s now() function. In addition to E WARNING and E NOTICE, PHP date and time functions additionally return E WARNING and E NOTICE when checking the correct timezone or utilising system variables. All PHP date() methods utilise the default time zone specified in the php.ini file. Using PHP scripts, the default time zone may also be set programmatically.

Please visit alsoIntroduction to Date and Time in PHP

Add a Comment

Your email address will not be published. Required fields are marked *

ABOUT CODINGACE

My name is Nohman Habib and I am a web developer with over 10 years of experience, programming in Joomla, Wordpress, WHMCS, vTiger and Hybrid Apps. My plan to start codingace.com is to share my experience and expertise with others. Here my basic area of focus is to post tutorials primarily on Joomla development, HTML5, CSS3 and PHP.

Nohman Habib

CEO: codingace.com

Request a Quote