在 PHP 中,now() 函数用于获取当前日期和时间。如今,使用日期和时间函数代替 now() 函数。日期和时间都是内置函数,其中日期函数用于根据用户的具体要求格式化日期和时间。它返回以特定格式显示日期的字符串。 time 函数将当前时间返回给用户,该时间以 Unix 时间戳的秒数来衡量。使用 time() 函数(相当于 now() 函数)时,它返回的值取决于默认时区。
广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
as now() 函数用于根据用户的要求检索日期和时间。
下面给出了 PHP 中 now() 函数获取日期的基本语法:
date(format, [ts]);
哪里,
在 PHP 中使用 now() 函数获取时间的语法如下:
time();
哪里,
与 PHP 中 now() 函数工作相关的一些要点如下:
示例:
代码:
<?PHP mktime(hr, min, sec, mon, day, yr, is_dst); ?>
显示时间时使用的now()函数的一些常用时间参数如下:
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]. |
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.
以上是现在 PHP的详细内容。更多信息请关注PHP中文网其他相关文章!