tags:

views:

61

answers:

10

I have a date value posted from php using jquery (MMDDYYYY)and i want to convert this date format to YYYYMMDD using php .

                       $fromDate=str_replace("'","''",trim($_POST['tx_fromDateDsp']));
        echo "-->".$year = substr($fromDate, 6, 4)."<br>";
        echo "-->".$month = substr($fromDate, 0, 2)."<br>";
        echo "-->".$date = substr($fromDate, 3, 2)."<br>";
        echo $new_date = date( 'Ymd', strtotime($month, $date, $year ));

Suppose in the above code I have entered date as '070122010'.The new_date at the down gives me '20100714' . I dont know why it is giving todays Date .I have tried both the mktime and strtotime both are giving same result .Iam expecting as '20100712'(YYYYMMDD)

A: 
$new_date = date("Ymd", strtotime($old_date));
Dennis Haarbrink
`date("Ymd", strtotime('12242010'))` should return `20101224` but it returns `19700101`
jigfox
Indeed.. hadn't realized that. Sometimes you just put a little too much trust in something ;) (although strtotime() IS a gem of course!)
Dennis Haarbrink
It is returning '19691231' when i use the strtotime()
Someone
+3  A: 

strtotime($old_date) won't work because MMDDYYYY is not a valid date string: http://www.php.net/manual/en/datetime.formats.date.php

preg_replace("/([0-9]{2})([0-9]{2})([0-9]{4})/","$3$1$2",$old_date);

or an even shorter version:

preg_replace("/([0-9]{4})([0-9]{4})/","$2$1",$old_date);
jigfox
Since both formats contain MMDD, you can simplify your regex a bit
Scott Saunders
@Scott, thanks, updated my answer
jigfox
All you people seem to like a regex (I myself voted for the php5.3 DateTime::createFromFormat b.t.w.), but IF you go that route, please add some sane semi-validation: `"/((0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])([0-9]{4})/"`
Wrikken
+2  A: 

Here are two ways:

$d = preg_replace("/([0-9]{4})([0-9]{4})/", "$2$1", $originalDate);

or

$d = substr($originalDate, 4, 4) . substr($originalDate, 0, 4);

If one of these makes more sense to you, I suggest you use that one. Otherwise, I suspect the second would be slightly faster though I haven't tested.

Scott Saunders
A: 
$data = strptime('12242010','%m%d%Y');

$date = mktime(
 $data['tm_hour'],
 $data['tm_min'],
 $data['tm_sec'],
 $data['tm_mon']+1,
 $data['tm_mday'],
 $data['tm_year']+1900);

echo date('Ymd',$date);
Wrikken
+3  A: 

The DateTime class DateTime::createFromFormat function will do what you want, providing you have a PHP 5.3.0 or greater:

$date = DateTime::createFromFormat('mdY', '12312010');

echo $date->format('Ymd');
// 20101231

echo $date->format('Y-m-d');
// 2010-12-31

echo $date->format('Y-M-d');
//2010-Dec-31
Mike
A: 

THis is my Answer atlast succededded

date( 'Ymd', mktime(0,0,0,$month, $date, $year ));

Someone
+3  A: 

strtotime won't work because MMDDYYYY is not a valid date string:

preg_replace("/([0-9]{2})([0-9]{2})([0-9]{4})/","$3$1$2", $orig_date);
Sarfraz
even shorter: `preg_replace("/([0-9]{4})([0-9]{4})/","$2$1", $orig_date);`
jigfox
even shorter: `preg_replace("/(\d{4})(\d{4})/", "$2$1", $orig_date);` :)
Crozin
A: 

Using substr would be faster: http://php.net/manual/de/function.substr.php

$orig="20072010"; // DDMMYYYY
$new=substr($orig,5,4)+substr($orig,3,2)+substr($orig,0,2); // YYYYMMDD

And it is worth reading this: http://www.php.net/manual/en/datetime.formats.date.php

Andreas Rehm
+1  A: 
function mmddyyyy_to_yyyymmdd ($input) {
    $monthdays = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    if ( !preg_match('/\\A\\d{8}\\Z/', $input) ) {
        return false;
    }
    $month = (int)substr($input, 0, 2);
    $day   = (int)substr($input, 2, 2);
    $year  = (int)substr($input, 4);
    if ( $year % 4 == 0 and
         ( $year % 100 != 0 or $year % 400 == 0 )
         ) {
        $monthdays[1] = 29;
    }
    if ( $month < 1 or
         $month > 12 or
         $day < 1 or
         $day > $monthdays[$month-1]
         ) {
        return false;
    }
    if ( $month < 10 ) { $month = '0'.$month; }
    if ( $day < 10 ) { $day = '0'.$day; }
    while ( strlen($year) < 4 ) {
        $year = '0'.$year;
    }
    return $year.$month.$date;
}
Hammerite
+1 I'm very sure you made it verbose on purpose :)
BoltClock
Why so much work? This can be done MUCH simpler.
Pickle
+2  A: 

You don't need to tear apart the date. All you need to do is move the year to the front.

$orig = '01022010';
$new = substr($orig,4).substr($orig,0,4);
Pickle