溫馨提示×

php獲取當前時間代碼怎么寫

PHP
小億
133
2023-09-15 01:38:29
欄目: 編程語言

以下是一些示例代碼,展示如何在PHP中獲取當前時間:

  1. 使用date函數獲取當前時間:
$current_time = date("Y-m-d H:i:s");
echo $current_time;
  1. 使用time函數獲取當前時間的時間戳,并通過date函數格式化輸出:
$current_timestamp = time();
$current_time = date("Y-m-d H:i:s", $current_timestamp);
echo $current_time;
  1. 使用DateTime類獲取當前時間:
$current_time = new DateTime();
echo $current_time->format("Y-m-d H:i:s");
  1. 使用Carbon庫獲取當前時間(需要先安裝Carbon庫):
require 'vendor/autoload.php'; // 引入Carbon庫
use Carbon\Carbon;
$current_time = Carbon::now();
echo $current_time->toDateTimeString();

這些代碼中,都使用了常見的日期格式(年-月-日 時:分:秒)來格式化輸出當前時間。您可以根據自己的需要,選擇適合的代碼來獲取當前時間。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女