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 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)