HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Displaying content based on the current time

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thetimedisplayingcurrentbasedcontent

Problem

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.


= $breakfastStart && $currentTime 
        
        Breakfast
        
            
        
        
           
        
        
            Served
        

= $lunchStart && $currentTime 
        
        Lunch
        
            
        
        
           
        
        
            Served
        

= $dinnerStart && $currentTime 
        
        Dinner
        
            
        
        
           
        
        
            Served
        

        
        Late Night
        
            
        
        
           
        
        
            Served
        

Solution

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

= $breakfastStart && $currentTime = $lunchStart && $currentTime = $dinnerStart && $currentTime 
    
    
        
    
    
       
    
    
        Served
    

<?php
}

Code Snippets

<?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 >= $breakfastStart && $currentTime <= $breakfastEnd )
{

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

    // if not, is it lunch time?
} elseif( $currentTime >= $lunchStart && $currentTime <= $lunchEnd )
{

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

    // if not, is it dinner time?
} elseif( $currentTime >= $dinnerStart && $currentTime <= $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 )
{
?>
    <h4 class="ribbon"><?=$field_title?></h4>
    <h1>
        <?php echo $rows[ $i ]['dish_name']; ?>
    </h1>
    <p>
       <?php echo $rows[ $i ]['dish_description'];  ?>
    </p>
    <h3>
        Served<span><?php echo $rows[ $i ]['time_served'];  ?></span>
    </h3>

<?php
}

Context

StackExchange Code Review Q#57011, answer score: 4

Revisions (0)

No revisions yet.