Need specific time each week in PHP - php

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')

Related

Time dependent URL redirection script, should work differently on Monday - How?

This is the top part of a script I am using for redirection.
It connects to two txt based databases depending on time and redirects the user to a particular link.
There are two databases
'db1.txt' and 'db2.txt'
I only want to use the 'db2.txt' on Mondays.
All other day it should work normally, but on Mondays only, it should not change to urls1.txt.
How can I achieve that ?
$time = date("Hi", time());
if ($time >= 2224 && $time <= 2359)
{
$db = "db1.txt";
}
elseif ($time >= 0000 && $time <= 729)
{
$db = "db1.txt";
}
else
{
$db = "db2.txt";
}
Edit:
I am from India, and I am using server time in my script to avoid more complications. The script is designed to load db1.txt from 9:00AM to 6:00PM IST, and db2.txt from 6:01PM to 8:59AM. So using the "if (date('N') !=1 " wont work. I will need to change the time-zone as well.
I think you need to add this condition != monday like this
$time = date("Hi", time());
if (date('N') !=1 && $time >= 2224 && $time <= 2359)
{
$db = "db1.txt";
}
elseif (date('N') !=1 && $time >= 0000 && $time <= 729)
{
$db = "db1.txt";
}
else
{
$db = "db2.txt";
}
note : N ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0)
Check the argument documentation for the date function. With:
date('N');
you should be able to check if it's Monday and execute your desired code conditionally, ex.:
if(date('N') === 1){
}else{
}

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">';

PHP IF/ELSEIF/ELSE for Day and Time

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
}

PHP on getting time before 6:30PM

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)

If statement syntax and logic issue

I am trying to switch our surf web cameras off at civilian twilight but having some difficulties with the if statement at the bottom of this code. I am pretty sure it's a syntax issue but can't see it.
//Sunrise
//Set Zenneth to 96 which is Civilian Twilight start. Normally set to 90 for "normal" sunrise
$sunrise = date_sunrise(time(), SUNFUNCS_RET_STRING, 51.575363, -4.037476, 96, 0);
$sunrise = (integer) str_replace(":", "", $sunrise);
// echo "Sunrise: ".$sunrise."</br>";
//Sunset
//Set Zenneth to 96 which is Civilian Twilight start. Normally set to 90 for "normal" sunrise
$sunset = date_sunset(time(), SUNFUNCS_RET_STRING, 51.575363, -4.037476, 96, 0);
$sunset = (integer) str_replace(":", "", $sunset);
// echo "Sunset: ".$sunset."</br>";
// get the current date using a 24 digit hour without leading zeros, as an int
$current_time = (Integer) date('Gi');
if ((($current_time >= 0000 && $current_time <= $sunrise) && ($current_time >= $sunset
&& $current_time <= 2359)) && ($_SERVER["REQUEST_URI"] == "/webcams/langland-webcam"
| $_SERVER["REQUEST_URI"] == "/webcams/caswell-webcam" || $_SERVER["REQUEST_URI"] ==
"/webcams/llangennith-webcam" || $_SERVER["REQUEST_URI"] == "/webcams/swansea-webcam"))
{
// Cameras are offline
return true;
}
Yikes. That is one huge if statement. I've broken it up a bit:
if (
(
($current_time >= 0000 && $current_time <= $sunrise)
&& ($current_time >= $sunset && $current_time <= 2359)
// ^^ Should be `||`
) && (
$_SERVER["REQUEST_URI"] == "/webcams/langland-webcam"
| $_SERVER["REQUEST_URI"] == "/webcams/caswell-webcam"
// ^ Should be `||`
|| $_SERVER["REQUEST_URI"] == "/webcams/llangennith-webcam"
|| $_SERVER["REQUEST_URI"] == "/webcams/swansea-webcam"
)
) {
As commented, first thing I notice: You should be using || on the first comparison. Additionally, you later use a single pipe | instead of a ||.
Overall, I would recommend that you refactor this code a bit. Perhaps move the allowed URIs into an array, then use in_array() to check it. Cumbersome ifs like this can cause problems--as you just discovered. Something like this:
$validUris = array("/webcams/langland-webcam", "/webcams/caswell-webcam", "/webcams/llangennith-webcam", "/webcams/swansea-webcam");
if (in_array($_SERVER["REQUEST_URI"], $validUris)) {
if (($current_time >= 0000 && $current_time <= $sunrise) || ($current_time >= $sunset && $current_time <= 2359)) {
// Cameras
return true;
}
}

Categories