Display PHP object depending on day of the week

So I have the hours of a business set up in 7 different objects each for a day of the week. I wanted to be able to display in the header the hours of this business for the day that it currently is. Set it up like this but it won't spit anything out

<?php
    $d=date("D");
    if ($d=="Mon")
    echo <?php echo of_get_option('monday', 'no entry'); ?>;
    elseif ($d=="Tue")
    echo <?php echo of_get_option('tuesday', 'no entry'); ?>;
    elseif ($d=="Wed")
    echo <?php echo of_get_option('wednesday', 'no entry'); ?>;
    elseif ($d=="Thu")
    echo <?php echo of_get_option('thursday', 'no entry'); ?>;
    elseif ($d=="Fri")
    echo <?php echo of_get_option('friday', 'no entry'); ?>;
    elseif ($d=="Sat")
    echo <?php echo of_get_option('saturday', 'no entry'); ?>;
    elseif ($d=="Sun")
    echo <?php echo of_get_option('sunday', 'no entry'); ?>;
    else
    echo "Have a nice day!";
?>

Answers


I am assuming you don't have problems with the nested php tags, would be too obvious.

Since the conditions are correct, you should check the function of_get_option(). How does it work, does it return anything? Check with var_dump().

Furthermore it would be way cleaner to use

date("N")

so you get numbers from 1 to 7 for the weekdays.

edit: Ok, in reply to your comment, I think you copypasted these php-tag-parts from somewhere. So please try:

<?php
    $d=date("D");
    if ($d=="Mon")
      echo of_get_option('monday', 'no entry');
    elseif ($d=="Tue")
      echo of_get_option('tuesday', 'no entry');
    elseif ($d=="Wed")
      echo of_get_option('wednesday', 'no entry');
    elseif ($d=="Thu")
      echo of_get_option('thursday', 'no entry');
    elseif ($d=="Fri")
      echo of_get_option('friday', 'no entry');
    elseif ($d=="Sat")
      echo of_get_option('saturday', 'no entry');
    elseif ($d=="Sun")
      echo of_get_option('sunday', 'no entry');
    else
      echo "Have a nice day!";
?>

In your if/else you dont need the else at all. Have a look at the manual for date. The only way date will return false is when you pass in a non-numeric timestamp for the second argument. You are not doing that, so date will return one of the days anyways.

But, instead of using if/else or switch/case (which would be the same), why dont you just use

echo of_get_option(strtolower(date('l')), 'no entry');

Calling date with l will give you the name of the day. strtolower will make it all lowercase. That mapping you are doing there is superfluous. The single line above is equivalent to the entire snippet you show in your question.

On a sidenote, what kind of function name is of_get_option? What is of? Does it stand for office? And if it does, why is that a function rather than a method on an Office object? After all, you said you are using objects. And why does it say get_option when its supposed to display office hours? Shouldn't the name rather be getOfficeHoursOnDay($day) or even better, displayOfficeHoursOnDay($listRenderer, $day). Try to make your code more expressive and readable and try to move responsibilites where they belong.


You certainly don't need to open other PHP tags if one is already open.


It's nice that a solution has been found, but let me post a version that is a bit more readable and there is no unnecessary repetition. It uses an array for mapping purposes.

<?php
    $d=date("D");
    $mapping=array(
        "Mon" => "monday",
        "Tue" => "tuesday",
        "Wed" => "wednesday",
        ...
    );

    if (array_key_exists($d, $mapping)) {
        echo of_get_option($mapping[$d], 'no entry');
    }
    else {
        echo "Have a nice day!";
    }
?>

Try using switch and remove extra tags from code, for example

$d=date("D");
switch ($d) {
case "Mon":
echo of_get_option('monday', 'no entry');
break;
case "Tue":
echo of_get_option('tuesday', 'no entry');
break;
default:
echo "Have a nice day!";
}

I would suggest getting away from using strings at all. Date returns localized strings depensing upon locale settings. So you wind up with a magic string that isn't so magic.

Instead, use the numeric day of week:

 of_get_option(date('w'));

You will of course need to change the function to accept it. But its easier to understand, more portable, and IMHO cleaner...


Need Your Help

Where to find opa api docs

documentation opa

http://doc.opalang.org/api has has been down for several days, is there a mirror

Blackfish SQL management tools

database delphi blackfish database-managers

I started to use Blackfish, but I missed having a good and evolved manager. Is there something already? To help us manage the database and its tables?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.