How to convert date from dd-mm-yy to mm-dd-yy

How to convert date from dd-mm-yy to mm-dd-yy

below is my code

 <? $date= date('dm-y'); echo date('md-Y',strtotime($date)); ?> 

but the result: 09-10-2001

I need 09-01-2010

+6
date php
source share
5 answers

You can do:

 $input = 'dd-mm-yy'; $a = explode('-',$input); $result = $a[1].'-'.$a[0].'-'.$a[2]; 
+3
source share

This will not work:

 date('md-y', strtotime($the_original_date)); 

If you have PHP 5.3, you can do:

 $date = DateTime::createFromFormat('dm-y', '09-10-01'); $new_date = $date->format('md-Y'); 

Otherwise:

 $parts = explode('-', $original_date); $new_date = $parts[1] . '-' . $parts[0] . '-' . $parts[2]; 
+8
source share

try date("mdy", {your date variable})

0
source share

If the format is always as above, this should work:

 $pieces = explode("-", $yourTimestring); $timestamp = mktime(0,0,0,$pieces[1], $pieces[0], $pieces[2]); $newDateStr = date("mdy", $timestamp); 

Edit: True, I just saw the answer from "codaddict", and yes, if I shared it, you could also link it directly ^^

0
source share

You can use the sscanf function if you do not have access to PHP 5.3 and use the argument exchange option:

 $theDate = '01-02-03'; print join(sscanf($theDate, '%2$2s-%1$2s-%3$2s'), '-'); # output: 02-01-03 

sscanf basically parses the provided string in the array based on the provided format, replacing the first matched argument (day) with the second (month), leaving the third (year) untouched, and then this array is again connected to the separator - .

0
source share

All Articles