Hey Everyone, I am trying to write some PHP code to change a variable based on open hours M-S from 12:01 am until 6:30 pm Sun from 12:01 am until 4:30 pm I have started on the code below but I am sure there is an easier way of doing this, any help would be greatly appreciated! <?php $h = date('G'); //set variable $h to the hour of the day $d = date("w", $timestamp); //set variable $d to day of week if ($h > 8 && $h < 18 && ($d == 1 || $d == 2 || $d == 3 || $d == 4 || $d == 5)) $link = 'link1'; else $link = 'link2'; ?> PHP:
$time = date('H:i'); $d = date('w'); $link = ($time >= '12:01' && $time <= '18:30') && ($d >= 1 && $d < 7) ? 'link1' : 'link2'; // this will check for time between 12.01 and 18.30, and monday to saturday - modify as needed PHP:
What happens if the user is outside of those 2 time frames? Would there be a third link, or is it automatically in the first time frame regardless?
If the server is in the same time-zone as the users accessing the site, this isn't necessarily a problem - that depends on what type of business is being done. And on how many possible time-zones you're dealing with (for instance) - for me, based in Norway, as long as the server runs on standard time here, I wouldn't have any problem doing this based on server-time, really. There's only one time-zone to care about. Of course, if you're in the middle of Europe, or in the US, and do business all over, then it's a different issue, and you'd be wise to pull the time from the user instead.
Of course if the server is in the same time zone as ALL of the visitors there's nothing to worry about...................however...in the REAL WORLD we don't control where our visitors come from. Duh. But that is completely irrelevant...because the original poster is creating a script to control opening hours of a particular store or perhaps his web site. So the time zone would need to be based off the company's location which may be in a different time zone than the server. Even if by coincidence the server shares the same time zone we cannot rely on the time of the server. This could change do to a system admin change or if he completely moves his web site from one server to another.
And again... not every part of the world have different time zones to consider - if you have a local website, in your local language, you won't be very concerned if someone from across the world shows up, will you? And if they do, if you're based off the webserver, they'll see the correct information - basically if they visit when it's their middle of the day, and it's the middle of the night where you are, you might not want to show them a "call us here" link. And again - if you have control of your server, you have control of what time it shows. Regardless of that, you can control what time PHP shows - regardless of what time the SERVER has running - this is what we have timezone-settings in PHP for. Agreed, it might not be ideal, but it's perfectly fine for a plethora of different scenarios - since we know nothing of the OP's scenarios, giving a solution to what he asked for seemed pertinent.