1

I am trying to rotate phone numbers depending on the time of day. If the time is between 8am and 5pm, it should show the 888 number. If it is after 5pm but before 8am it should show the 777 number. This is what I got thus far, but it only shows the 777 number as my output, even though the time is between the hours of 8am and 5pm.

$dayPhoneNumber = "1-888-888-8888";
$nightPhoneNumber = "1-777-777-7777";
$currentPhoneNumber = "";

$nowHour = date("H"); 

$startHour = 8;
$endHour = 17;

if ($nowHour >= $startHour && $nowHour <= $endHour){
echo $dayPhoneNumber;
} else {
echo $nightPhoneNumber;
}
MethodWWW
  • 15
  • 4
  • works fine for me - are you taking your server's local time zone into account? – scrowler Oct 22 '13 at 20:43
  • 3
    Are you sure your server is returning the correct hour? date("H") will return the hour using the server's clock not your local PC's clock. – Robbert Oct 22 '13 at 20:44
  • This isn't the solution, but `date('H')` returns a zero-padded number, 00->23. you should be using `date('G')`, which isn't zero padded, 0->23. – Marc B Oct 22 '13 at 20:44
  • Zero-padding shouldn't be a problem (see http://de1.php.net/manual/en/language.operators.comparison.php). I'd go for the server time issue, too. – TheWolf Oct 22 '13 at 20:46
  • What happens on the page if I go to the website at 4:30, but my wife calls me for dinner, so I leave the page open and I come back at 5:30 and then try to call? – Darrick Herwehe Oct 22 '13 at 21:02
  • @DarrickHerwehe - Then I would say that you are satisfied having had a sumptuous feast, perhaps even experiencing a slight food coma, and will not be stressed by the fact that your call doesn't go through. – ghoti Oct 22 '13 at 21:04
  • Thanks Everyone. Your suggestions helped immensely. Sorry for the late reply. It was the server clock that was different than the local clock, as well as returning zero padded or non-padded numbers. – MethodWWW Nov 21 '13 at 19:17

1 Answers1

1

My guess is your server's time is not the same as your computer's time. If you're using a hosting solution, your server may very well be across the country or on a different continent, meaning the time will be different.

If you just need to worry about one location and not where the end user is, then this should be easy to do. If you're on the West Coast of the US

date_default_timezone_set("America/Los_Angeles");
$hour = date("H", time()); 

Here is a list of Time Zones: http://www.php.net/manual/en/timezones.php

If you need to know the end user's time zone when determining the hour, here's a discussion on doing that: How to get client's timezone?

If you're dealing with time zone issues, you may also want to retrieve the time using UTC, and then add or subtract whatever number of hours to get to the correct time zone: get UTC time in PHP

Community
  • 1
  • 1
Robbert
  • 6,481
  • 5
  • 35
  • 61
  • Basing the display on the client's timezone is almost certainly not what you want because the hours of the staff manning the phone lines aren't contingent on the client's timezone. I.e., if an east coast operator goes home at 5pm then you don't want to use a west coast client's timezone to tell them the operators are there for another three hours. – Alex Howansky Oct 22 '13 at 20:53
  • Good point. In that case, you would need to set the time zone of the business location. I still think the server time is the main issue. – Robbert Oct 22 '13 at 20:56
  • Agreed. Server is probably defaulted to UTC, so OP should just need to run `date_default_timezone_set()` for the local zone. – Alex Howansky Oct 22 '13 at 20:58