現在 PHP

王林
發布: 2024-08-29 12:59:47
原創
1040 人瀏覽過

在 PHP 中,now() 函數用來取得目前日期和時間。如今,使用日期和時間函數來代替 now() 函數。日期和時間都是內建函數,其中日期函數用於根據使用者的特定要求格式化日期和時間。它會傳回以特定格式顯示日期的字串。 time 函數將當前時間傳回給用戶,該時間以 Unix 時間戳記的秒數來衡量。使用 time() 函數(相當於 now() 函數)時,它傳回的值取決於預設時區。

廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

現在的 PHP 語法

as now() 函數用於根據使用者的要求檢索日期和時間。

下面給出了 PHP 中 now() 函數取得日期的基本語法:

date(format, [ts]);
登入後複製

哪裡,

  • 格式:它定義了我們希望根據特定要求在輸出中顯示日期的格式。例如,“Y”用於顯示當前年份。 ‘Y-M-D’ 用於在 PHP 中以 ‘YYYY-MM-DD’ 格式顯示日期。
  • ts: 此參數是可選的。它指定當前時間戳。如果使用者未指定時間戳,PHP 將使用伺服器上的目前日期時間。

在 PHP 中使用 now() 函數取得時間的語法如下:

time();
登入後複製

哪裡,

  • time() 函數傳回目前時間(以秒為單位)。它不接受任何參數。

now() 函數在 PHP 中如何運作?

與 PHP 中 now() 函數工作相關的一些要點如下:

  • PHP 中的預設時區在 PHP.ini 檔案中設定。
  • 我們也可以在 PHP 中以程式設定時區。為了設定它,需要使用 date_default_timezone_set 函數。
  • 我們可以使用 listIdentifiers() 函數來取得所有可用時區的列表,該函數是內建 DateTimeZone 類別的靜態方法。
  • 在 PHP 中,mktime() 是一個函數,用於以 Unix 格式將時間戳記回傳給使用者。

範例:

代碼:

<?PHP
mktime(hr, min, sec, mon, day, yr, is_dst);
?>
登入後複製
  • PHP 中使用的 now() 中的所有日期函數都使用預設時區。
  • 目前時間與 1970 年 1 月 1 日 00:00:00 GMT 之間的秒數是時間戳記。這也稱為 UNIX 時間戳記。
  • 雖然程式設計師在PHP中有可以透過PHP程式碼設定時區的功能。

顯示時間時所使用的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.
老號 參數 描述 1 高 它代表一個小時的24小時格式。它代表[00-23]之間的小時。 2 高 它代表一小時的 12 小時格式。它表示 [1-12] 之間的小時,並帶有前導零。 3 s 它表示帶有前導零的秒。它代表[00-59]之間的秒。 4 我 它表示帶有前導零的分鐘。代表[00-59]之間的數據。 5 A,'a' 表示顯示的時間是上午還是下午(中午前和下午後)。 6 r 用於傳回完整的日期和時間值。 表>

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

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

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

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

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.

以上是現在 PHP的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!