PHP'营业时间' - 自定义字段的时间

时间:2016-06-26 14:45:21

标签: php datetime advanced-custom-fields

我使用此代码显示一个简单的“开放式”代码。或者'关闭'信息。代码取自这个问题并略微调整 - Opening hours in PHP

    <?php
    date_default_timezone_set('Europe/London'); // timezone 

    $weekday = date(l); // today
    //print $weekday; // Debug
    //print date("H:i"); // debug

    // Set open and closing time for each day of the week
    if ($weekday == "Saturday") {
        $open_from = "10:00";
        $open_to = "14:00";
    }

   elseif ($weekday == "Sunday") {
        $open_from = "10:00";
        $open_to = "15:00";
    }

    else {
        $open_from = "10:00";
        $open_to = "17:00";
    }

    // now check if the current time is before or after opening hours
    if (date("H:i") < $open_from || date("H:i") > $open_to ) {
        echo '<div class="opening">Sorry , We Are Closed</div>';
    }

    // show open message
    else {
        echo '<div class="opening">We Are Open Now !</div>';
    }
?>

此代码适用于手动输入的开放时间,我遇到的问题是当我尝试为开放时间添加(ACF)自定义字段时,它会停止运行 - 作为星期日的示例如下所示?

elseif ($weekday == "Sunday") {
        $open_from = "<?php the_field('sunday_open','options'); ?>";
        $open_to = "<?php the_field('sunday_closed','options'); ?>";
    }

只要我在代码中输入字段,它就会说“抱歉,我们已关闭”&#39; ,即使在ACF选项时间选择器中设置了开启时间。当我将其更改回来并手动输入时,例如 -

$open_from = "10:00";
$open_to = "15:00";

它再次有效,并给出了“我们现在开放!”的信息。正确。

我运行了这个调试

<?php 

$time = get_field('sundays_open','options');

echo '<pre>';
    var_dump( $time );
echo '</pre>';

?>

并返回此 -

string '10:00' (length=5)

ACF时间字段的输出设置为H:i

有人能指出这里可能出现的问题吗?

1 个答案:

答案 0 :(得分:0)

您是将PHP标记放在PHP标记内吗?

试试,

elseif ($weekday == "Sunday") {
    $open_from = the_field('sunday_open','options');
    $open_to = the_field('sunday_closed','options');
}