dsirr48088 2016-12-30 17:27
浏览 14
已采纳

PHP - 如何比较此条件语句中的日期/持续时间

I have some code that allows users to input the date of their 'first exam', compares that date to today's date and displays a warning/notification if the difference between those two dates is a certain amount (as the rest of their exams must be completed within 18 months of their first exam being taken). I'm having trouble working out how to compare the dates/difference between the dates in the conditional statement - in other words, how can I compare $difference to "18 months" or "2 months", etc. If anyone could point me in the right direction it would be greatly appreciated.

Code:

<?php

// Set first exam date
$firstexamdate = "2015-08-20";

// Work out date that is 18 months from first exam date
$add_18_months = strtotime($firstexamdate . ' + 18 months');
$eighteen_months_time = date('Y-m-d',$add_18_months);

// Check
echo "Date of first exam: " . $firstexamdate . "<br>";
echo "18 months from this date: " . $eighteen_months_time . "<br>";

// Work out diff between cut-off date and today
$today = date('Y-m-d');
$date1 = new DateTime($eighteen_months_time);
$date2 = new DateTime($today);
$difference = $date1->diff($date2);

// Display
echo "Difference/time remaining: " . $difference->y . " years, " . $difference->m . " months, " . $difference->d . " days " . "<br";

// BELOW CODE NOT WORKING

// Display $difference as string
echo $difference->format('Y-m-d');

// Output correct warning colour
if ($difference <=  /* 18 months */ and >= /* 6 months */ {
    echo "Green warning: At least 6 months left.";
} elseif ($difference  <=  /* 5 months */ and >= /* 2 months */ {
    echo "Orange warning: Less than 5 months left.";
} elseif ($difference <=  /* 1 months */ and >= /* 0 months */ {
    echo "Red warning: Less than one month left.";
}

?>
  • 写回答

2条回答 默认 最新

  • dsbckxk165039 2016-12-30 17:45
    关注

    You can use the days property of your $difference object:

    if ($difference->days <=  (18 * 30) && $difference->days >= (6 * 30) {
        echo "Green warning: At least 6 months left.";
    }
    

    About property days in Php docs:

    If the DateInterval object was created by DateTime::diff(), then this is the total number of days between the start and end dates. Otherwise, days will be FALSE.

    http://php.net/manual/en/class.dateinterval.php

    EDIT

    If you need 6 and 18 months exactly you could use this approach:

    $targetDate = new DateTime('20170529'); //here your target date;
    $now = new DateTime(); //by default today;
    $min = clone $targetDate;
    $min->add(new DateInterval('P6M'));
    $max = clone $targetDate;
    $max->add(new DateInterval('P18M'));
    if($now >= $min && $now <= $max){
    // your code here
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。