strtotime未插入数据库 [英] strtotime not inserting into database
问题描述
因此,我要在网站上抓取数据,而其中一项数据是要抓取某些项目的日期.
商品日期采用"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();
我在从提要中转换了字符串,然后将其传递给
进行插入.$datepublished = strtotime(scrape_between($separate_result, "<span class=\"date\">", "</span>"));
scrape_between是我用于抓取的功能.
当呼出$ date发布时,我得到时间戳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>")));
OR
您对
的查询$stmt = $dbh->prepare("INSERT INTO `feedsdata` (`id`, `feedid`, `feedurl`, `feedsummary`, `feedtitle`, `feeddatapublished`)
VALUES (NULL, :feed_id, :feed_url, :feed_summary, :title, from_unixtime(:datapublished))");
So Im scraping a website for data, and one piece of data that im scraping is the date of certain items.
The date of the items comes in the format "Wed 11th March, 2015".
I have been trying to then insert this into my mysql database. The structure of the database contains a column with "datapublished" as a Timestamp,
`feeddatapublished` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP)
When updating the rest of the columns with the data it updates fine with the following code
$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();
I converted the string from the feed before passing it to be inserted with
$datepublished = strtotime(scrape_between($separate_result, "<span class=\"date\">", "</span>"));
scrape_between is a function I use for the scraping.
When echoing out the $datepublished I get the timestamp 1458155700, which isnt the correct timestamp from what i can see.
All other columns are updating as required, the only one which isnt is the datepublished one.
My two questions are
- Is the reason its not updating because im passing a malformed timestamp to the mysql database
- How can I generate a better timestamp from the format above, Ive checked the date function but I cant seem to get it to work.
The MySQL timestamp
format is 2016-02-13 15:48:29
or Y-m-d H:i:s
convert your unix timestamp
to that format first, and then MySQL will accept it.
Either with
<?php
$datapublished = date("Y-m-d H:i:s", strtotime(scrape_between($separate_result, "<span class=\"date\">", "</span>")));
OR
your query to
$stmt = $dbh->prepare("INSERT INTO `feedsdata` (`id`, `feedid`, `feedurl`, `feedsummary`, `feedtitle`, `feeddatapublished`)
VALUES (NULL, :feed_id, :feed_url, :feed_summary, :title, from_unixtime(:datapublished))");
这篇关于strtotime未插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!