溫馨提示×

PHP now函數(shù)與時間戳轉(zhuǎn)換方法

PHP
小樊
81
2024-10-09 05:54:27
欄目: 編程語言

在 PHP 中,now() 函數(shù)和 strtotime() 函數(shù)都可以用于獲取當(dāng)前時間戳。

now() 函數(shù)返回當(dāng)前的日期和時間,格式為 YYYY-MM-DD HH:mm:ss。例如:

$current_time = now();
echo $current_time; // 輸出類似 "2022-01-01 12:34:56"

要將 now() 函數(shù)返回的日期時間字符串轉(zhuǎn)換為時間戳,可以使用 strtotime() 函數(shù)。例如:

$date_string = "2022-01-01 12:34:56";
$timestamp = strtotime($date_string);
echo $timestamp; // 輸出類似 1641027296

同樣地,要將一個時間戳轉(zhuǎn)換為日期時間字符串,也可以使用 date() 函數(shù)。例如:

$timestamp = 1641027296;
$date_string = date("Y-m-d H:i:s", $timestamp);
echo $date_string; // 輸出類似 "2022-01-01 12:34:56"

0