简体   繁体   English

Excel日期值检索为DD / MM / YY,如何将其格式化为YYYY-MM-DD?

[英]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. 在转换从excel工作表检索的日期时, YEAR插入为20XX而不是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. 在excel表格中单击单元格时,我们可以知道年份并正确显示了年份,但是在插入时将19xx插入为20xx。

When i echo the date retrieved from excel sheet like- 当我回显从Excel工作表中检索到的日期时,例如-

    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 输出-有些年份正确,但有些19xx显示为20xx

example- 2066-11-22 instead of 1966-11-22 例如2066-11-22而不是1966-11-22

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

Output- YY-MM-DD 输出-YY-MM-DD

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

Output- 1970-01-01 输出-1970-01-01

Retrieving data from excel in Model- 在Model-中从Excel检索数据

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: 我认为以下代码很糟糕-我宁愿尝试更改Excel电子表格的日期格式-但它适用于您给定的示例日期:

$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... 看来PHP如何处理1970年之前的两位数年份存在问题。使用从PHP文档中获得的dd.mm.yy格式的示例日期,我得到了...

$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. 我需要在两位数年份中添加“ 19”,以使strtotime能够理解年份。

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 . 但是,除了在php端进行处理之外,我所做的是将excel文件的列的数据类型更改为text替换date

Now all i am doing on php side is as regularly we do: 现在,我在php方面所做的就是按照常规进行:

<?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. 但是,如果您无法控制将要上传的excel文件,请使用上述php处理解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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