所以我正在抓取一个网站的数据,我抓取的一条数据是某些项目的日期。
项目的日期格式为“2015 年 3 月 11 日星期三”。
我一直在尝试将其插入到我的 mysql 数据库中。数据库的结构包含一个以“datapublished”作为时间戳的列,
`feeddatapublished` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP)
当用数据更新其余列时,它可以使用以下代码正常更新
$stmt = $dbh->prepare("INSERT INTO `feedsdata` (`id`, `feedid`, `feedurl`, `feedsummary`, `feedtitle`, `feeddatapublished`) VALUES (NULL, :feed_id, :feed_url, :feed_summary, :title, :datapublished)");
$stmt->bindParam(':feed_id', $feed_id);
$stmt->bindParam(':feed_url', $feed_url);
$stmt->bindParam(':feed_summary', $feed_summary);
$stmt->bindParam(':title', $feed_title);
$stmt->bindParam(':datapublished',$datepublished);
$stmt->execute();
在将它传递给插入之前,我从 feed 中转换了字符串
$datepublished = strtotime(scrape_between($separate_result, "<span class=\"date\">", "</span>"));
scrape_between 是我用于抓取的函数。
回显 $datepublished 时,我得到时间戳 1458155700,这不是我所看到的正确时间戳。
所有其他列都按要求更新,唯一不是发布日期的列。
我的两个问题是
- 它没有更新的原因是因为我将格式错误的时间戳传递给 mysql 数据库
- 我怎样才能从上面的格式生成更好的时间戳,我已经检查了日期函数,但我似乎无法让它工作。
最佳答案
MySQL timestamp
格式为 2016-02-13 15:48:29
或 Y-m-d H:i:s
转换你的 unix timestamp
首先转换为该格式,然后 MySQL 将接受它。
要么用
<?php
$datapublished = date("Y-m-d H:i:s", strtotime(scrape_between($separate_result, "<span class=\"date\">", "</span>")));
或
你的问题
$stmt = $dbh->prepare("INSERT INTO `feedsdata` (`id`, `feedid`, `feedurl`, `feedsummary`, `feedtitle`, `feeddatapublished`)
VALUES (NULL, :feed_id, :feed_url, :feed_summary, :title, from_unixtime(:datapublished))");
关于php - strtotime 不插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35516488/