We have a current date format is 02-05-15
and the output that I want is 02/05/2015
in PHP.
Asked
Active
Viewed 73 times
-1

Narendrasingh Sisodia
- 21,247
- 6
- 47
- 54

Faizan Sattar
- 1
- 1
-
You have to try first – Al Amin Chayan Sep 30 '15 at 05:11
4 Answers
1
Use DateTime
$myDateTime = DateTime::createFromFormat('d-m-y', "02-05-15");
echo $myDateTime->format('d/m/Y');
// Output : 02/05/2015
Note:
You may also want to set the default timezone before using DateTime
, i.e.:
date_default_timezone_set("Europe/Lisbon");
For date_and_time
and other php best practices visit:

Pedro Lobito
- 94,083
- 31
- 258
- 268
0
You can do this in this way
$sections = explode('-','02-05-15');
$formated_date = $sections[0]."/".$sections[1]."/20".$sections[2];
hope this will help

Janaka
- 398
- 5
- 16
0
The following code should do the trick:
<?php
$time = mktime(0,0,0, '02', '05', '15');
//OR mktime(0,0,0, '05', '02', '15'); //depending on the expected format
echo date("d/m/Y",$time); //assuming the above date is 2 May 2015
//alternatively it would be
echo date('m/d/Y', $time); //if the above date is 5 February 2015
?>

Tash Pemhiwa
- 7,590
- 4
- 45
- 49
-
1
-
-
I guess you'll have to try harder...`date() expects parameter 2 to be long, object given in /home/cg/root/main.php on line 3` – Pedro Lobito Sep 30 '15 at 05:58
-
-
Do you actually test your code? it doesn't seem so! now it returns `01/01/1970` you can test it online... http://www.tutorialspoint.com/execute_php_online.php – Pedro Lobito Sep 30 '15 at 06:15
-
Link wasn't working earlier on so I had to do it locally and yeah, fixed it up as shown. – Tash Pemhiwa Sep 30 '15 at 15:22