dougu3290 2015-09-29 16:17
浏览 23

从SQL检索到的PHP日期的比较运算符无法正常工作

I've created an If an Else Statement but it doesn't work properly. I have some dates within my SQL which have been retrieved and stored in variables using PHP.

I'm comparing the current date with the dates from the database but for some reason, it thinks for example that 29-09-2015 if LESS THAN 31-01-2015.

I can understand that the format could be the issue d,m,Y but I thought I'd corrected that already. Here's the code:

$today = date('d-m-Y');
$date = $row['respondby'];
$euDate= date("d-m-Y", strtotime($date));

<?php 


if($today < $euDate){                                                           echo "<td>". $today." is less than ". $euDate ."</td>";
}
else{
echo"<td>Lost somewhere in between ?!?!?! :S </td>";
}
?>

As a result it prints 29-09-2015 is less than 30-06-2015

today's date was 29-09-2015 and one of the dates was in the data was this one as shown.

Thank you everyone that helps.

  • 写回答

1条回答 默认 最新

  • dongluo1853 2015-09-29 16:19
    关注

    Comparison of dates as strings uses lexicographical order, hence your result is "correct".

    Instead of d-m-Y format, try to use Y-m-d, this guarantees proper ordering.

    $today = date('Y-m-d');
    $date = $row['respondby'];
    $euDate= date("Y-m-d", strtotime($date));
    if($today < $euDate) { [...] }
    

    Or, you can use Date objects instead:

    $today = new Date('now');
    $euDate= new Date($row['respondby']);
    if($today < $euDate) { [...] }
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP