Hi

Please could someone kindly help me with an issue I'm having converting date formats.

I'd like to convert dates which are in the format mm/dd/yy hh:mm:ss into yyyy/mm/dd hh:mm:ss but am kind of stuck on how to do it.

Anyone able to help?

Many thanks in advance.

Member Avatar for diafol

I suppose there are all sorts of nice conversion scripts out there. You can roll your own if you have a fixed input and output formats. A problem with your specific input format is the two year 'yy'. I assume that all dates need to be set to '20yy'?

You can split the input into components via substr():

function returnDate($input_date){
 //have a handling routine to check the validity of the input
 //a good way to do this would be with regular expressions - but I must admit, I struggle with these!
 //mm/dd/yy hh:mm:ss 
 //0123456789....... (character position - notice start at position 0)

 $month = substr($input_date,0,2);
 $day = substr($input_date,3,2);
 $year = '20' . substr($input_date,6,2);
 $time = substr($input_date,8);
 $output_date = $year . "/" . $month . "/" . $day . $time;
 return $output_date;
}

I haven't tested this! As mentioned, regular expressions could be of help here, cutting down the code to about two lines.

function returnDate($input_date){
//validate
 return $output_date = '20' . substr($input_date,6,2) . "/" . substr($input_date,0,2) . "/" . substr($input_date,3,2) . substr($input_date,8);
}

Of course, you could cut the the function to the above (one line), but that's just ugly!

Hi,

How about trying the following?

function convertDate($strDate) {
    return date('Y/m/d G:i:s', strtotime($strDate));
}

R.

I suppose there are all sorts of nice conversion scripts out there. You can roll your own if you have a fixed input and output formats. A problem with your specific input format is the two year 'yy'. I assume that all dates need to be set to '20yy'?

You can split the input into components via substr():

function returnDate($input_date){
 //have a handling routine to check the validity of the input
 //a good way to do this would be with regular expressions - but I must admit, I struggle with these!
 //mm/dd/yy hh:mm:ss 
 //0123456789....... (character position - notice start at position 0)

 $month = substr($input_date,0,2);
 $day = substr($input_date,3,2);
 $year = '20' . substr($input_date,6,2);
 $time = substr($input_date,8);
 $output_date = $year . "/" . $month . "/" . $day . $time;
 return $output_date;
}

I haven't tested this! As mentioned, regular expressions could be of help here, cutting down the code to about two lines.

function returnDate($input_date){
//validate
 return $output_date = '20' . substr($input_date,6,2) . "/" . substr($input_date,0,2) . "/" . substr($input_date,3,2) . substr($input_date,8);
}

Of course, you could cut the the function to the above (one line), but that's just ugly!

You, my friend, are awesome :)

Have to make a few changes to it but you've put me on the right track. Thank you!

Hi,

How about trying the following?

function convertDate($strDate) {
    return date('Y/m/d G:i:s', strtotime($strDate));
}

R.

Many thanks for the reply. I'm pretty sure strotime only works when the date format is mm/dd/yy though...?

Member Avatar for diafol

I agree with strtotime. It works with US format data - which is what you quoted in your first post and the format which I used in my solution. So technically, robothy is right - much nicer that my cobbled solution. However, the question in your title seems to be in UK/European format, so strtotime won't work with this.


As I mentioned, you could rework the function to work with all different kinds of format:

function($input_date,$input_format){
//$input_format could be 'us', 'euro' - depending on the parameter, you could do the following:
  if($input_format = 'us'){
    $output_date = date('Y/m/d G:i:s', strtotime($input_date));
  }else{
    $output_date = '20' . substr($input_date,6,2) . "/" . substr($input_date,3,2) . "/" . substr($input_date,0,2) . substr($input_date,8)
  }
  return $output_date;
}

BTW - made a mistake in my previous post:

function returnDate($input_date){
//validate
 return $output_date = '20' . substr($input_date,6,2) . "/" . substr($input_date,0,2) . "/" . substr($input_date,3,2) . substr($input_date,8);
}

should have been:

function returnDate($input_date){
//validate
 return '20' . substr($input_date,6,2) . "/" . substr($input_date,3,2) . "/" . substr($input_date,0,2) . substr($input_date,8);
}

No need for $output_date variable and swapped values to make UK/Euro format.

REM - if solved, mark it so. Cheers.

I seem to have made an error in the actual description... should have stated dd/mm/yy like in the title of the thread. My error.

Yep, I'd already made that amendment to your code plus a couple of other changes :)

I thought I'd already marked this thread as solved. Is it still showing as unsolved?

Many thanks to you both once again and apologies to robothy as it was my mistake.

Thanks diafol :)

Im getting $dob in this format 26-DEC-85
I changed to 1985-12-26
I did like this:

/**
* parse the date of birth
*/

function person_details_dob( &$params , $dob)



    if (empty($dob))
    {
        return;
    }



    $day = substr($dob,0,2);
    $month = substr($dob,3,3);
    $yy = substr($dob,7,8);

    if($yy > 30){
        $year = '19' . $yy;
    }
    else
    {
        $year = '20' . $yy;
    }

    $months_array =  array(
                        'JAN'       => '01',
                        'FEB'       => '02',
                        'MAR'       => '03',
                        'APR'       => '04',
                        'MAY'       => '05',
                        'JUN'       => '06',
                        'JUL'       => '07',
                        'AUG'       => '08',
                        'SEP'       => '09',
                        'OCT'       => '10',
                        'NOV'       => '11',
                        'DEC'       => '12',
                        );
    if ($month)
    {
        $month = $months_array[$month];
    }

    return $dob = $year . "-" . $month . "-" . $day;

}
Member Avatar for diafol

Im getting $dob in this format 26-DEC-85
I changed to 1985-12-26

Use this - it's easier

function formatDOB($date,$inputFormat='j-M-y',$outputFormat='Y-m-d')
{
    $d = date_create_from_format($inputFormat, $date);
    return date_format($d, $outputFormat);
}

echo formatDOB('26-DEC-85');

Obviously, you know to be careful with 2 digit years.

00-69 = 2000-2069
70-99 = 1970-1999

You could always set your own cutoff though.

Something like this:

function formatDOB($date,$yearcutoff=70,$inputFormat='j-M-y',$outputFormat='Y-m-d')
{
    $d = date_create_from_format($inputFormat, $date);
    if($yearcutoff != 70 && is_int($yearcutoff) && $yearcutoff < 100 && strpos($inputFormat,'y') !== false)
    {
        $y = date_format($d, 'y');
        if($y >= $yearcutoff && $y < 70){
            $d = date_sub($d, date_interval_create_from_date_string('100 years'));      
        }elseif($y < $yearcutoff && $y >= 70){
            $d = date_add($d, date_interval_create_from_date_string('100 years'));
        }
    }
    return date_format($d, $outputFormat);
}

//Usage

echo formatDOB('08-12-67',50,'d-m-y'); //gives: 1967-12-08

//without the yearcutoff it would give: 2067-12-08


/*TEST SCRIPT
for($i=0;$i<=99;$i++){
echo "<h3>Year CUTOFF: $i</h3>";

    for($x = 0;$x<=99;$x++)
    {
        $y = str_pad($x,2,'0',STR_PAD_LEFT);
        echo 'Shortyear:' . $y . " = " . formatDOB('26-Dec-' . $y, $i) . "<br />";
    }
}
*/

If you find a cutoff number that you're happy to stick with, it's easier to switch the parameter order:

function formatDOB($date,$inputFormat='j-M-y',$outputFormat='Y-m-d',$yearcutoff=40)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.