PHP时间特定的div显示

时间:2018-07-15 00:54:34

标签: php

我一直在研究一个脚本,该脚本将采用提供的两个div之一,并根据当天的工作时间显示正确的div。我不确定要完成我所需要的更好的方法,我的知识仅限于此语言,并且来自项目的设计需求。

$date = date("w"); //0-6 Day of Week
$currentHour = date("H");
$currentHour2 = date("Hi");

//Tuesday, Thursday, Friday
if ($date == 2 || $date == 4 || $date == 5) {
    $openTime = 8; $closeTime = 18;
} elseif ($date == 3) {
    //Wednesday
    $openTime = 10; 
    $closeTime = 18;
} elseif ($date == 6) {
    //Saturday
    $openTime = 10; 
    $closeTime = 17;
} elseif ($date == 1 || $date == 0) {
    //Sunday, Monday
    $openTime = 12; 
    $closeTime = 13; 
    $openTime2 = 1830; 
    $closeTime2 = 1930;
} 

if ($currentHour < $openTime || $currentHour > $closeTime) {
    $OffOn = 0;
} elseif ($currentHour2 < $openTime2 || $currentHour2 > $closeTime2) {
    $OffOn = 0;
} else {
    $OffOn = 1;
}

下面的代码块是此代码的最终结果的简化示例-编写为使您了解我对上面的变量的确切用途。

if ($OffOn == 1) { 
    <a>We Are Current Open</a> 
}else{ 
    <a>We Are Current Closed</a> 
} 

我们将不胜感激。

4 个答案:

答案 0 :(得分:0)

我建议进行类似isOpen功能的配置。如果某些开放时间发生变化,这样做不会使您更改代码。您只需要更改配置数组即可。

<?php
function isOpen( $timestamp ) {
    $times = [
        0 => [[1200,1300],[1830,1930]], // Sunday
        1 => [[1200,1300],[1830,1930]], // Monday
        2 => [[800,1800]], // Tuesday
        3 => [[1000,1800]], // Wednesday
        4 => [[800,1800]], // Thursday
        5 => [[800,1800]], // Friday
        6 => [[1000,1700]], // Saturday
    ];

    $open = false;
    $weekday = date('w', $timestamp);
    $hour_and_minute = date('Hi', $timestamp);

    foreach($times[$weekday] as $opening_times) {
        if(
            $hour_and_minute >= $opening_times[0]
            && $hour_and_minute <= $opening_times[1]
        ) {
            $open = true;
            break;
        }
    }

    // For testing only
    echo date( 'Y-m-d H:i', $timestamp ) . " " . date('l', $timestamp) . " is " . ($open?'open':'closed');
    echo '<br>';

    return $open;
}

// Testing
isOpen( time() ); // now
isOpen( strToTime( '1980-01-01 15:30:00' ) );
isOpen( strToTime( '1980-01-02 19:30:00' ) );
isOpen( strToTime( '1980-01-03 07:30:00' ) );
isOpen( strToTime( '1980-01-03 08:00:00' ) );

// in your template you'd write
/*

    echo "We are currently ";
    if(isOpen(time())) {
        echo "open";
    } else {
        echo "closed";
    }


*/

输出

2018-07-14 21:29 Saturday is closed
1980-01-01 15:30 Tuesday is open
1980-01-02 19:30 Wednesday is closed
1980-01-03 07:30 Thursday is closed
1980-01-03 08:00 Thursday is open

答案 1 :(得分:0)

为进行比较,可以使用PHP的strtotime函数。

在这种情况下,您可以使用它,也可以使用数组来存储小时以提高可读性:

$openTimes = [
    'Sunday' => [
        ['1200', '1300'],
        ['1830', '1930']
    ],
    'Monday' =>  [
        ['1000', '1800'],
        ['1830', '1930']
    ],
    'Tuesday' =>  [
        ['0800', '1800']
    ],
    'Wednesday' =>  [
        ['1000', '1800']
    ],
    'Thursday' =>  [
        ['0800', '1800']
    ],
    'Friday' =>  [
        ['0800', '1800']
    ],
    'Saturday' =>  [
        ['1200', '1300'],
        ['1830', '1930']
    ],
];

$dayOfWeek = date("l");
$currentHour = strtotime(date("hi"));

$isOpen = false;
foreach ($openTimes[$dayOfWeek] as $times) {
    $open = strtotime($times[0]);
    $close = strtotime($times[1]);
    if ($currentHour > $open && $currentHour < $close) {
        $isOpen = true;
    }
}

if($isOpen) {
    echo "<div>We are open!</div>";
} else {
    echo "<div>We are closed!</div>";
}

答案 2 :(得分:0)

这是我通过更详细的数组设置来处理此问题的方式,因此您可以了解正在发生的事情...

<?php

function is_open()
{

    $days = array(
        0 => array( // Sunday
            'Day Shift' => array(
                'Start' => '12:00',
                'End' => '13:00'
            ),
            'Night Shift' => array(
                'Start' => '18:30',
                'End' => '19:30'
            )
        ),
        1 => array( // Monday
            'Day Shift' => array(
                'Start' => '12:00',
                'End' => '13:00'
            ),
            'Night Shift' => array(
                'Start' => '18:30',
                'End' => '19:30'
            )
        ),
        2 => array( // Tuesday
            'Day Shift' => array(
                'Start' => '08:00',
                'End' => '18:00'
            )
        ),
        3 => array( // Wednesday
            'Day Shift' => array(
                'Start' => '10:00',
                'End' => '18:00'
            )
        ),
        4 => array( // Thursday
            'Day Shift' => array(
                'Start' => '08:00',
                'End' => '18:00'
            )
        ),
        5 => array( // Friday
            'Day Shift' => array(
                'Start' => '08:00',
                'End' => '18:00'
            )
        ),
        6 => array( // Saturday
            'Day Shift' => array(
                'Start' => '10:00',
                'End' => '17:00'
            )
        )
    );

    $today_index = date('w');
    $time_now = date('H:i');

    foreach($days[date('w')] as $shift)
    {

        if($today_index == $days[date('w')] && $time_now >= $shift['Start'] && $time_now < $shift['End'])
        {

            return true;

        }

    }

    return false;

}

if(is_open())
{

    echo '<a>We Are Current Open</a>';

} else {

    echo '<a>We Are Current Closed</a>';

}

答案 3 :(得分:0)

这是在php中完成的方式

$date = date("w"); //0-6 Day of Week
$currentHour = (int)date("H");
$daysHours = array(array(1200,1300),array(1830,1900),array(1000,1800),array(800,1800),array(800,1800), array(800,1800), array(1000,1700));//days of week with open and close hours

if($currentHour < $daysHours[$date][0] || $currentHour > $daysHours[$date][1] ){
$OffOn = 0;
    echo "div is off ";
    return;
}
$OffOn = 1;
echo "div is on";