溫馨提示×

PHP now()和strtotime對比

PHP
小樊
112
2024-07-16 14:06:49
欄目: 編程語言

在PHP中,now()strtotime() 是兩個常用的日期時間函數(shù)。

  1. now() 函數(shù):now() 函數(shù)返回當(dāng)前的日期和時間,格式為 Y-m-d H:i:s,即年-月-日 時:分:秒格式。它返回的是當(dāng)前服務(wù)器的時間。例如:
echo now(); // 輸出類似于 2022-01-25 14:30:00
  1. strtotime() 函數(shù):strtotime() 函數(shù)將任何英語文本的日期時間描述轉(zhuǎn)換為 Unix 時間戳(自 January 1 1970 00:00:00 GMT 起的秒數(shù))。它可以接受各種日期時間表示形式,例如 “now”, “+1 day”, “tomorrow”, “next week”, 等等。例如:
$timestamp = strtotime("+1 day");
echo date('Y-m-d H:i:s', $timestamp); // 輸出明天的日期和時間

總的來說,now() 返回的是當(dāng)前的日期和時間,而 strtotime() 接受一個日期時間描述并將其轉(zhuǎn)換為 Unix 時間戳。兩者可以根據(jù)實際需求來選擇使用。

0