doufei8691 2014-12-11 07:26 采纳率: 100%
浏览 26

如何在比较两个日期时解决问题?

I've written following code to compare a date in a variable with today's date. If the date in a variable is greater than today's date the error message should be echoed. But it's not working properly in my case.

$form_data['trans_date'] = '12-11-2014';//Date to be compared with today's date in mm-dd-yyyy format
if(strtotime(date('m-d-Y')) < strtotime($form_data['trans_date'])) {
        echo 'Error';
    } else {
        echo 'Success';
    }

Actually today's date is 12-11-2014 i.e. 11th December 2014 and I'm indirectly comparing today's date with itself. So I should not get an error message but I'm getting the error message.

I tried to debug the code by printing timestamp values then I got following values :

Today's date 1413097200
Trans date 1415779200

Actually since the Trans Date timestamp value is slightly greater than timestamp value of Today's date the error is coming.

But it shouldn't since The date I'm comparing is not greater than today's date.

So how should I resolve this issue?

  • 写回答

2条回答 默认 最新

  • 普通网友 2014-12-11 07:36
    关注

    You can use simplePHP class to compare dates:

    include('../code/simpleDate.php');
    
    $date = new simpleDate();
    
    echo $date->set('2014-11-12')->compare('now')->isAfterOrEqual(); 
    

    Please check tutorials page.

    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据