0

我有以下代码,我想获取存储的时间戳$begin并将其传递给 MySQL 查询,但查询失败:

Fatal error: Unsupported operand types

这是用于填充变量的代码$begin

$datepicker_begin = "01/07/2013";
$begin = DateTime::createFromFormat('m/d/Y', $datepicker_begin);
$beginObj = $begin->format('Y-m-d');
$begin = strtotime($beginObj);  // becomes Unix Timestamp

这是查询的代码。它正在检查日期列,它是一种date类型并被调用date,以查看表中是否存在日期。

// Check if chosen date is available.
$s=$dbh->prepare("
    SELECT DISTINCT
        `date`
    FROM
        `report_coa_bal_hist`
    WHERE
        UNIX_TIMESTAMP(date) = ?
");

if ($s->execute($begin)) {

    return true;

} else {

    return false;

}
4

3 回答 3

0

strtotime在 Unix 时间戳中创建日期

你必须直接使用这个变量而不做strtotime

$begin = strtotime($beginObj);

编辑

将此查询与`周围一起使用date

$s=$dbh->prepare("
SELECT DISTINCT
    `date`
FROM
    `report_coa_bal_hist`
WHERE
    UNIX_TIMESTAMP(`date`) = ?");

if ($s->execute($begin)) {

    return true;

} else {

    return false;

}
于 2013-05-08T04:21:42.220 回答
0

您当前的代码应该可以正常工作...但是您也可以尝试一下

$datepicker_begin = "01/07/2013";
$begin = DateTime::createFromFormat('m/d/Y', $datepicker_begin);
echo $begin = $begin->getTimestamp();;  // becomes Unix Timestamp

看看http://php.net/manual/en/datetime.gettimestamp.php

于 2013-05-08T04:23:38.903 回答
0

尝试 FROM_UNIXTIME:

$datepicker_begin = "01/07/2013";
$begin = DateTime::createFromFormat('m/d/Y', $datepicker_begin);
$beginObj = $begin->format('Y-m-d');
$begin = strtotime($beginObj);  // becomes Unix Timestamp
$sql = "INSERT INTO test(ID, time) VALUES(NULL, FROM_UNIXTIME('$begin'));";
mysql_query($sql);
于 2013-05-08T04:29:04.727 回答