TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

How to change date format in PHP

PHP uses two function strtotime() and date() functions to convert the date format from one format to another like ? MM-DD-YYYY to DD-MM-YYYY.

<< Back to HOW

How to change date format in PHP?

To convert the date-time format PHP provides strtotime() and date() function. We change the date format from one format to another. For example - we have stored date in MM-DD-YYYY format in a variable, and we want to change it to DD-MM-YYYY format.

We can achieve this conversion by using strtotime() and date() function. These are the built-in functions of PHP. The strtotime() first converts the date into the seconds, and then date() function is used to reconstruct the date in any format. Below some examples are given to convert the date format.

Change YYYY-MM-DD to DD-MM-YYYY

In the below example, we have date 2019-09-15 in YYYY-MM-DD format, and we will convert this to 15-09-2019 in DD-MM-YYYY format.

<?php
	$orgDate = "2019-09-15";
	$newDate = date("d-m-Y", strtotime($orgDate));
	echo "New date format is: ".$newDate. " (MM-DD-YYYY)";
?>

Output

New date format is: 15-09-2019 (DD-MM-YYYY)

Change YYYY-MM-DD to MM-DD-YYYY

In the below example, we have date 2019-02-26 in YYYY-MM-DD format, and we will convert this to 02-26-2019 (MM-DD-YYYY) format.

<?php
	$orgDate = "2019-02-26";
	$newDate = date("m-d-Y", strtotime($orgDate));
	echo "New date format is: ".$newDate. " (MM-DD-YYYY)";
?>

Output

New date format is: 02-26-2019 (MM-DD-YYYY)

Change DD-MM-YYYY to YYYY-MM-DD

In the below example, we have date 17-07-2012 in DD-MM-YYYY format, and we will convert this to 2012-07-17 (YYYY-MM-DD) format.

<?php
	$orgDate = "17-07-2012";
	$newDate = date("Y-m-d", strtotime($orgDate));
	echo "New date format is: ".$newDate. " (YYYY-MM-DD)";
?>

Output

New date format is: 2012-07-17 (YYYY-MM-DD)

Change DD-MM-YYYY to YYYY/MM/DD

Suppose we have date 17-07-2012 in DD-MM-YYYY format separated by dash (-) sign. We want to convert this to 2012/07/17 (YYYY/MM/DD) format, which will be separated by the slash (/). In the below example, DD-MM-YYYY format is converted to the YYYY-MM-DD format, and also dashes (-) will be replaced with slash (/) sign.

<?php
	$orgDate = "17-07-2012";
	$date = str_replace('-"', '/', $orgDate);
	$newDate = date("Y/m/d", strtotime($date));
	echo "New date format is: ".$newDate. " (YYYY/MM/DD)";
?>

Output

 date format is: 2012/07/17 (YYYY/MM/DD)

Change date time to another format

Here in the below example, we will convert the date format MM-DD-YYYY to YYYY-DD-MM format and 12 hours time clock to 24 hours time clock.

<?php
	$date = "06/13/2019 5:35 PM";
	//converts date and time to seconds
	$sec = strtotime($date);
	//converts seconds into a specific format
	$newdate = date ("Y/d/m H:i", $sec);
	//Appends seconds with the time
	$newdate = $newdate . ":00";
	// display converted date and time
	echo "New date time format is: ".$newDate;
?>

Output

New date time format is: 2019/13/06 17:35:00





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf