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.";
}
?>
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.";
}
?>
Wrote from hand.
$dayname = date('w');
$hour = date('G');
if( $dayname >= 1 && $dayname <= 5 && $hour>=9 && $hour<=17) {
//your code
}
Related
I am trying to show a text in a page only on Thursday between 9pm to 10pm.
At the moment I wrote this code:
if((date('N') == 4 && date('G') >= 21) || (date('N') == 4 && date('G') < 22)) {
echo "Text";
}
but not work.
The or was causing the problem. So it always echo out.
if(date('N') == 4 and ( date('G') >= 21 and date('G') < 22)) {
echo "Text";
}
I'm trying to find a solution to a conditional based on the day of the week and a time range within that day. I've managed to hunt down the code for the day of the week but I can't find how to incorporate a time frame within the day?
For example:
IF today is Monday AND between 2pm and 4pm THEN do THIS
This is what I have...
<?php
date_default_timezone_set('Australia/Perth'); // PHP supported timezone
$script_tz = date_default_timezone_get();
// get current day:
$currentday = date('l'); ?>
<?php if ($currentday == Monday){ ?>
Monday
<?php } elseif ($currentday == Tuesday){ ?>
Tuesday
<?php } elseif ($currentday == Wednesday){ ?>
Wednesday
<?php } elseif ($currentday == Thursday){ ?>
Thursday
<?php } elseif ($currentday == Friday){ ?>
Friday
<?php } elseif ($currentday == Saturday){ ?>
Saturday
<?php } elseif ($currentday == Sunday){ ?>
Sunday
<?php } else { ?>
<?php } ?>
I'm not sure if this may help for the time frame?
Check day of week and time
Basically this:
<?php
if (date('l') === 'Monday' && date('G') >= 2 && date('G') < 4) {
// do something
}
You were missing quotes around the day names.
The condition I wrote will evaluate to true on Monday between 2 PM and 4 PM (while 4 PM itself will not, e.g. the last allowed values is 3:59 PM).
you could use different date/time components to streamline the code:
function between($value, $start, $end)
{
return $value > $start && $value <= $end;
}
$hours = date("G");
switch( date("N"))
{
case 1: //monday
if (between($hours,12 + 2,12 + 4 )) //using 24h format to avoid checking am/pm
{
// IF today is Monday AND between 2pm and 4pm THEN do THIS
}
break;
case 2: //tuesday
break;
//....
}
You'd better user date('N') as it is not language dependant and date('H') to take advantage of the 24h format that is better fit for time comparison.
function date_in_frame($test_date, $day, $start, $end){
$d = new Datetime($test_date);
return $d->format("N") == $day && $d->format("H") >= $start && $d->format("H") < $end;
}
//test if "now" is Monday between 2pm (14:00) and 4pm (15:59)
var_dump(date_in_frame("now", 0, 14, 16));
This code here checks a DateTime is between a start and end time.
<?php
$date = new DateTime('2019-11-18 12:49');
$start = new DateTime('2019-11-18 09:00');
$end = new DateTime('2019-11-18 17:00');
if ($date > $start && $date < $end) {
echo 'In the zone!';
}
Note.
$date->format('l'); will return Monday or whatever day it is.
$date->format('H:i'); will return 13:15 or whatever time it is.
Have a play! https://3v4l.org/XK5KR
All the conditions packed into an array is easier for maintenance.
A simplified example:
$ranges = [
['Monday',12,14, function(){echo "do something";}],
['Tuesday',12,14, function(){echo "do something on Tue";}],
//: more
];
$curWeekDay = date('l');
$hours = date("G");
foreach($ranges as $range){
if($curWeekDay == $range[0] AND $hours >= $range[1] AND $hours < $range[2]){
$range[3]();
}
}
Output on Tue 13:25:
do something on Tue
Ok.. so I've created myself a messy problem.
So I have a jquery slider that shows 5 slides of content
1) -2 hours ago
2) -1 hour ago
3) (0) present
4) +1 hours
5) +2 hours
The content for each slide is decided by what time of day it is. However when it comes to trying to go back/forwards 1 to 2 hours during the run up to midnight and after midnight the whole script breaks, and kills the site.
<?php
$h = date('G', strtotime ("-2 hour")); //set variable $h to the hour of the day.
$m = date('i', strtotime ("-2 hour")); //set variable $m to the min of the hour.
$d = date('w', strtotime ("-2 hour")); //set variable $d to the day of the week.
// SATURDAY SCHEDULE
if ($d == 6 && $h >= 0 && $h < 07) $file ='earlyhours.php';
else if ($d == 6 && $h >= 07 && $h < 09) $file ='breakfast.php';
else if ($d == 6 && $h >= 09 && $h < 12) $file ='throughthemorning.php';
else if ($d == 6 && $h >= 12 && $h < 13) $file ='rewind.php';
else if ($d == 6 && $h >= 13 && $h < 17 && $m <= 30) $file ='nonstop.php';
else if ($d == 6 && $h >= 17 && $m >= 30 && $h <21) $file ='livetrend.php';
else if ($d == 6 && $h >= 17 && $m >= 35 && $h < 21) $file ='nonstop.php';
else if ($d == 6 && $h >= 21 && $h < 18) $file ='gennation.php';
else if ($d == 6 && $h >= 23) $file ='earlyhours.php';
else if ($d == 7 && $h >= 0) $file ='earlyhours.php';
require $_SERVER['DOCUMENT_ROOT'] . '/collection/profiles/' . $file . '';
?>
As you can see it figures the time and then drops the correct file in - there's five of these for everyday of the week (-2.php, -1.php, 0.php, 1.php, 2.php).
Does anybody have a solution? Ideally I need to stop the break, but I don't want my visitors to be scrolling +1 / 2 or -1 / 2 hours on for the same days rotation when it nears, and steps over midnight.
For example, right now the code is broken on -2.php until at least 2am (my timezone) so that it back track.
I've totally burnt myself out trying to figure this one out.
The problems arising from the change of day can become intractable. I'd tackle this a different way. Start by calculating the day and hour for the five periods your interested in and use DateTime to do the heavy lifting. Then, use a function to provide the schedule item for a particular day/time combination.
Here's a skeleton
<?php
date_default_timezone_set('Pacific/Auckland');
$now = new DateTime();
$oneHour = new DateInterval('PT1H');
$minusOne = (clone $now);
$minusOne->sub($oneHour);
$minusTwo = (clone $minusOne);
$minusTwo->sub($oneHour);
$plusOne = (clone $now);
$plusOne->add($oneHour);
$plusTwo = (clone $plusOne);
$plusTwo->add($oneHour);
echo returnFile($minusTwo);
echo returnFile($minusOne);
echo returnFile($now);
echo returnFile($plusOne);
echo returnFile($plusTwo);
function returnFile(DateTime $t) {
$day = $t->format('D');
$hour = $t->format('G');
// echo "Day:$day, Hour: $hour;<br>";
switch ($day) {
case 'Mon':
if ($hour<7) {
// Small hours Monday...
$filename = "smallMonday.html";
break;
}
if ($hour<12) {
// Monday morning
$filename = "morningMonday.html";
break;
}
break;
case 'Tue':
if ($hour >=23) {
// Late Tuesday
$filename = "lateTuesday.html";
}
default:
$filename = "Some other time";
}
return $filename;
}
?>
I haven't put in a complete schedule - you can work that out.
If you're using PHP 5.5 or later you can use DateTimeImmutable instead of DateTime which does away with all the cloning.
There's a fiddle here
Get rid of the leading zeros in your comparisons. Those are octal numbers and not decimals. You won't get the results you expect.
// 09 is not a valid octal number. It gets converted to zero in decimal.
else if ($d == 6 && $h >= 07 && $h < 09)
..
else if ($d == 6 && $h >= 09 && $h < 12) $file ='throughthemorning.php';
This is my code below to get. but this code will invalidate the hour before 6PM which minute is above 30, how do I do it that
if today is "3" and today hour is still before 6:30PM , the if condition will works
My current code:
if( ($today=="3") && ($today_hour < "18") && ($today_min < "30") )
{
}
You should only check the minutes if the hour is actually 6. Change your condition to:
if ($today == 3 && ($today_hour < 18 || ($today_hour == 18 && $today_min < 30))) {
// Do stuff
}
Or easier to read:
$beforeTime = $today_hour < 18 || ($today_hour == 18 && $today_min < 30);
if ($today == 3 && $beforeTime) {
// Do stuff
}
Make sure to work with numbers as well, because doing string comparison on them will lead to unexpected results when the amount of digits is different.
Try this it will help you :
if ((date('d') == "03") && (date('H') < "18:30")) {
$condition = true;
}
H = 24-hour format of an hour (00 to 23)
I have a DIV on a page that I wanted to show only on Thursdays between 7pm and 8pm (local time) on a site. I made a post a while back and found my answer for that. Previous Post Here.
I was using this code to do so:
if( date('l') == "Thursday" && date('G') >= 19 && date('G') < 20 )
However now I need to show the div from 6:59:30 to 8:01pm (server time) and I'm not quite sure how I would format the statement above for that?
This would need to run weekly for an indefinite time until I remove the code.
if( date('l') == "Thursday" && date('Gis') >= 185930 && date('Gis') < 200100) {...
You should check out the php date manual for clarification: http://php.net/manual/en/function.date.php
if (date('l') == 'Thursday' && time() >= strtotime('18:59:30')
&& time() <= strtotime('20:01:00'))
if (date('l') == "Thursday" && date('His') >= '185930' && date('His') <= '200100')