【问题标题】:How to compare two dates from database which gets records from Excel? [duplicate]如何比较从 Excel 获取记录的数据库中的两个日期? [复制]
【发布时间】:2011-03-31 19:29:05
【问题描述】:

可能重复:
How to convert date from one format to another

我有一个包含 5 列的电子表格。其中之一包含格式为 02-Dec-10 的日期列。

当我将该 Excel 表格输入数据库时​​,它会将空日期存储为 0000-00-00 00:00:00。

如何将其存储为日期?

编辑

这是使用 CSV 文件向数据库提供数据的代码

        $date           =           date("d-m-Y");      
        $databasetable = "sample";
        $fieldseparator = ",";
        $lineseparator = "\n";
        $csvfile = "../uploads/" . basename( $_FILES['file']['name']); 
        $addauto = 0;
        $save = 1;
        $outputfile = "../temp/output.sql";
        if(!file_exists($csvfile)) {
            #echo $csvfile;
            echo "File not found. Make sure you specified the correct path.\n";
            return 0;
            exit;
        }

        $file = fopen($csvfile,"r");

        if(!$file) {
            #echo "Error opening data file.\n";
            return 0;
            exit;
        }

        $size = filesize($csvfile);

        if(!$size) {
            echo "File is empty.\n";
            return 0;
            exit;
        }

        $csvcontent = fread($file,$size);

        fclose($file);

        $lines = 0;
        $queries = "";
        $linearray = array();

        foreach(split($lineseparator,$csvcontent) as $line) {
            $lines++;
            $line = trim($line," \t");
            $line = str_replace("\r","",$line);
            $line = str_replace("'","\'",$line);
            $linearray = explode($fieldseparator,$line);
            $linemysql = implode("','",$linearray);
            if($addauto)
                $query = "insert into $databasetable values('','$linemysql');";
            else
                $query = "insert into $databasetable values('$linemysql');";
            $queries .= $query . "\n";
            @mysql_query($query);
        }

        @mysql_close($con);

        if($save) {
                $file2 = fopen($outputfile,"w");                    
                if(!$file2) {
                    echo "Error writing to the output file.\n";
                    return 0;
                }
                else {
                    fwrite($file2,$queries);
                    fclose($file2);
                    return 1;
                }
        }
        //echo "Found a total of $lines records in this csv file.\n";

【问题讨论】:

  • 你用什么样的方法使用什么样的库使用代码输入什么样的数据库?
  • 请停止发布重复的内容,尤其是如果新的和另一个的措辞一样不清楚,并且那里已经提供了大量的帮助。
  • 这不是重复的伙伴......我错误地发布了那个问题。这个问题无关紧要
  • @Rajasekar 很好,但是请停止发布重复内容。这是在浪费每个人的时间,也是对在另一个问题上帮助过你的人不尊重的表现。跨度>
  • 没什么大不了的,只是不是一件好事。无论如何,我会通知版主合并这两个问题,因为它们都有答案。这样就解决了。

标签: php mysql datetime date


【解决方案1】:

只要您只使用 1970 到 2038 之间的日期,strtotime() 就应该可以安全使用。在每个日期列上执行此操作以将其转换为 mySQL 的 YYYY-MM-DDdate 格式:

 $column = date("Y-m-d", strtotime($column)); // Will return 2010-12-02

确定日期列的最简单方法是手动指定它们。否则,您必须事先开始获取表的结构,并查看哪些列是DATEDATETIME

【讨论】:

    【解决方案2】:

    改编自我关于您其他类似问题的帖子:

    对于更通用的方法,您始终可以将当前格式转储为字符串,就像您拥有它的方式一样,并使用字符串操作来子字符串和重新组织。我知道 MySQL 接受 DATETIME 字段的字符串值。

    $day = substr($input, 0, 2);
    $month = substr($input, 2, 3);
    switch($month){
        case "Jan":
            $month = "01";
            break;
        ...
    }
    

    将此字符串作为其显示格式插入到您的数据库中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-28
      • 2013-05-30
      • 2021-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-29
      • 2014-07-18
      相关资源
      最近更新 更多