douduo2407 2014-03-28 19:07
浏览 15

too long

I was wondering if it is possible to use PHP to figure out when a specific date it.

For example:

// Next week
$date->setDate(2014, 4, 3);

if($date->format('Y-m-d') == NEXT_WEEK){
// Do something
}elseif($date->format('Y-m-d') == NEXT_MONTH){
// DO something else
}elseif($date->format('Y-m-d') == NEXT_YEAR){
// Do something else
}

I understand my code won't work, but I am looking for something that would achieve something similar to that?

  • 写回答

3条回答 默认 最新

  • dqp10099 2014-03-28 19:14
    关注

    This can be achieved with DateTime objects using diff to see the time in between, something like this should do the trick:

    // The date we want to check
    $checkDate = new DateTime('2014-04-03', new DateTimeZone('UTC'));
    // We need to compare it against today + a specific interval,
    // so create a seperate DateTime object for today
    $today = new DateTime('now', new DateTimeZone('UTC'));
    
    // Check the difference between the dates
    $diff = $today->diff($checkDate);
    
    if ($diff->y == 0 && $diff->m == 0 && $diff->d == 7) {
        // Date lies a week in the future
    } elseif ($diff->y == 0 && $diff->m == 1 && $diff->d == 0) {
        // Date lies a month in the future
    } elseif ($diff->y == 1 && $diff->m == 0 && $diff->d == 0) {
        // Date lies a year in the future
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了