0

I'm new to programming, and trying to develop an IF, ELSE statement that relies on the day of the week, and the current time in the Central time zone.

I'm writing in PHP and I've been looking at strtotime, then trying to format it in various ways, but it isn't clear to me. Can someone recommend a guide that I could use to sort through the mess?

I found this, which may offer a help.

$time = strtotime('10:00');
$startTime = date("H:i", strtotime('-30 minutes', $time));
$endTime = date("H:i", strtotime('+30 minutes', $time));

My application is having an action occur if before business hours, and a second action occur if after business hours, for a business open five days a week.

Thank you in advance.

4 Answers4

1

You can use date("l") to get a string representation of the DOW then create an array that contains the business hours using the DOW string as the array's key

<?php
$arr = array(
  "Monday" => array(
      "open" => "09:00",
      "close" => "17:00",
  ),
  "Tuesday" => array(
      "open" => "09:00",
      "close" => "17:00",
  ),
  // Wednesday, Thursday....etc.
  "Saturday" => null,
  "Sunday" => null
);

echo $arr[date("l")];
?>
sciweb
  • 51
  • 3
0

php's date() will return the day of the week with "l"

date_default_timezone_set('UTC');
echo date("l");

http://php.net/manual/en/function.date.phpenter link description here

Ryan Kozak
  • 1,091
  • 6
  • 16
  • 21
0

Here's an example that uses DateTime() which is better for working with dates since it takes daylight savings time and leap years into account. It also makes working with timezones easier.

All this does is create three DateTime objects. One to represent "now". One to represent today at 10am and one to represent today at 10pm. It then checks to see if "now" is after opening time and before closing time. It can do this easily because DateTime objects are comparable (which means you don't have to convert the dates and times into integers or some string format to try to compare them).

$now = new DateTime();
$startTime = new DateTime('10:00');
$endTime = new DateTime('22:00');
if ($now < $startTime) {
    // before business hours
}
else if ($now > $endTime) {
    // after business hours
}

To check the day of the week you can use the l modifier to DateTime::format(). Then you can dynmaically alter the opening and closing times:

$now = new DateTime();
switch ($now->format('l')) {
    case 'Monday' :
        $open = '10:00';
        $close = '22:00';
        break;
    case 'Tuesday' :
        $open = '9:00';
        $close = '23:00';
        break;
    // etc...
}
$startTime = new DateTime($open);
$endTime = new DateTime($close);
if ($now < $startTime) {
    // before business hours
}
else if ($now > $endTime) {
    // after business hours
}

If your server is not in central timezone, you can modify your code to work with it using DateTimeZone().

$timezone = new DateTimeZone('America/Chicago');
$now = new DateTime(null, $timezone);
switch ($now->format('l')) {
    case 'Monday' :
        $open = '10:00';
        $close = '22:00';
        break;
    case 'Tuesday' :
        $open = '9:00';
        $close = '23:00';
        break;
    // etc...
}
$startTime = new DateTime($open, $timezone);
$endTime = new DateTime($close, $timezone);
if ($now < $startTime) {
    // before business hours
}
else if ($now > $endTime) {
    // after business hours
}
John Conde
  • 217,595
  • 99
  • 455
  • 496
0

You really need to look at date function

For starters, use something like date("l") to get the day of the week, then check timezone function to set Central time zone.

Community
  • 1
  • 1
yoyoma
  • 3,336
  • 6
  • 27
  • 42