0

I am trying to output a number of months using days(value fro $daydiff).

Whole code goes like:

$ArrivalDate = $variants_data['ArrivalDate'];

$daydiff=floor((abs(strtotime(date("Y-m-d")) - strtotime($ArrivalDate))/(60*60*24)));


if (!empty($daydiff) &&  $ArrivalDate == '2013-12-25') {
    $ETA ='Date Not Confirmed';
} 

elseif (!empty($daydiff) &&  is_null($ArrivalDate)) {
    $ETA ='Not available';
}

elseif ( $daydiff > 30 && $daydiff < 60 ) { // anything between 31 and 59 days is 1 month
    $ETA ='1 Month';
}

elseif ( $daydiff > 60 &&  $daydiff < 90  )  { // anything between 61 and 89 days is 2 months
    $ETA ='2 Months';
}

else
{
    $ETA ='';
}

This section of code doesn't give me required results

elseif ( $daydiff > 30 && $daydiff < 60 ) { // anything between 31 and 59 days is 1 month
    $ETA ='1 Month';
}

elseif ( $daydiff > 60 &&  $daydiff < 90  )  { // anything between 61 and 89 days is 2 months
    $ETA ='2 Months';
}

else
{
$ETA ='';
}

I get 'Date Not Confirmed' as results. Required result is '1 month' or '2 months' if the $daydiff value falls in the range.

What am I doing wrong here?

Mlungisi
  • 51
  • 2
  • 10
  • Can you give some example output of $daydiff? Seems like $ArrivalDate is always "2013-12-25" so the first if-statement is true – Chris Apr 10 '13 at 10:21
  • So your if statement `!empty($daydiff) && $ArrivalDate == '2013-12-25'` is evaluating as true, so `$daydiff` is probably not empty and `$ArrivalDate` is in fact equal to `2013-12-25`. Please check these values because without them debugging your code for us is impossible – Pankrates Apr 10 '13 at 10:24
  • $daydiff = 2013-05-05 on some products and I dont know why it defaults to 'Date Not Confirmed' – Mlungisi Apr 10 '13 at 10:27
  • Are you sure you're fetching the ``$ArrivalDate`` correctly? – Chris Apr 10 '13 at 10:32
  • @Chris nice, figured it out, the testing dB was not updating and i referenced with our ERP system AX. So far all works fine now. Thank you all for your help. – Mlungisi Apr 10 '13 at 10:54

2 Answers2

0

try

    $ArrivalDate = $variants_data['ArrivalDate'];

if (!empty($ArrivalDate)){
    $ArrivalDate = date('Y-m-d', strtotime($ArrivalDate));

    $daydiff=floor(abs(strtotime(date("Y-m-d")) - strtotime($ArrivalDate)) / (60*60*24));

    echo $daydiff;

    if ($ArrivalDate == '2013-12-25') {
        $ETA ='Date Not Confirmed';
    }
    elseif ( $daydiff > 30 && $daydiff < 60 ) { // anything between 31 and 59 days is 1 month
        $ETA ='1 Month';
    }

    elseif ( $daydiff > 60 &&  $daydiff < 90  )  { // anything between 61 and 89 days is 2 months
        $ETA ='2 Months';
    }
    else
    {
        $ETA ='';
    }
} else {
    $ETA ='Not available';
}
0

try this here is simple example to count date difference in month with no extra if else conditions

<?php

$date1=date('2013-1-1');
$date2=date('2013-5-2');

echo "date1 :: ".$date1."<br>";
echo "date2 :: ".$date2."<br>";

$date_diff=strtotime($date2)-strtotime($date1);

echo "date difference in months => ".floor(($date_diff)/2628000)." months <br>";
?>
liyakat
  • 11,825
  • 2
  • 40
  • 46