dongtu1357 2017-05-01 01:04
浏览 14
已采纳

在计算日期时正确使用PHP和MySQLi查询

I am new to PHP / MySQLi and having a little problem and wondering if someone can see what i am doing wrong.

I have 2 fields in question, invoice_date and invoice_due_date which are formatted within the database like "15/03/2017 and 12/04/2017"

What i am trying to do is check invoice_due_date in the query and if date is less than current date to return those items but cannot seem to crack it... here are the few queries i have tried.

$query = "SELECT SUM(`total`) as `total` FROM invoices WHERE invoice_due_date < DATE_FORMAT(now(),'%d/%m/%Y') AND invoice_type = 'invoice' AND status = 'open'";
$query = "SELECT SUM(`total`) as `total` FROM invoices WHERE invoice_due_date < DATE_FORMAT(NOW(),'%d/%m/%Y') AND status = 'open'";

Was assuming would need to convert NOW() to format it needs to compare from to work correctly but clearly im missing something haha

DATE_FORMAT(now(),'%d/%m/%Y')
  • 写回答

1条回答 默认 最新

  • doubo6658 2017-05-01 01:11
    关注

    Set your dates to be ISO-8601 dates, YYYY-MM-DD in a DATE column. You cannot compare values like this. It's total nonsense because the fields are not in the right order.

    Look at how these sort:

    2011-02-01
    2012-01-01
    2015-01-02
    

    Versus this which may as well be random even though "ordered":

    01/01/2012
    01/02/2011
    02/01/2015
    

    Keep your data in the most neutral format possible and apply any necessary date formatting in your display logic.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决