php

Convert a date DD-MM-YYYY to YYYY-MM-DD in PHP

In this tutorial, we are going to see how to write a PHP script to convert a date from DD-MM-YYYY to YYYY-MM-DD.You can first use the PHP strtotime() function to convert any textual date to the Unix timestamp, then simply use the PHP date() function to convert that timestamp to the desired date format.

The following example will convert a date from the format YYYY-MM-DD to DD-MM-YYYY.
 

Convert a date DD-MM-YYYY to YYYY-MM-DD in PHP
<?php
	$date = "25-05-2021";
	 
	// Create the timestamp from the given date
	$timestamp = strtotime($date);
	 
	// Create the new format from the timestamp
	$date = date("Y-m-d", $timestamp);
	echo $date;
?>

Output:

2021-05-25

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *