如何比较从Excel获取记录的数据库的两个日期 [英] How to compare two dates from database which gets records from excel

查看:129
本文介绍了如何比较从Excel获取记录的数据库的两个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何将日期从一种格式转换为另一种格式

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

I am having a spreadsheet with 5 columns. One of that contain date column with format 02-Dec-10.

当我将该excel表提供给数据库时,它将作为空日期存储为0000-00-00 00: 00:00

when i feed that excel sheet to database it stores as empty date as 0000-00-00 00:00:00

我如何将其存储为日期。 Plz帮助

How can i store it as date. Plz help

编辑1:

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

This is the code to feed data to database using CSV file

        $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";


推荐答案

只要你在1970年之间工作而仅限于2038年, strtotime()应该是安全的使用。在每个日期列上执行此操作将其转换为mySQL的 YYYY-MM-DD 日期格式:

As long as you're working with dates between 1970 and 2038 only, strtotime() should be safe to use. Do this on each date column to convert it to mySQL's YYYY-MM-DDdate format:

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

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

the easiest way to determine a date column would be to specify them manually. Otherwise, you'd have to start fetching the table's structure beforehand, and looking which columns are DATE or DATETIME ones.

这篇关于如何比较从Excel获取记录的数据库的两个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆