在 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中文網其他相關文章!