Home  >  Article  >  Backend Development  >  How to convert date format in PHP

How to convert date format in PHP

不言
不言Original
2019-02-18 10:52:006462browse

This article uses PHP strtotime() and date() functions to convert date and time format. For example, if a date is stored in a variable in YYYY-MM-DD format, it needs to be changed to MM-DD-YYYY format.

How to convert date format in PHP

We can achieve this by first converting the date to seconds using the strtotime() function. After using the date() function to reconstruct the date to any format. Here are a few conversion examples:

1. Change YYYY-MM-DD => MM-DD-YYYY

Here we have the date yyyy-mm-dd ("2019-02-18") format and convert it to mm-dd-yyyy ("02-18-2019") format.

$origDate = "2019-02-18";
 
$newDate = date("m-d-Y", strtotime($origDate));
echo $newDate;

The output is:

02-18-2019

2. Change YYYY-MM-DD => DD-MM-YYYY

Here we have the date yyyy-mm-dd ("2019-02-18") format and convert it to dd-mm-yyyy ("18-02-2019") format.

$origDate = "2019-02-18";
 
$newDate = date("d-m-Y", strtotime($origDate));
echo $newDate;

The output result is:

18-02-2019

This article has ended here. For more exciting content, you can pay attention to other related column tutorials on the PHP Chinese website! ! !

The above is the detailed content of How to convert date format in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to use ftell functionNext article:How to use ftell function