I'm sorry i'm currently having a problem on this section. My Date format is Y-m-d
$date1 = '2016-03-1';
$date2 = '2016-07-1';
$date3 = '2016-10-1';
$date4 = '2016-12-1';
if today is 2016-8-12 it should fall at $date3 since it the next period. i tried using the if conditions but it only falls at first condition. Can you enlighten me on this?
My code is somewhat like this
$today = date("2016-07-29");
if(strtotime($date1) < strtotime($today)){
$next_accrual_date = $date1;
$date_condition = 'condition 1';
}else
if(strtotime($date2) < strtotime($today)){
$next_accrual_date = $date2;
$date_condition = 'condition 2';
}else
if(strtotime($date3) < strtotime($today)){
$next_accrual_date = $date3;
$date_condition = 'condition 3';
}else
if(strtotime($date4) < strtotime($today)){
$next_accrual_date = $date4;
$date_condition = 'condition 4';
}
echo $next_accrual_date." falls to ".$date_condition;
One way to do this is
<?php
$dates = array();
$dates[] = '2016-03-1';
$dates[] = '2016-07-1';
$dates[] = '2016-10-1';
$dates[] = '2016-12-1';
usort($dates, "cmp");
function cmp($a, $b){
return strcmp($a, $b);
}
foreach($dates as $date){
if($date > "2016-08-12"){
echo $date;
break;
}
}
//print_r($dates);
?>
Live demo : https://eval.in/621460
This will give output :
2016-10-1
$today = strtotime(date('Y-m-d'));
$currentYear = date('Y');
$dates = ['q1' => strtotime($currentYear.'-03-1'),
'q2' => strtotime($currentYear.'-07-1'),
'q3' => strtotime($currentYear.'-10-1'),
'q4' => strtotime($currentYear.'-12-1')];
foreach ($dates as $qName => $qDate) {
if ($qDate > $today) {
return "$qDate falls to $qName";
}
}
Just Change your comparison from < to >:
$today = date("2016-07-29");
if(strtotime($date1) > strtotime($today)){
$next_accrual_date = $date1;
$date_condition = 'condition 1';
}else ...
echo $next_accrual_date." falls to ".$date_condition;
And it will work. currently you look if today is bigger than the date of the first quater, and then break the if. But every date after the '2016-03-1' will be true for this condition. That's why you always get condition 1.