【问题标题】:Insert date and time from Excel sheet into DB with PHP使用 PHP 将 Excel 工作表中的日期和时间插入数据库
【发布时间】:2014-12-10 19:57:19
【问题描述】:

我有不同列的 Excel 表,但我需要在数据库中添加一些特定列(电子邮件、日期和时间)。日期从 9/7/14 开始,一切正常,直到 9/12 /14 ,但是当天变成 13 到月底时,我将 1970-01-01 01:00:00 输入数据库,当月份再次变为 10 时,一切都很好,直到 10/12 /14 和表格 13 我将 1970-01-01 01:00:00 输入数据库。

这是我的 insert.php 代码

session_start();
$target_dir = $_SESSION["targetDir"];
$file_name = $_SESSION["fileName"];

 ini_set('memory_limit', '512M');
  require_once 'excel_reader2.php';
  require_once 'dbConn.php';
 $data = new Spreadsheet_Excel_Reader($target_dir);
 for($i=0;$i<count($data->sheets);$i++) // Loop to get all sheets in a file.
     {
switch ($i) {
    case 1:
         echo "<p class='font_p'>Rows in Sheet $i : " . count($data->sheets[$i]['cells'])."&nbsp&nbsp&nbsp&nbsp" ;
        $countWithHeader = count($data->sheets[$i]['cells']);
        $finalCount = $countWithHeader - 1;

        for ($j = 1; $j <= count($data->sheets[$i]['cells']); $j++) // loop used to get each row of the sheet
        {
            $data->sheets[$i]['cells'][$j][1];
            $email = mysqli_real_escape_string($connection, $data->sheets[$i]['cells'][$j][1]);
            $first_name = mysqli_real_escape_string($connection, $data->sheets[$i]['cells'][$j][2]);
            $last_name = mysqli_real_escape_string($connection, $data->sheets[$i]['cells'][$j][3]);
            $in_time = mysqli_real_escape_string($connection, $data->sheets[$i]['cells'][$j][5]);
            $out_time = mysqli_real_escape_string($connection, $data->sheets[$i]['cells'][$j][6]);

            $timestampIn = date('Y-m-d H:i:s',strtotime($in_time));
            $timestampOut = date('Y-m-d H:i:s',strtotime($out_time));

            $query = "INSERT INTO study_table (email,last_name,first_name,in_time,out_time)
                      VALUES ('".$email."','".$first_name."','".$last_name."','".$timestampIn."','".$timestampOut."')";

            mysqli_query($connection, $query);
        }
        break;

    case 2:

【问题讨论】:

  • 你能告诉我们var_dump($in_time);var_dump($out_time);吗?
  • 这是 var_dump($in_time);var_dump($out_time);问题发生时的 2014 年 9 月 14 日: string(16) "14/09/2014 14:50" string(16) "14/09/2014 15:13"
  • strtotime() 返回 false,因为 php 将 14/09/2014 解析为 m/d/Y,但您尝试将其用作 d/m/Y。在发送到 strtotime() 之前使用 DateTime::createFromFormat() 或 str_replace('/', '-', $in_time)
  • 谢谢 Glavić 我会尝试让你知道它是否有效。
  • 当然,确实如此。我不能投票,因为它需要 15 声望。

标签: php database excel datetime


【解决方案1】:

函数strtotime() 将在您的代码中返回false

$timestampIn = date('Y-m-d H:i:s', strtotime($in_time));

因为 $in_time 的格式无效 (14/09/2014 14:50)。函数将14/09/2014 解析为m/d/Y 格式,但您尝试将其用作d/m/YSee here for demo.

简单的解决方案是将日期分隔符/ 替换为-,因为格式更改为d-m-Y,这是有效的,strtotime() 将知道如何解析它。 See here for demo 和请RTM here 看看什么是有效的日期格式。

解析未知格式的最佳解决方案是使用DateTime::createFromFormat()方法:

$in_time = DateTime::createFromFormat('!d/m/Y H:i', $in_time);
echo $in_time->format('Y-m-d H:i:s');

demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    • 2011-02-28
    相关资源
    最近更新 更多