1

I've got a piece of code on a small system I've put together for work and have got it to almost work perfectly.

The idea is that between 9am and 5pm, Monday to Friday it'll show a form and outside of those times it'll show a message advising them to seek help from another team.

It seemed to work fine today while I've been testing it by changing the times etc.. but then it didn't seem to want to work at all, I seemed to have solved it by adding '09' instead of '9' as the starting time but I've just checked the page it's being used on and the form is showing when in theory it should be the message.

Any ideas?

    <?php
date_default_timezone_set('Europe/London');

$day = date("l"); 
$current_hour = date("g"); 

if ($day == "Monday" && $current_hour >= 09 && $current_hour <= 17) { 
require_once('../forms/function.php');
formcraft(6); 
} 
elseif ($day == "Tuesday" && $current_hour >= 09 && $current_hour <= 17) { 
require_once('../forms/function.php');
formcraft(6); 
} 
elseif ($day == "Wednesday" && $current_hour >= 09 && $current_hour <= 17) { 
require_once('../forms/function.php');
formcraft(6); 
} 
elseif ($day == "Thursday" && $current_hour >= 09 && $current_hour <= 17) { 
require_once('../forms/function.php');
formcraft(6); 
} 
elseif ($day == "Friday" && $current_hour >= 09 && $current_hour <= 17) { 
require_once('../forms/function.php');
formcraft(6); 
} 
else 
{ 
     echo "We're not in the office right now. If your request is urgent, and by that we mean that it will have an immediate impact on our customers and/or people, please forward it to the Duty Managerwho'll know what to do."; 
} 
?>
5
  • 2
    constant beginning from zero is octal. 9 is unavailible digit. So 09 turns to zero Commented Jun 27, 2016 at 21:14
  • You might try date('w') for a numeric representation of the day of the week (0-6). Also, date('G') seems more appropriate, as it returns a 24 hour representation of the hour without zero-padding. See date(). Commented Jun 27, 2016 at 21:14
  • You'll want to use date('G') (with a big G) to get an hour of the day that is not zero-padded and goes from 0 to 23 (I assume you don't offer services from 9PM to 5AM). You don't need to zero-pad 09, that may make the interpreter think you're doing octal math. Commented Jun 27, 2016 at 21:15
  • use H with leading zeros php.net/manual/en/function.date.php if you're going to want to use that (leading zero) format. Commented Jun 27, 2016 at 21:16
  • For the day, you can use date('w') which gives a numeric day of the week from 0 to 6. Then instead of having five conditionals, you can just say if ($day >= 1 && $day <= 5)... Commented Jun 27, 2016 at 21:17

2 Answers 2

7

Your code, optimized:

<?php
date_default_timezone_set('Europe/London');

$day = date("w"); // Numeric representation of the day of the week
$current_hour = date("G"); // G 24-hour format of an hour without leading zeros

if ($day >= 1 && $day < 6 && $current_hour >= 9 && $current_hour <= 17)

{
    require_once ('../forms/function.php');
    formcraft(6);
}else{
    echo "We're not in the office right now. If your request is urgent, and by that we mean that it will have an immediate impact on our customers and/or people, please forward it to the Duty Managerwho'll know what to do.";
}

?>
Sign up to request clarification or add additional context in comments.

2 Comments

I knew it could be optimised a lot, PHP isn't my strong point at all so I was going with what I knew and was going to look at optimising at a later date once I'd worked out this whole not reading the timezone properly. Thanks for your help @Dragon it's worked a treat and has given me a great comparison so that I look to see where the optimising could have been so I can learn for next time.
@Dave i see your new to stack overflow, so please have a look at the help documentation and specifically: stackoverflow.com/help/someone-answers
2

Wrote from hand.

$dayname = date('w');
$hour = date('G');

if( $dayname >= 1 && $dayname <= 5 && $hour>=9 && $hour<=17) {
    //your code
}

2 Comments

date('H') includes leading zeros.
php.net/manual/en/function.date.php "H 24-hour format of an hour with leading zeros 00 through 23" - as per your original post stackoverflow.com/revisions/38063418/1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.