简体   繁体   中英

Excel Date value retrieved as DD/MM/YY,how to format it to YYYY-MM-DD?

On converting the date retrieved from the excel sheet the YEAR getting inserted as 20XX instead of 19xx.

On clicking on the cell in excel sheet we can know the year and it is shown correctly,but on insertion 19xx is being inserted as 20xx.

When i echo the date retrieved from excel sheet like-

    echo date('Y-m-d', strtotime($data[3]));
    exit();  //output 1970-01-01

   echo $data[3];
    exit();  //output 22/11/66

I tried with the following methods-

1. $dob=date('Y-m-d', strtotime(str_replace('/', '.', $data[3])));

Output- Some years correct,but some 19xx was shown as 20xx

example- 2066-11-22 instead of 1966-11-22

2. $dob = implode('-', array_reverse(explode('/', $data[3])));

Output- YY-MM-DD

3. $dob=date('Y-m-d', strtotime(str_replace('/', '-', trim($data[3]))));

Output- 1970-01-01

Retrieving data from excel in Model-

function upload(){
   ini_set('auto_detect_line_endings',TRUE);
 /* if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {

  readfile($_FILES['userfile']['tmp_name']);
    }*/
    if (($handle = fopen($_FILES['userfile']['tmp_name'], "r")) !== FALSE) {
    $firstRow = true;
        while (($data = fgetcsv($handle, 4096, ",",'"')) !== FALSE)
        {
         $num = count($data);
                if($data[0]!=""){
                    //echo implode('-', array_reverse(explode('/', $data[3])));exit();
                    if($data[3]!="")
                    {
                        //$dob=date('Y-m-d', strtotime(str_replace('/', '.', $data[3])));
                        //$dob=date("Y-m-d", strtotime($data[3]));
                        //$dob = implode('-', array_reverse(explode('/', $data[3])));
                        //$dob=date("Y-m-d", strtotime($date));
                        $dob=date('Y-m-d', strtotime(str_replace('/', '-', trim($data[3]))));
                    }
                     $data1 = array(
                    'member_name' => $data[1],
                     'member_phone' => $data[2],
                     'member_dob' => $dob,
                     'member_outlet' => $data[4]
                   );
                  //echo "<pre/>";print_r($data) ;exit(); 
                       $this->db->insert('member_info', $data1);
                       $member_id = $this->db->insert_id();
                }

        }

//echo "<pre/>";print_r($data) ;exit();
     fclose($handle); 
    }
ini_set('auto_detect_line_endings',FALSE);             
           }

}

Any suggestions?

I think the following code is terrible - I would rather try to change the Excel spreadsheet's date formatting - but it works for the example date you gave:

$data[3] = '22/11/66'; 
$arr = explode('/', $data[3]);
$dob = date("Y-m-d", strtotime('19' . $arr[2] . '/' . $arr[1] . '/' . $arr[0]));
echo var_dump($dob);

It seems there is a problem with how PHP handles two digit years prior to 1970. Using an example date in dd.mm.yy format from the PHP documentation I get...

$dob = date("Y-m-d", strtotime("30.6.08"));
echo var_dump($dob); // 2008-06-30

$dob = date("Y-m-d", strtotime("30.6.66"));
echo var_dump($dob); // 1970-01-01

$dob = date("Y-m-d", strtotime("30.6.1966"));
echo var_dump($dob); // 1966-06-30

I needed to add "19" to the two digit year to get strtotime to understand the year.

I have faced same problem as above,

But instead of doing processing on php side what i have done is changed the excel file's column's datatype as text replacing date .

Now all i am doing on php side is as regularly we do:

<?php
  $date = date("Y-m-d", strtotime($excel_date));
 //here you can insert it in your db table if you want.
?>

But if you don't have control on excel file which will be uploaded, then go with the above php processing solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM