在 PHP 中設定時區

PHP 中的時區可以使用 php.ini
設定進行更改。PHP 還提供了一個內建函式 date_default_timezone_set()
,它還可以在 PHP 中設定時區。
本教程演示如何在 PHP 中設定時區。
在 PHP 中設定時區
在 PHP 中設定時區有兩種方法。一個來自 php.ini
檔案,另一個來自內建方法。
使用 PHP.INI
檔案在 PHP 中設定時區
按照以下步驟使用 php.ini
檔案設定時區:
使用 date_default_timezone_set()
方法在 PHP 中設定時區
date_default_timezone_set()
是 PHP 中的一個內建方法,用於在 PHP 中設定時區。如果時區有效,該方法返回 false;否則,它將始終返回 true。
PHP 5.1+ 版本支援此方法。此方法的語法是:
textCopydate_default_timezone_set(timezone)
其中 timezone
是 UTC、GMT 或 Asia/Kolkata 等時區,時區列表可在此處找到。
讓我們嘗試 date_default_timezone_set()
方法的示例:
phpCopy<?php
echo "The Default Timezone: ";
echo date_default_timezone_get();
echo "<br><br>";
date_default_timezone_set("Europe/Rome");
echo "The Updated Timezone: ";
echo date_default_timezone_get();
?>
上面的程式碼顯示了預設時區,然後使用 date_default_timezone_set()
方法對其進行更新。見輸出:
textCopyThe Default Timezone: UTC
The Updated Timezone: Europe/Rome
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.
LinkedIn Facebook