在导入到数据库

时间:2015-11-17 21:26:51

标签: php database csv ms-access

我遇到了一些困难。我有应用程序,即读取CSV文件数据,并将其导入Access数据库。问题是 - CSV文件中的日期格式 - 我想将其更改为年/月/日。为此,我需要知道在导入之前如何格式化它?

代码:

if (isset($_POST['submit'])) {
            $i=0;
            require "connection.php";
            if (is_uploaded_file($_FILES['csv']['tmp_name'])) {
                echo "<h3>" . "File ". $_FILES['csv']['name'] ." uploaded successfully." . "</h3>";
            }
            //Import uploaded file to Database
            $handle = fopen($_FILES['csv']['tmp_name'], "r");
            $import=$db->prepare("INSERT INTO adherence(
                    dateandtime,
                    lastname,
                    firstname,
                    paidtime,
                    approvedtime,
                    notadhering)VALUES(
                    ?,?,?,?,?,?)");
            while (($data = fgetcsv($handle, 10000,',',"'")) !== FALSE) {
                if($i>0) {
                $data = str_replace('"', '', $data);
                $data = str_replace('00:00', '', $data);
                $data = str_replace('0:00', '', $data);
                $data = str_replace('/01', '/1', $data);
                $data = str_replace('/02', '/2', $data);
                $data = str_replace('/03', '/3', $data);
                $data = str_replace('/04', '/4', $data);
                $data = str_replace('/05', '/5', $data);
                $data = str_replace('/06', '/6', $data);
                $data = str_replace('/07', '/7', $data);
                $data = str_replace('/08', '/8', $data);
                $data = str_replace('/09', '/9', $data);

                $import->bindParam(1, $data[0], PDO::PARAM_STR);
                $import->bindParam(2, $data[1], PDO::PARAM_STR);
                $import->bindParam(3, $data[2], PDO::PARAM_STR);
                $import->bindParam(4, $data[3], PDO::PARAM_STR);
                $import->bindParam(5, $data[4], PDO::PARAM_STR);
                $import->bindParam(6, $data[5], PDO::PARAM_STR);
                str_replace('"',' ',$data);
                $import->execute();
                }
                $i++;

            }
            $removal=$db->prepare("delete FROM adherence WHERE approvedtime = '0' OR notadhering IS NULL");
            $removal->execute();

            fclose($handle);
        }

See a screenshot of the CSV file format

任何帮助将不胜感激。

修改

<?php
        require "connection.php";
        $getdata = $db->prepare("SELECT DISTINCT dateandtime FROM adherence ORDER BY dateandtime"); 
        $getdata->execute(); 
        $infodate = $getdata->fetchAll(PDO::FETCH_ASSOC);
        $myinfoDate =  date( "Y/n/j", strtotime($infodate));
            ?>
            <form name='add' method="POST">
                <select name="late" id="late">
                <?php foreach ($myinfoDate as $row): ?>
                <option><?=$row["dateandtime"]?></option>            
                <?php endforeach ?>
                </select>
                <input type='submit' name='submitlate'/>
            </form> 

在这里,我尝试从数据库获取日期信息(没有时间)。它给出了错误:警告:strtotime()期望参数1为字符串。 怎么处理这一个?我有一些问题理解这个...

2 个答案:

答案 0 :(得分:2)

无需执行这些字符串替换... strtotime()可以将大多数字符串转换为日期...

lists

答案 1 :(得分:0)

如果csv格式是m / d / y那么你可以使用strtotime()

$date = date("Y/m/d", strtotime($data['0']));

但是如果它是d / m / y那么你需要处理字符串,因为strtotime()假定欧洲日期格式为2位数年份为y-m-d,所以你需要处理像

这样的东西
// split to date and time bits
$bits = explode(" ", $data['0']);
// split the date into day month year (assumed)
$dateBits = explode("/", $bits['0']);
// split the time into hour minute
$timeBits = explode(":", $bits['1']);
// rebuild the time from the bits
$new = mktime($timeBits['0'], $timeBits['1'], 0, $dateBits['1'], $dateBits['0'], $dateBits['2']);
$date = date("Y/m/d", $new);