php - How to find the difference between campaign dates and filter date -
i have 4 dates me, 2 campaign 1 of them campaign start date , other 1 campaign end date , same thing goes filter dates. want check how many days campaign runs in between given filter start date , filter end date . using these conditions
if ($datecs != false && $datece != false && $datefs != false && $datefe != false) { if( ($datecs >= $datefs) && ($datece <= $datefe)) { // campaign start , end date between filterdates $tot_days = $this->get_days($datecs,$datece); if($tot_days != 'ok'){ return $tot_days; } else { return 0; } } elseif (($datecs <= $datefs) && ($datece >= $datefe)) { //filterdates between campiagn start , end $tot_days = $this->get_days($datefs,$datefe); if($tot_days != 'ok'){ return $tot_days; } else { return 0; } } elseif(($datecs <= $datefs ) && ( $datece <= $datefe ) && ($datece >= $datefs)) { // campaign end date between filterdates $tot_days = $this->get_days($datefs,$datece); if ($tot_days != 'ok'){ return $tot_days; } else { return 0; } } elseif(( $datecs>= $datefs) && ($datece >= $datefe) && ($datecs <= $datefe )) { // campaign start date between filterdates $tot_days = $this->get_days($datecs,$datefe); if (($tot_days != 'ok')) { return $tot_days; } else { return 0; } } }
datecs campaign start date , datece campaign end date .datefs filter start date , datefe filter end date. , get_days function gets me number of days get_days function using
function get_days($date1,$date2) { if($date2 > $date1) { $interval = $date1->diff($date2); return $interval->days+1; //we add 1 because not calculate starting date if substract. //it give number of days reach last date. } else if($date2 = $date1) { return 1; } else { return "ok"; } }
please let me know if going wrong where.
i rewrite get_days function :
function get_days($date1,$date2) { if($date2 < $date1) { $interval = $date1->diff($date2); return $interval->days; } else if($date2 == $date1) { return 1; } else { return 0; } }
i don't know if want found 2 mistakes :
1) using =
, not ==
compare
2) swapped in comparaison : it's $date2 < $date1
also it's better return 0 failed> try not mix return type in same function, make code easier understand.
this worked me.
edit
in second condition (second line) doing :
if ($datecs >= $datefs) { get_days($datecs,$datece); }
and if function get_days : if ($date2 > $date1)
that means doing
if ($datecs >= $datefs) if ($datecs < $datefs) //do stuff
Comments
Post a Comment