PHP Date Export To CSV Format

Hector

After export and when I open the CSV up, you'll notice column L formats only some of the dates properly. This happend on a couple other date columns as well. Is there anything I can do with PHP to ensure all dates are returned properly? Below id my function:

public function formatDateCI($date) {

    // If Birthdate Column
    if(strlen($date) == 10){
        $date = explode('-',$date);
        $m = $date[0];
        $d = $date[1];
        $Y = $date[2];
        $date = $Y.'-'.$m.'-'.$d;
    }

    $date = new DateTime($date);
    // Final Format 1983-24-12 00:00:00
    return date_format($date, 'Y-d-m H:i:s');
}

Column L Date Format Issue

Gabriel

Try:

date('Y-m-d H:i:s', strtotime($date));

Instead of:

$date = new DateTime($date);

>

Be sure to try an if else structure:

if(strlen($date) == 10){
        $date = explode('-',$date);
        $M= $date[0];
        $D = $date[1];
        $Y = $date[2];
       //this $date = $Y.'-'.$m.'-'.$d;
       //or this =>
       $date = date('Y-m-d H:i:s', mktime($h, $m, $s, $M, $D, $Y));
    }else{
         $date = date('Y-m-d H:i:s', strtotime($date));
    }

    return $date;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related