简体   繁体   中英

Compare difference between two dates PHP

I am trying to compare to date to figure out how much time is between them, which I know how to do, date_diff(), but I want to then compare the time between the dates and if it is greater than 7 days do something and if not do something else. I think it sounds easy and I know there are probably fairly simple solutions to do so but I am just not a fan of dates and comparisons. Here is a snippet of what I got so far as it is just one case of a switch statement so the rest are basically identical.

$array = array();
$today = date("Y-m-d"); // get today's date

foreach($arrayOfObjs as $obj){
    if ($obj->get("renewalDate") >= $today){
        array_push($array, $obj->get("renewalDate"));
    }else{
        switch($obj->get("recurrencePeriod")){
            case 1:
       /*
       * All cases follow same structure
       * Build the date in format Y-m-d from renewalDate out of the obj.
       * Loop through the date while it's less than today.
       * After date is greater than today return date add to array
       */

       $date = DateTime::createFromFormat("Y-m-d", $obj->get('renewalDate'));
       while($date <= $today){
          $date->add(new DateInterval('P7D'));
        }
        $diff = date_diff($today, $date);
        if($diff->format('%a') <= 7){
          $obj->renewalDate($date);
          array_push($array, $obj);
        }
        break;

Basically, my database stores dates and those dates could be passed but it could be a reoccurring event. To calculate the next time that event would happen I check if the data in the database is before today's date and if it is then I continue to add the incremental amount (in this case 7 for a weekly reoccurring event) and compare the dates again. After the date that is incremented passes today's date I want to find out if it is within 7 days and if so add it to an array to get returned. I know... since I'm adding 7and it's within 7 days the first reoccurring event will always be within 7 days but that is not the case for monthly events or anything greater.

All cases are broken so I only included this one for simplicity. I can get date_Diff to return something like 7 or 12 or whatever the number may be but how can I check if that number is within the 7 days I want?

Thanks, I will include more information if needed to clarify any misunderstandings.

I'm not entirely sure what you are trying to do, but how about the following if you are just projecting dates forward and backwards and want to know if they are 7 days or more either way:

$today = date("Y-m-d");
$todaytime = strtotime($today);

$testdate = "2017-06-31";
$testtime = strtotime($testdate);

$back7days = strtotime("-7 days",$todaytime);
if($testtime < $back7days)
    echo "X"; // do somthing if testdate was more than 7 days ago


$fwd7days = strtotime("+7 days", $todaytime);
if($testtime > $fwd7days)
    echo "Y"; // do somthing if testdate is more than 7 days in future  

Just make sure that you use less-than or less-than-and-equals comparators etc to handle the boundary conditions you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM