doubi8512 2016-05-24 15:35 采纳率: 0%
浏览 20

在PHP中检查mySQL日期是否过去的“最佳”方法是什么?

So I have a database which stores a date-time value. I want to find out if the date is in the past - I don't care about the time part in this instance, just the date.

So I have the following code at the moment

//this is the current date
$today = strtotime(date("Y-m-d"));
//this is the date from the database
$gameDate = strtotime($rec[0])
if (strtotime($rec[0]) < $today) {

I feel like I am not doing this in the most accepted way, but my brain hurts from too much data and I was hoping someone could give me a pointer as to the 'correct'approach.

  • 写回答

1条回答 默认 最新

  • douxun7992 2016-05-24 16:00
    关注

    Assuming you only care about the Date part, and not the time part, as in 2016-01-01 14:00:00 and 2016-01-01 15:00:00 would be counted as the same day and not the past, you can use the following:

    $dateFromDatabase = new DateTime('2016-05-23 17:00:00'); // $rec[0] in your case
    $current = new DateTime();
    
    $difference = $current->diff($dateFromDatabase);
    
    if ($difference->format('%R') === '-') {
        echo 'Date is in the past';
    }
    

    ideone can be found here

    Any issues let me know.

    评论

报告相同问题?

悬赏问题

  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。