首頁  >  文章  >  後端開發  >  PHP 更改日期格式

PHP 更改日期格式

王林
王林原創
2024-08-29 13:06:35519瀏覽

PHP 日期函數用於列印伺服器日期或目前日期。它主要用於記錄系統在伺服器上執行任何操作時的資訊。人們可以更改任何特定操作所需的日期資訊。提供的數據將是標準的 (yyyy-mm-dd),因為可以根據需要使用許多日期方法和函數更改日期。下面列出了用於更改伺服器中日期格式的技術。

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

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

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

PHP 日期格式:

PHP 更改日期格式為您提供執行特定 PHP 腳本的伺服器的日期資訊。因此,當日期函數執行時,伺服器會以標準格式向我們提供資訊。如果您想根據需要更改格式,可以使用多種方法來完成。

日期格式類型

PHP 中使用了幾種資料格式來根據要求列印日期:

  • date_create(): 此函數將根據 date_format 函數中提到的日期格式建立日期。如果您將日期格式傳遞為 dd-mm-yyyy,它將以提到的格式列印日期。它將物件傳回 date_format () 函數。
  • date_modify():此函數將修改使用者指定的日期,即,如果使用者指定+15天,則列印的日期將比目前日期早15天。此函數修改要列印的伺服器的時間戳記。
  • date_create_from_format (): 此函數將建立格式中提到的日期,根據所提及的格式建立一個對象,並將其傳遞給 date_format 函數。

如何轉換日期格式?

許多程式設計師使用不同的日期和時間函數來根據他們的要求進行更改。更改取決於地區以及使用地點。

例如,如果使用者想要使用資料庫來記錄任何員工條目,而伺服器的日期格式與資料庫使用的格式不同,那麼資料庫將不接受標準格式,因為資料庫有其限制。因此,使用者必須使用 PHP 中可用的各種技術將格式變更為所需的格式。在很多情況下,日期和時間函數都有其格式。

例如,在印度,日期將以IST(印度標準時間)格式列印;在美國,將以CDT/CST(中部標準時間)格式列印;在日本,將以JST(日本標準時間)格式列印,在加拿大,將以EST(東部標準時間)格式列印。因此,資料庫和伺服器將具有相對不同的時區。為了使其與日期格式相容,時區會相應更改,以免輸入的資料之間出現任何衝突。其中一項技術是 strtotime() 函數,它按原樣列印格式中提到的日期。  

範例#1

這裡是使用 strtotime() 函數的範例,該函數列印日期。

代碼:

<!DOCTYPE html>
<html>
<body>
<?php
$o = "2019-10-30";
// It creates the timestamp from the date mentioned.
$a = strtotime($o);
$new = date("d/m/Y", $a);
echo $new;
?>
</body>
</html>

輸出:

PHP 更改日期格式

在上面的輸出中,提到的日期將是日期函數中提到的日期。由於這裡提到的日期是 d/m/y,因此輸出也具有相同的格式。如果您想指定連字符(-)而不是反斜線(/),您可以在日期格式本身中提及它。

範例#2

以下是如何將日期從標準格式轉換為所需格式的一些範例。

代碼:

<!DOCTYPE html>
<html>
<body>
<?php
$currentdate=date_create("2012-09-13");
echo date_format($currentdate,"m/d/Y");
?>
</body>
</html>

輸出:

PHP 更改日期格式

在上面的程式碼和輸出中,日期格式函數中指定的日期是輸出中要列印的日期。使用者可以指定他想要看到的任何格式,例如 dd-mm-yyyy 或 dd-mm-yy 或 dd/mm/yyyy 或 dd/mm/yy 等

代碼:

<!DOCTYPE html>
<html>
<body>
<?php
$a = '2019-05-25';
$d = new DateTime($a);
echo $d->format('Y.m.d');
?>
</body>
</html>

輸出:

PHP 更改日期格式

在上面的程式碼和對應的輸出中,日期格式在使用函數格式傳遞的程式碼中提到,如上所示。 DateTime() 方法會將日期轉換為 format 函數中提到的格式,並建立物件將其傳遞給 format 函數。

Code:

<!DOCTYPE html>
<html>
<body>
<?php
$date=date_create_from_format("j-M-Y","27-Feb-2019");
echo " The changed date format is ", date_format($date,"d/m/Y");
?>
</body>
</html>

Output:

PHP 更改日期格式

In the above code, the format specified in the date created from the format method is the input to the method date_format as the date mentioned in the code will be printed in the format mentioned in the code.

So we learned many methods as to how to change the date for a particular format using various date functions. In all the date functions, the common thing was already the date was mentioned in the code itself. If you want to take the current date as the input to the specified format, you can simply use the date () function or Date Time () function to retrieve the date and time, respectively. So the user can work the static way as well as a dynamic way to retrieve the date from the server. Similarly, like date functions, we have various time functions that can be used to change the time zones as well as the time format of the servers. All of the servers will have a common time, i.e. UTC (Universal Time Zone), unless it gets changed to its respective country/region. 

Conclusion – PHP Change Date Format

In this article, we discussed the date format, how to change the date format, and its types. These changes’ main objective is to have a smooth flow between various conflicts faced when servers of different countries are used together and have a commonplace as a repository.

Recommended Article

This is a guide to PHP Change Date Format. Here we discuss different types of date format and their examples, along with code implementation and output. You can also go through our other suggested articles to learn more –

  1. Palindrome in PHP
  2. Abstract Class in PHP
  3. Socket Programming in PHP
  4. Factorial in PHP

以上是PHP 更改日期格式的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn