donglun7151 2015-05-16 16:33
浏览 59

PHP / MYSQL比较日期

I have a date in php formatted like this:

Fri May 01 2015 17:25:00 GMT +0100 (GMT Daylight Time)

And Im trying to compare it to a mysql DATETIME field:

$date1 = DateTime::createFromFormat("Y-m-d H:i:s e+", $start);
$sql = "SELECT * FROM ax_timestamps WHERE $date1 < datetimefeild ORDER BY id ASC";

But when it executes it returns 0 results even though based in the input and elements in the database it should pull results.

example of data in the datetimefeild feild:

2015-05-16 07:44:56

The date is being passed to php via ajax post from jQuery datepicker which is setup like this:

$("#datepicker").datepicker({ dateFormat: 'yy-mm-dd' });
var start = $.datepicker.parseDate("yy-mm-dd", $('#datepicker').val());
  • 写回答

2条回答 默认 最新

  • drbouzlxb92333332 2015-05-16 17:40
    关注

    You are passing a the date in wrong format (as string). You should first convert it to datetime type in the where clause using str_to_date() function.

    Example:

    SELECT str_to_date('2015-05-01 08:00:00', '%Y-%m-%d %h:%i:%s')
    

    Applying it, your $sql string would become:

    $sql = "SELECT * FROM ax_timestamps WHERE datetimefeild < str_to_date('".$date1."', '%Y-%m-%d %h:%i:%s') ORDER BY id ASC";
    

    EDIT: this also works:

    $sql = "SELECT * FROM ax_timestamps WHERE datetimefeild < '".$date1."' ORDER BY id ASC";
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度