Displaying content based on the current time I'm new to PHP and just wrote a bunch of if statements to display content based on the current time.

Is there a better way of writing the following block of code to easily be maintainable? The only thing that changes in each if statement is the if variables, h4 tag and the $rows variable.

&lt;?php

    date_default_timezone_set("Europe/London");

    $currentTime = date('Hi');
    $breakfastStart = "0200";
    $breakfastEnd = "1129";

    $lunchStart = "1130";
    $lunchEnd = "1559";

    $dinnerStart = "1600";
    $dinnerEnd = "2129";

    $lateStart = "2130";
    $lateEnd = "0159";

?&gt;

&lt;?php if ($currentTime &gt;= $breakfastStart &amp;&amp; $currentTime &lt;= $breakfastEnd): ?&gt;
        &lt;?php
            $rows = get_field('breakfast', 41190);
            $row_count = count($rows);
            $i = rand(0, $row_count - 1);
        ?&gt;
        &lt;h4 class="ribbon"&gt;Breakfast&lt;/h4&gt;
        &lt;h1&gt;
            &lt;?php echo $rows[ $i ]['dish_name']; ?&gt;
        &lt;/h1&gt;
        &lt;p&gt;
           &lt;?php echo $rows[ $i ]['dish_description'];  ?&gt;
        &lt;/p&gt;
        &lt;h3&gt;
            Served&lt;span&gt;&lt;?php echo $rows[ $i ]['time_served'];  ?&gt;&lt;/span&gt;
        &lt;/h3&gt;
&lt;?php endif; ?&gt;

&lt;?php if ($currentTime &gt;= $lunchStart &amp;&amp; $currentTime &lt;= $lunchEnd): ?&gt;
        &lt;?php
            $rows = get_field('lunch', 41190);
            $row_count = count($rows);
            $i = rand(0, $row_count - 1);
        ?&gt;
        &lt;h4 class="ribbon"&gt;Lunch&lt;/h4&gt;
        &lt;h1&gt;
            &lt;?php echo $rows[ $i ]['dish_name']; ?&gt;
        &lt;/h1&gt;
        &lt;p&gt;
           &lt;?php echo $rows[ $i ]['dish_description'];  ?&gt;
        &lt;/p&gt;
        &lt;h3&gt;
            Served&lt;span&gt;&lt;?php echo $rows[ $i ]['time_served'];  ?&gt;&lt;/span&gt;
        &lt;/h3&gt;

&lt;?php endif; ?&gt;

&lt;?php if ($currentTime &gt;= $dinnerStart &amp;&amp; $currentTime &lt;= $dinnerEnd): ?&gt;
        &lt;?php
            $rows = get_field('dinner', 41190);
            $row_count = count($rows);
            $i = rand(0, $row_count - 1);
        ?&gt;
        &lt;h4 class="ribbon"&gt;Dinner&lt;/h4&gt;
        &lt;h1&gt;
            &lt;?php echo $rows[ $i ]['dish_name']; ?&gt;
        &lt;/h1&gt;
        &lt;p&gt;
           &lt;?php echo $rows[ $i ]['dish_description'];  ?&gt;
        &lt;/p&gt;
        &lt;h3&gt;
            Served&lt;span&gt;&lt;?php echo $rows[ $i ]['time_served'];  ?&gt;&lt;/span&gt;
        &lt;/h3&gt;

&lt;?php endif; ?&gt;

&lt;?php if ($currentTime &lt;= $lateStart &amp;&amp; $currentTime &lt;= $lateEnd): ?&gt;
        &lt;?php
            $rows = get_field('late_night', 41190);
            $row_count = count($rows);
            $i = rand(0, $row_count - 1);
        ?&gt;
        &lt;h4 class="ribbon"&gt;Late Night&lt;/h4&gt;
        &lt;h1&gt;
            &lt;?php echo $rows[ $i ]['dish_name']; ?&gt;
        &lt;/h1&gt;
        &lt;p&gt;
           &lt;?php echo $rows[ $i ]['dish_description'];  ?&gt;
        &lt;/p&gt;
        &lt;h3&gt;
            Served&lt;span&gt;&lt;?php echo $rows[ $i ]['time_served'];  ?&gt;&lt;/span&gt;
        &lt;/h3&gt;
&lt;?php endif; ?&gt;
You are repeating a lot of HTML, which I guess you know could be condensed down, you can also make use of elseif this way it will execute the first TRUE statement and skip the rest.

If there are no matches, for breakfast, lunch, dinner we can assume it must be late night, as there are no more hours left in the day. As this is the case we can just } else { for the last statement.

I have added a check for $row_count as we can never guarantee there will be results returned

&lt;?php

date_default_timezone_set("Europe/London");

$currentTime = date('Hi');
$breakfastStart = "0200";
$breakfastEnd = "1129";

$lunchStart = "1130";
$lunchEnd = "1559";

$dinnerStart = "1600";
$dinnerEnd = "2129";

$lateStart = "2130";
$lateEnd = "0159";

// set up your variables
$rows = $field_key = $field_title = false;

// is it breakfast time?
if( $currentTime &gt;= $breakfastStart &amp;&amp; $currentTime &lt;= $breakfastEnd )
{

    // if it is, set the field key and title
    $field_key = 'breakfast';
    $field_title = 'Breakfast';

    // if not, is it lunch time?
} elseif( $currentTime &gt;= $lunchStart &amp;&amp; $currentTime &lt;= $lunchEnd )
{

    $field_key = 'lunch';
    $field_title = 'Lunch';

    // if not, is it dinner time?
} elseif( $currentTime &gt;= $dinnerStart &amp;&amp; $currentTime &lt;= $dinnerEnd )
{

    $field_key = 'dinner';
    $field_title = 'Dinner';

    // if not, lets assume its late night
} else {

    $field_key = 'late_night';
    $field_title = 'Late Night';

}

$rows = get_field( $field_key , 41190);
$row_count = count($rows);
$i = rand(0, $row_count - 1);

// if we have some rows..
if( $row_count )
{
?&gt;
    &lt;h4 class="ribbon"&gt;&lt;?=$field_title?&gt;&lt;/h4&gt;
    &lt;h1&gt;
        &lt;?php echo $rows[ $i ]['dish_name']; ?&gt;
    &lt;/h1&gt;
    &lt;p&gt;
       &lt;?php echo $rows[ $i ]['dish_description'];  ?&gt;
    &lt;/p&gt;
    &lt;h3&gt;
        Served&lt;span&gt;&lt;?php echo $rows[ $i ]['time_served'];  ?&gt;&lt;/span&gt;
    &lt;/h3&gt;

&lt;?php
}