Home > Article > Backend Development > PHP now
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 TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
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,
Syntax of using the now() function to get the time in PHP is given below:
time();
Where,
Some of the important points related to the working of now() function in PHP are given below:
Example:
Code:
<?PHP mktime(hr, min, sec, mon, day, yr, is_dst); ?>
Some of the commonly used Time parameters of now() function used while displaying the time are given below:
|
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]. |
Given below are the examples of PHP now:
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:
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:
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:
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:
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!