dongtui4038 2013-03-15 16:53
浏览 24

Codeigniter 2.1,MySQL - 按日期搜索

In my DB I have date field with a standard datetime for MySQL (YYYY-MM-DD HH:MM:SS). Trough AJAX request I am getting this time: DD/MM/YYYY

This is the function I had written to search trough DB:

$this->db->get_where('tvprogram', array('DATE(date)' => $date))->result_array();

At the moment this function isn't getting any data. What seems to be wrong here? How can I search and find all entries for specific day?

  • 写回答

3条回答 默认 最新

  • dongyi9082 2013-03-15 17:12
    关注

    Check the format coming out of DATE(date) because it is probably like YYYY-MM-DD and the comparison will always fail.

    Go into phpMyAdmin or similar and try SELECT DATE(now()) and double check the date format.

    If $date has a format like DD/MM/YYY, then you can reformat it by using:

    $date=date('Y-m-d',strtotime(strtr($date,"/","-")));
    

    From the PHP manual (http://www.php.net/manual/en/function.strtotime.php):

    Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

    To fix the American/European date ambiguity, I simply replace "/" with "-" before the string to time conversion.

    评论

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误