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

How to convert date format in PHP

不言
Release: 2023-04-05 09:44:01
Original
6499 people have browsed it

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;
Copy after login

The output is:

02-18-2019
Copy after login

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;
Copy after login

The output result is:

18-02-2019
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template