Aaron Aaron - 23 days ago 5
PHP Question

Trouble with inarray

So I'm trying to create something that will check the date and produce a different variable result depending on the date today.

this is my current code:

<?php
for($i = 0; $i <= 25; $i++)
$dates[] = date("d", strtotime( "+$i days"));
foreach ($dates as $today) {
if (in_array($today, array('01', '05', '09', '14', '19', '24'), true)) {
$tweet = "one";
}
if (in_array($today, array('02', '06', '10', '15', '20', '25'), true)) {
$tweet = "two";
}
if (in_array($today, array('03', '07', '11', '16', '21'), true)) {
$tweet = "three";
}
if (in_array($today, array('04', '08', '12', '17', '22'), true)) {
$tweet = "four";
}
}
echo $tweet;
?>


Problem is that even if the date changes it always echos "four", what am i missing?

Answer

Because you overwrite $tweet in each iteration of the loop, it means only the last iteration is actually available. The last iteration will be today + 25 days, which would be the 12th December.

12 causes the assignment of $tweet to four and since that's the last iteration, that's what $tweet be be equal to after the loop ends.