0

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.

Adam R
  • 77
  • 7

1 Answers1

1

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']
];
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • So stupid of me to miss that. Thanks, works fine now. I must have had it that way a while back and changed it for some reason. – Adam R Aug 15 '19 at 10:58