根据星期几显示PHP对象

时间:2022-04-20 08:17:04

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

因此,我在一周中的每一天都在7个不同的对象中设置了营业时间。我希望能够在标题中显示当前这一天的业务时间。像这样设置,但它不会吐出任何东西

<?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!";
?>

5 个解决方案

#1


-1  

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

我假设你没有嵌套的php标签的问题,太明显了。

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().

由于条件正确,您应该检查函数of_get_option()。它是如何工作的,是否会返回任何东西?检查var_dump()。

Furthermore it would be way cleaner to use

此外,使用起来会更清洁

date("N")

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

所以你可以在工作日得到1到7的数字。

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

编辑:好的,在回复你的评论时,我认为你从某个地方复制了这些php-tag-parts。所以请尝试:

<?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!";
?>

#2


6  

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.

在你的if / else中你根本不需要别的东西。看一下日期手册。 date传递false的唯一方法是传入第二个参数的非数字时间戳。你没有这样做,所以日期将会返回其中一天。

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

但是,不是使用if / else或switch / case(这将是相同的),为什么不用你

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.

用l调用日期将为您提供当天的名称。 strtolower将全部小写。你在那里做的映射是多余的。上面的单行相当于您在问题中显示的整个代码段。

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.

在旁注中,什么样的函数名是of_get_option?什么是?它代表办公室吗?如果是这样,为什么这是Office对象上的函数而不是方法?毕竟,你说你正在使用物体。为什么当它应该显示办公时间时会说get_option?不应该将名称改为getOfficeHoursOnDay($ day)甚至更好,displayOfficeHoursOnDay($ listRenderer,$ day)。尝试使您的代码更具表现力和可读性,并尝试将责任转移到他们所属的位置。

#3


2  

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

如果已打开其他PHP标记,您当然不需要打开它们。


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!";
    }
?>

#4


1  

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!";
}

#5


1  

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.

我建议远离使用字符串。 Date返回根据区域设置设置的本地化字符串。所以你结束了一个不那么神奇的魔法弦。

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...

您当然需要更改功能以接受它。但它更容易理解,更便携,恕我直言清洁......

#1


-1  

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

我假设你没有嵌套的php标签的问题,太明显了。

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().

由于条件正确,您应该检查函数of_get_option()。它是如何工作的,是否会返回任何东西?检查var_dump()。

Furthermore it would be way cleaner to use

此外,使用起来会更清洁

date("N")

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

所以你可以在工作日得到1到7的数字。

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

编辑:好的,在回复你的评论时,我认为你从某个地方复制了这些php-tag-parts。所以请尝试:

<?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!";
?>

#2


6  

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.

在你的if / else中你根本不需要别的东西。看一下日期手册。 date传递false的唯一方法是传入第二个参数的非数字时间戳。你没有这样做,所以日期将会返回其中一天。

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

但是,不是使用if / else或switch / case(这将是相同的),为什么不用你

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.

用l调用日期将为您提供当天的名称。 strtolower将全部小写。你在那里做的映射是多余的。上面的单行相当于您在问题中显示的整个代码段。

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.

在旁注中,什么样的函数名是of_get_option?什么是?它代表办公室吗?如果是这样,为什么这是Office对象上的函数而不是方法?毕竟,你说你正在使用物体。为什么当它应该显示办公时间时会说get_option?不应该将名称改为getOfficeHoursOnDay($ day)甚至更好,displayOfficeHoursOnDay($ listRenderer,$ day)。尝试使您的代码更具表现力和可读性,并尝试将责任转移到他们所属的位置。

#3


2  

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

如果已打开其他PHP标记,您当然不需要打开它们。


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!";
    }
?>

#4


1  

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!";
}

#5


1  

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.

我建议远离使用字符串。 Date返回根据区域设置设置的本地化字符串。所以你结束了一个不那么神奇的魔法弦。

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...

您当然需要更改功能以接受它。但它更容易理解,更便携,恕我直言清洁......