PHP on getting time before 6:30PM - 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)

Related

Show text if current time is between 9:00pm and 10:30pm on friday

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

Pulling in data around/after midnight

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

Display content on all days besides on Wednesday between specific times

I'm trying to display a div every day besides Wednesday between 3pm CST & Midnight or 11:59pm on that same Wednesday. Below is the code I have so far that is not working? This is my first time with the if date function so it's all new to me.
<?php
if (date('w') == '1' | == '2' | == '4' | == '5' | == '6' | == '0') {
//info to show Mon, Tue, Thur, Fri, Sat, Sun all day.
}
else if (date('w') == 3){
if (date('H') >= 15 && date('H') < 0) {
//Content to show on Wednesday from 3pm to midnight
}
}
?>
| is the bitwise operator. || is the logical OR operator.
PHP operators.
Also, here's a more efficient version of what you wanted:
if (date('w') == 3) {
if (date('H') >= 15) {
//Content to show on Wednesday from 3pm to midnight
}
} else {
//info to show Mon, Tue, Thur, Fri, Sat, Sun all day.
}
Your if comparison needs the date variable in each case, a better idea might be to use a temporary array to check:
if(in_array(date('w'), array(0, 1, 2, 4, 5, 6))) {
// info to show except Wed
} else {
// w is 3
if(date('H') >= 15) {
// Content to show on Wed from 3pm to midnight
}
}
Note:
The else is implicit that the day number will be 3, since you've specified all other possibilities in the first clause.
The hour will never be less than zero, but if it's between 3pm and midnight it will always be greater than 15, so removed the second part
Your syntax for comparing with different values is all wrong. You have to repeat the comparison, and combine them with the boolean or operator ||
$w = date('w');
if ($w == 1 || $w == 2 || $w == 4 || $w == 5 || $w == 6 || $w == 0) {
//info to show Mon, Tue, Thur, Fri, Sat, Sun all day.
} else if ($w == 3 && date('H') >= 15) {
//Content to show on Wednesday from 3pm to midnight
}
Of course, you could also simplify the first condition to if ($w != 3).
Your test date('H') < 0 can never be true -- there are no negative hours on the clock. I guess you meant < 24, but that's unnecessary, since all hours are less than 24.
Three things I can see that are not correct:
| == should be || == , but I've simplified into a single if statement as it appears you're only interested in knowing if it's after 3pm on a Wednesday.
Logically, you cannot be >= 15 && <0 - so this will never be true.
The date() function returns a string and you are comparing with an integer == 3 (PHP will probably juggle types just fine but I've thrown in an intval() for a belt-and-braces approach).
Try this:
<?php
if (intval(date('w')) == 3 && intval(date('H')) >= 15) {
// it's after 3pm on a Wed...
}
?>

Meaning of a Function

Here is the function I'm not understanding:
$year = date("Y");
$leapYear = false;
if((($year%4==0) && ($year%100==0)) || ($year%400==0))
$leapYear = true;
Does that mean that, if the (($year is divided by 4 but not divided by 100) or (the year is divided by 400)) that is a leap year?
I'm new to PHP, please anyone help me to understand the statements.
I think you're likely confused because the code you've provided doesn't find leap years. Leap years are divisible by 4 but NOT 100.. unless they are also divisible by 400. If you flip the logic around, it's easier to look at. If it's divisible by 400, it's a leap year, no matter what. Otherwise, it's a leap year if it's divisible by 4 and NOT divisible by 100.
// pseudocode
if ( year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
return true;
else
return false;
You can play around with it and just say:
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)
The && means both sides have to be true in order for && to return true.
This is basically what is says in pseudo code
if(year is divisible by 4 or 100) {
leapyear = true
} else if (year is divisible by 400) {
//this will never be hit because it would meet the first case
leapyear = true
}
Based on your code, your second guess is correct.
if((($year % 4==0) && ($year % 100==0)) || ($year % 400==0))
means that EITHER of these must be true to be a leap year:
($year%4==0) && ($year%100==0)
$year%400==0
So a year is a leap year under these conditions:
year is divisible by 4 and divisible by 100
OR
year is divisible by 400
However, the code you posted is wrong in that it doesn't correctly define a leap year.
A year is a leap year if it is divisible by 4, except when it is divisible by 100 and not divisible by 400. So in your if statement the ($year % 100 == 0) should be ($year % 100 != 0)
So the proper logic would be this:
if ((($year % 4 == 0) && ($year % 100 != 0)) || ($year % 400 == 0))
$leapYear = true;
Regarding the &&:
&& means AND, and the expression A && B is either true or false.
By definition, A && B is true only if both A and B are true. If any of them are false, then A && B is false.

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