duanjianfu1398 2019-08-15 10:28
浏览 91
已采纳

动态打开时间短代码始终显示已关闭

I'm having issues with a shortcode I've created to pull the opening times of a shop and display an 'Open' or 'Closed' status based on the opening times array.

I had it working or so I thought at one point but it since seems to have stopped working. I don't believe I've changed much with it and I've also tried some of the alternative methods mentioned in this SO post: Determine If Business Is Open/Closed Based On Business Hours

The alternative methods either didn't work or just flagged errors.

I was wanting it set to Europe/London timestamp as the store is only UK based. Any ideas what I could've done wrong here?

I'll keep plugging away at in the meantime, seems a strange one to me though.

I'm aware the original code I used is based off an old post on here (approximately 6 years old), so I'm wondering if it could be outdated PHP. Our server is running PHP 7.

//open/closing times
function opening_times() {
ob_start();
$storeSchedule = [
'Sun' => ['10:00 AM' => '15:00 PM'],
'Mon' => ['10:00 AM' => '17:00 PM'],
'Tue' => ['10:00 AM' => '17:00 PM'],
'Wed' => ['00:00 AM' =>  '00:00 AM'],
'Thu' => ['10:00 AM' => '17:00 PM'],
'Fri' => ['10:00 AM' => '17:00 PM'],
'Sat' => ['09:00 AM' => '17:00 PM']
];

// current OR user supplied UNIX timestamp
$timeObject = new DateTime('Europe/London');
$timestamp = $timeObject->getTimeStamp();

// default status
$status = 'closed';

// get current time object
$currentTime = (new DateTime())->setTimestamp($timestamp);

// loop through time ranges for current day
foreach ($storeSchedule[date('D', $timestamp)] as $startTime => $endTime) {

// create time objects from start/end times
$startTime = DateTime::createFromFormat('h:i A', $startTime);
$endTime   = DateTime::createFromFormat('h:i A', $endTime);

// check if current time is within a range
if (($startTime < $currentTime) && ($currentTime < $endTime)) {
    $status = 'open';
    break;
    }
}

echo '<div><i class="far fa-clock fa-3x"></i><span class="open-times"><small>We are ' . $status . '</small><br>';
return ob_get_clean();
}
add_shortcode( 'open_times', 'opening_times' );

It works to some extent in the fact that the status constantly displays as "Closed" which should be the default status if the store isn't open.

It just no longer display as "Open" status at any point. It should display as "Open" when it meets the criteria of the Opening Times.

  • 写回答

1条回答 默认 最新

  • douji0108 2019-08-15 10:52
    关注

    Not sure why you picked to use an AM and PM time format in an array you appear to create yourself, but basically there is no 17:00 PM it should be 5:00 PM so just fix your array and the code will work.

    The issue was trying to create a DateTime from 17:00 PM in this line. Basicaly it failed every time.

    $endTime   = DateTime::createFromFormat('h:i A', $endTime);
    

    so just fix your array to use valid times and the code will work.

    $storeSchedule = [
        'Sun' => ['10:00 AM' => '03:00 PM'],
        'Mon' => ['10:00 AM' => '05:00 PM'],
        'Tue' => ['10:00 AM' => '05:00 PM'],
        'Wed' => ['00:00 AM' => '00:00 AM'],
        'Thu' => ['10:00 AM' => '05:00 PM'],
        'Fri' => ['10:00 AM' => '05:00 PM'],
        'Sat' => ['09:00 AM' => '05:00 PM']
    ];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计