Reputation: 45
I'm trying to build a PHP script so that when a store is opened it will show the words "is now open" and when it's closed it'll show "is now closed" based on timezone and hours. I can't seem to get this to work, I found this script but it doesn't work properly.. It should work on the dutch timezone.. +1 Amsterdam.. Can anyone aid me?
I'd appreciate the help!
SCRIPT:
<?php
$schedule[0] = "700-1730";
$schedule[1] = "700-1730";
$schedule[2] = "700-1730";
$schedule[3] = "700-1730";
$schedule[4] = "10:00-17:30";
$schedule[5] = "700-1300";
$schedule[6] = "0";
$today = $schedule[date('w')];
list($open, $close) = explode('-', $schedule);
$now = (int) date('Gi');
$state = 'is geopend.';
if ($today[0] == 0 || $now < (int) $today[0] || $now > (int) $today[1]) {
$state = 'is gesloten.';
}
?>
<?php echo $state;?>
Upvotes: 0
Views: 1268
Reputation: 818
The colon is causing a bit of a problem so you can remove it with a quick str_replace();
Instead of $schedule
you want to explode();
$today
once you have made it.
<?php
$schedule[0] = "700-1730";
$schedule[1] = "700-1730";
$schedule[2] = "700-1730";
$schedule[3] = "700-1730";
$schedule[4] = "10:00-17:30";
$schedule[5] = "700-1300";
$schedule[6] = "0";
$today = $schedule[date('w')];
$now = (int) date('Gi');
list($open, $close) = explode('-', $today);
// get rid of colon if you have used one
$open = (int) str_replace(':', '', $open);
$close = (int) str_replace(':', '', $close);
$state = ' is geopend.';
if ($today[0] == 0 || $now < $open || $now > $close){
$state = ' is gesloten.';
}
?>
Depending on the setup of your server, if you don't want to dynamically reset timezone in your ini settings, you could add 100 per hour you want to adjust by, so if you have hours time difference in your database you could make a variable $time_difference * 100
and add it to or subtract it from $now = (int) date('Gi');
so, for example $now = (int) date('Gi') + 700;
would give you European Central Time when your server time is based on New York time or $now = (int) date('Gi') + $time_difference * 100;
could be used to adjust it dynamically. (You would need to account for daylight saving time though!)
Time reference for the right way to set timezones: PHP date(); with timezone?
In case you wanted to play with using the user's own timezone to display your opening times and adjust accordingly for a bit of fun - may not be advisable for a live project as there seem to be few totally reliable ways. Determine a User's Timezone
Please note that the numbers for days work from 0 for Sunday to 6 for Saturday http://php.net/manual/en/function.date.php
Upvotes: 1