(PHP) Display link ONLY during business hours? - php

I am trying to make a live chat link appear on the website only during business hours. I have the code below which seems to work in the afternoon, but won't work in the morning and I'm not sure why... $start and $end are values received from a MySQL database but in my example I've hard coded them to make the example simpler.
$LinkStatus = "on";
$start = 9:00:00;
$end = 23:00:00;
$current_time = date('G:i:s'); //9:35:00
if (($start > $current_time) || ($end < $current_time)) {
$LinkStatus = "off";
}
If the start time is greater than the current time, then the business is not open yet. If the end time is less than the current time, then it's after hours. Any time between 9am and 11pm (23:00) neither one of those conditions should be true, therefore $LinkStatus should remain "on". However, it does not seem to be doing that right now. Something is setting it to "off".
I've echoed the variable above the if statement and below it so I can confirm it's this if statement causing the variable to be set to "off".
As you can probably see from my code example, I'm not very knowledgeable when it comes to PHP. Any help is appreciated.

date('G:i:s') // 24 hours time without leading zero for hour
...won't sort well as a string, for example '9' > '10'.
Use 24 hour time with a leading zero instead, which makes the correct sort '09' < '10';
date('H:i:s') // 24 hour time with leading zero for hour

date_default_timezone_set('Europe/London');
$day_start = '09:00:00';
$day_end = '16:59:59';
$current_time = date('H:i:s'); // For 9-5 hours only
$current_day = date('N'); // For Monday to Friday only
if (($current_day <= 5) && ($current_time >= $day_start) && ($current_time <= $day_end)) {
echo 'ON';
} else {
echo 'OFF'
}

Related

Alarm sound at certain time of a day

The idea is to play an alarm from html/php web-page via a TV hanging near the school canteen reminding students to wash their hands. The TV is used as a tabloid screen for school events and news.
The piece of code I'm using on this page is as follows:
<?php
$hour = date('G');
$day = date('1..5'); // 1..7 for Monday to Friday
if (($hour >= 11.05 && $hour <= 11.35) // 5am - 7am
|| ($hour >= 12.15 && $hour <= 12.30) // 10am - 12 noon
|| ($hour >= 21.10 && $hour <= 21.30) // 10am - 12 noon
|| ($day == 1-5) // Monday - Friday
) { ?>
<audio src="Audio/sirena.mp3" autoplay="true" loop="loop">
<?php } ?>
This only plays it once the page is uploaded onto the server, and if it falls within the times above. Otherwise, it stays silent. And, strangely it plays only on my home PC on Chrome/IE/Mozilla and it doesn't play completely on school PCs.
Bear in mind the page auto refreshes itself every 5 min.
Would appreciate if someone gave a hint on event listeners or anything else.
There are a couple of problems with your code.
In $day = date('1..5');, '1..5' isn't a valid format for date(). That will just return the string 1..5. You want date('w').
date('G') returns the hour in 24-hour format, so $date will be a string representation of an integer. In your if condition you are comparing it to floats. Since all of the different time conditions are various decimals above the same integers, $date will never match any of them. For example, if the time is 11:30 AM, $date will be '11', which is not between 11.05 and 11.35. (Obviously, neither is '10' or '12'.)
In the last part of the if condition, you have || ($day == 1-5). There are two problems with that. First, 1-5 does not mean "between one and five"; it means "one minus five". And second, the fact that this is an additional non-nested or condition means that it negates all of the various time comparisons, because whenever any part of an or condition is true, the entire condition evaluates to true. If it was || ($day >= 1 && $day <= 5), then the if would be true all day every Monday through Friday. As it is, it's true all day only on Thursday.
I added an updated version that I think will do what you want it to (based on the comments in your code). I used some kind of strange indentation in the if condition to hopefully show the grouping a little better. Basically it needs to check the day and check a group of time conditions. Note the different format for time ('Gi'), which gets the hour and minute in a format that can be compared to integer values in the if condition.
$day = date('w');
$time = date('Gi');
if ( ($day > 0 && $day < 6) // Monday - Friday
&& ( // AND
($time >= 1105 && $time < 1135) // time range 1
|| // OR
($time >= 1215 && $time < 1230) // time range 2
|| // OR
($time >= 2110 && $time < 2130) // time range 3
)
)
echo '<audio src="Audio/sirena.mp3" autoplay="true" loop="loop">';

Checking if current time of day is before or after a specific time

I've found plenty of solutions that output a bool if the day is before or after a specific date, but I can't seem to find one about time specifically in PHP.
I've got a script that I want to be available for use between 7:45AM and 3:45PM and only monday through friday - but never before or after these times or on weekends.
I've been racking my brain on how to do this, and I'm posting here because I'm sure there is a very simple way to do this that I am overlooking.
This is what I have so far:
$timeOfDay = date("h:iA");
$dayOfDay = date("D");
if(strcmp($dayOfDay,'Mon') != 0
||strcmp($dayOfDay,'Tue') != 0
||strcmp($dayOfDay,'Wed') != 0
||strcmp($dayOfDay,'Thu') != 0
||strcmp($dayOfDay,'Fri') != 0) {
//disable site
} else {
//if $timeOfDay is before 7:30AM or after 3:45PM, disable site ELSE enable site
}
To be clear, the script should only be online Monday-Friday between 7:45AM and 3:45PM.
EDIT:
Another note - I am ONLY seeking the PHP logic in which I can get a true or false for, I can handle the rest of the code disabling the script etc.
$now = strtotime("now"); // here come current timestamp
$dt_start = strtotime(date("d.m.Y 07:45")); // timestamp of today 07:45
$dt_end = strtotime(date("d.m.Y 15:45")); // timestamp of today 15:45
$cur_day = date("w", $now); // number of cur day 0 to 6
if ((0 < $cur_day && $cur_day < 6) // check what day is it
&& ($dt_start < $now && $now < $dt_end)) { // check time interval
// do stuff
}

Finding the difference between 2 dates in PHP

I am using PHP, jQuery AJAX and HTML to create a timesheet system, for this the user needs to select 2 dates within 1 month of each other. The system as yet is working and shows (very limited) data.
BUT! When I actually select a date over the month limit (i.e. 2 months further than the start or another year after the start), it still shows the table with the data.
For this I have this check:
$dt1 = new DateTime($_REQUEST['startdate']);
$dt2 = new DateTime($_REQUEST['enddate']);
$diff = date_diff($dt1, $dt2);
// I have tried this the other way around and get the same result...
if($diff->m > 1 || $diff->y > 1)
{
print("<center><strong>Time between dates it too great<br />Please choose another date or time within a month of each other</strong></center>");
die();
}
The dates are passed by a jQuery datepicker object via AJAX, and the dates I use, for example, are passed as such:
11/14/2015 (start date) && 12/14/2015 (end date) - should show data
09/14/2015 (start date) && 12/14/2015 (end date) - should not show data but does
11/14/2015 (start date) && 12/14/2016 (end date) - should not show data but does
There is a check in place that sees if the dates given start before the other and this works, I have tried the same kind of thing for this check, but without success, this check is as such:
function CountDaysBetween($startDate, $endDate)
{
$begin = strtotime($startDate);
$end = strtotime($endDate);
if ($begin > $end) {
echo "start date is in the future! <br />";
return;
} else {
$no_days = 0;
$weekends = 0;
while ($begin <= $end) {
$no_days++; // no of days in the given interval
$what_day = date("N", $begin);
if ($what_day > 5) { // 6 and 7 are weekend days
$weekends++;
};
$begin += 86400; // +1 day
};
$working_days = $no_days - $weekends;
return $working_days + 1;
}
}
Edit
Dates 2 or more months apart within the same year work, tested again and this is the case, but dates into the next year do not
In your first part of the php code, you have put this operator>, but the problem is it means, everything Smaller than 1, not everything that is smaller than one or equal to 1. The easy solution is to change the operators to >=; which means everything that is equal to 1 or smaller than 1.
The date_diff constructs in PHP suck monkeyballs. Far more practical is to use straight comparisons instead:
$dt1 = new \DateTime($_REQUEST['startdate']);
$dt2 = new \DateTime($_REQUEST['enddate']);
$dt1->add(new \DateInterval('P1M'));
echo ($dt1 < $dt2 ? 'Less' : 'More') . ' than a month';
Also please do not use $_REQUEST, it has potentially terrible security issues. You should use $_GET, $_POST or $_COOKIE according to what you explicitly expect.

Determine if a week is odd or even

I have debugged this legacy code, and would like a sanity check on it.
The purpose of it is to allow someone to choose a delivery frequency for shipping a product. If someone wants their product Every Other Week, the system needs to determine if they should get an order next week, or two weeks from now. We call it A week, or B Week.
Keep in mind I did not write this, I am just trying to make sense of it and would like some help evaluating its accuracy:
if (date("l") == "Monday" ) {
$start = 0;
} else if (date("l") == "Tuesday" || date("l") == "Wednesday" || date("l") == "Thursday" || date("l") == "Friday" || date("l") == "Saturday"|| date("l") == "Sunday") {
$start = -1;
}
// if changing to every other week set to next week's a/b-ness
$a_week_tid = 34;
$b_week_tid = 35;
$every_other_week_frequency_id = 32;
if ($delivery_frequency == $every_other_week_frequency_id) {
$julian = (int) (strtotime('Monday +' . $start . ' week') / 86400);
$julian_week = ($julian-4) / 7;
if ($julian_week % 2) {
$today_a_or_b = $b_week_tid;
$next_week_a_or_b = $a_week_tid;
$a_or_b_week_string = '(A Week)';
} else {
$today_a_or_b = $a_week_tid;
$next_week_a_or_b = $b_week_tid;
$a_or_b_week_string = '(B Week)';
}
} else {
$next_week_a_or_b = NULL;
$a_or_b_week_string = NULL;
}
This code is not commented or documented. The part that confuses me is:
Why is 4 subtracted from Julian, then divided by 7?
If today is Monday, $julian_week is 2129, and 2129 % 2 evaluates TRUE. Is that correct?
Is this even how it should be done? Can't I rewrite this using date('w') a lot easier?
Yeah using date would totally be easier, plus it takes into account leap years, daylight saving time, all that extra stuff you don't want to have to deal with.
if (date('W')%2==1)
That's SOOOO much easier to maintain than the above.
I don't believe you can use date("W") in this case. According to the ISO calculation, on occasion, there will be years with 53 weeks. In those years, Week 53 is followed by Week 01, both odd numbers, and an A/B calculation based on Even/Odd ISO week number would result in two successive A or B weeks.
The original calculation determines the number of days from the UNIX epoch of the present Monday, or of the most recent Monday if today is not a Monday. The -4 causes the A/B week labels to change on Thursdays. Even/oddness of a week is determined from a fixed date (the Unix Epoch), so there will be no discontinuity in the oscillation of A/B-ness using the original code.
The ISO standard for week one in a year is that it is the week that the first Thursday of the year falls. This is the reason for the 4 subtracted from the Julian date. The week number is then found by dividing by 7.
Again the ISO standard implies that week number cannot be greater than 53. I don't understand how your figure of 2129 can arise. However the div operator will not evaluate TRUE for this figure. Checking the div operator on the week number is the way of determining whether you are in week a or b. If it is before Thursday, it is quite likely that the number will be 1 less than you anticipate.
The coding looks fairly good to me, though I have not stepped through all of it. It does look correct.
Using W on consecutive Fridays, mod by 2. Both lines output 1. So doing it this way will occasionally fail.
echo date('W',strtotime('2016-01-01'))%2;
echo date('W',strtotime('2016-01-08'))%2;
Just a simple way.
<?php
$weekNumber = date("W");
echo 'Week number:',$weekNumber;
if($weekNumber&1) {
echo '<strong>Week A.</strong>';
} else {
echo '<strong>Week B.</strong>';
}
?>
$day = '2019-11-10';
$date = new DateTime($day);
$dayOfMonth = $date->format("j"); // month days 1 - 30
$weekNumber = ceil($dayOfMonth / 7); // get the week number
if ($weekNumber % 2 == 0) { //if week number is even
echo "Even Week";
} else {
echo "Odd Week";
}
**// output Even Week**

how to check depending on hour base

I have a field that has time and date in following format
2010-03-26 10:06:11
What I need is that if this time is within 4 hours of current time then show but if its over 4 hour then done show this record.
thanks
$ts = strtotime($value);
$curtime = time();
if (($ts > $curtime - 4*3600) && ($ts < $curtime + 4*3600)) {
//show
}
else
//don't show
You can also make a one-side comparison by choosing only one of the conditions (it isn't clear what you want from the question).

Categories