Given this PHP for
loop
$row->frequency = 1;
$row->date_1 = 10000;
$row->interval = 86400;
for ($i = 0; $i <= $row->frequency; $i++) {
$cal_data[] = array(
'start' => strtotime($row->date_1) + $row->interval,
);
}
I would like the first iteration of the loop to ignore the + $row->interval
giving me as a result:
10000
96400
I've seen this done with modulus but couldn't manage to make it work here. Anyone have suggestions?
Thanks!
Use + ($i ? $row->interval : 0)
In other words, if $i
is zero - first iteration - add 0 instead of $row->interval
. This is the ternary operator, (condition ? iftrue : iffalse)
which is roughly equivalent to an if/else
construction except it can be used amidst a statement.
for ($i = 0; $i <= $row->frequency; $i++) {
if ($i == 0) {
$val = strtotime($row->date_1) ;
} else {
$val = strtotime($row->date_1) + $row->interval;
}
$cal_data[] = array(
'start' => $val
);
}