Home  >  Article  >  Backend Development  >  PHP now

PHP now

王林
王林Original
2024-08-29 12:59:471275browse

In PHP, now() function is used to get the current date and time. Now-a-days, the date and time function is used in place of the now() function. Both the date and time are the inbuilt functions in which the date function is used to format the date and time according to the user’s specific requirements. It returns the string displaying the date in a particular format. The time function returns the current time to the user, which is measured in the number of seconds as a Unix timestamp. On using the time() function, which is equivalent to the now() function, the value returned by it depends on the default timezone.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax of PHP now

As now() function is used to retrieve the date and time according to the requirements of the user.

Below given is the basic syntax of the now() function to get the date in PHP:

date(format, [ts]);

Where,

  • format: It defines the format in which we want to display our date in the output according to specific requirements. For example, ‘Y’ is used to display the current year. ‘Y-M-D’ is used to display the date in the ‘YYYY-MM-DD’ format in PHP.
  • ts: This parameter is optional. It specifies the current timestamp. If the user does not specify the timestamp, PHP will use the current date-time on the server.

Syntax of using the now() function to get the time in PHP is given below:

time();

Where,

  • time() function returns the current time in seconds. It accepts no parameter.

How does now() Function work in PHP?

Some of the important points related to the working of now() function in PHP are given below:

  • The default timezone in PHP is set in the PHP.ini file.
  • We can also set the timezone programmatically in PHP. In order to set it, the date_default_timezone_set function is to be used.
  • We can get the list of all the available time zones using the listIdentifiers() function, which is a static method of a built-in DateTimeZone class.
  • In PHP, mktime() is a function which is used to return the timestamp to the user in Unix format.

Example:

Code:

<?PHP
mktime(hr, min, sec, mon, day, yr, is_dst);
?>
  • All the date functions in now() used in PHP use the default timezone.
  • The number of seconds between the current time and 1st Jan 1970 00:00:00 GMT is the timestamp. This is also called a UNIX timestamp.
  • Although the programmer has the function available in PHP to set the timezone through the PHP code.

Some of the commonly used Time parameters of now() function used while displaying the time are given below:

Sr.No Parameter Description
1 H It represents the 24-hour format of an hour. It represents the hours between [00-23].
2 h It represents the 12-hour format of an hour. It represents hours between [1-12] with leading zeros.
3 s It represents the seconds with leading zeros. It represents the seconds between [00-59].
4 i It represents the minutes with leading zeros. It represents data between [00-59].
5 A, ‘a’ It represents whether the time displayed is in am or pm (Ante meridiem and Post meridiem).
6 r It is used to return the full date and time value.
Sr.No
Parameter Description
1 H It represents the 24-hour format of an hour. It represents the hours between [00-23].
2 h It represents the 12-hour format of an hour. It represents hours between [1-12] with leading zeros.
3 s It represents the seconds with leading zeros. It represents the seconds between [00-59].
4 i It represents the minutes with leading zeros. It represents data between [00-59].
5 A, ‘a’ It represents whether the time displayed is in am or pm (Ante meridiem and Post meridiem).
6 r It is used to return the full date and time value.

Some of the commonly used Date parameters of now() function in the date are given below:

Sr.No Parameter Description
1 d It represents the day number in the month. The value ranges of this parameter are [01-31].
2 D It represents the day in a week. It displays the first 3 letters of the day. The value range of this parameter is [Sun- Sat].
3 z It is used to represent the day in a year. Its value ranges from [0-365].
4 l It is used to represent the weekday name. It displays the values from Sunday to Saturday.

Some of the commonly used Year parameters of now() function in the date are given below:

Sr.No Parameter Description
1 Y It represents the full 4 digits year format. The year displayed will be ‘YYYY’.
2 y It represents the 2 digits year format. The year displayed will be ‘YY’. Values range from [00-99].
3 L It represents whether the year is a leap year or not. If the year is a leap year, it returns 1. It returns 0 if the year is not a leap year and -1 if the year is unknown.

Some of the commonly used Month parameters of now() function in the date are given below:

Sr.No Parameter Description
1 M It represents the name of the month. Starting first 3 letters of the month name are displayed. The value ranges from [Jan-Dec].
2 m It represents the month number in numerical format. The value ranges from [01-12].
3 t It is used to represent the number of days in a given month. Its value ranges from [28-31] depending on the month and year given.
4 F It represents the full name of the month. The value ranges from [JANUARY -DECEMBER].

Examples of PHP now

Given below are the examples of PHP now:

Example #1

Code:

<!DOCTYPE html>
<html>
<body>
<?PHP
echo "Today's date is ".date("r"). "<br>";
echo "Formatted is ".date('y-m-d')."<br>";
echo " Format 2 is ".date('Y/M/D')."<br>";
echo "Day number according to year is ".date("z");
?>
</body>
</html>

Output:

PHP now

Example #2

Code:

<!DOCTYPE html>
<html>
<body>
<?PHP
echo "Current time in 24 hr format : " . date("H: i: s"). "<br>";
echo "Current time in 24 hr format : " . date("h -i - s"). "<br>";
echo "Check am or pm : " . date("a"). "<br>";
?>
</body>
</html>

Output:

PHP now

Example #3

Code:

<!DOCTYPE html>
<html>
<body>
<?PHP
echo "Default time <br> ";
echo "Time is : ".date("H: i: s"). "<br>";
echo "Now Setting the timezone <br>" ;
date_default_timezone_set("Asia/Calcutta");
echo "The time is " .date("H:i:s"). "<br>";
?>
</body>
</html>

Output:

PHP now

Example #4

Code:

<!DOCTYPE html>
<html>
<body>
<?PHP
$day = mktime(34, 24, 4, 8, 2, 2020);
echo "Created date when hours exceed is:  " .date("Y-m-d H:i:s", $day). "<br>";
$day2 = mktime(7, 67, 65, 8, 2, 2020);
echo "Created date when minutes and seconds exceed is " .date("Y-m-d H:i:s", $day2). "<br>";
?>
</body>
</html>

Output:

PHP now

Conclusion

The above description clearly shows what is now() function in PHP and how they work in order to retrieve the current date and time in the format given by the user. The date and time function in PHP works similar to the now() function in MySQL. PHP date and time functions also throw E_WARNING and E_NOTICE to check the correct timezone or use system variables. So before using them in the program, the programmer needs to understand them well.

The above is the detailed content of PHP now. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Inheritance in PHPNext article:Inheritance in PHP