Display content on all days besides on Wednesday between specific times - php

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...
}
?>

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

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

Every year last four months color different from other months in php

I have calculate the employee experience(months) in joining date to current date. I want every **last four months in a year** (eg 9-12 and 21-24 and 33-36 etc....) employeeexperience have shown as different color.(php code)
First year calculation has no problem after the years the conditions is not satisfy the `above criteria.`
This is my code but i need satisfy above criteria.
if($months % 9 == 0 || $months % 10 == 0 || $months % 11 == 0 || $months % 12 == 0)
{
<span style="color:green;"><?php echo $u_tot_exp;?><span>
}
else
{
<span style="color:black;"><?php echo $u_tot_exp;?><span>
}
The problem is your misunderstanding of the modulus operator %. You're using $months % 11 == 0 to mean "if the month is the eleventh in a year", but that isn't what it means. It actually means "if the month is a multiple of eleven after the first month". So this means November one year (the eleventh month), then October the next (the 22nd) then September (the 33rd).
The effect multiplies with % 10 or % 9. If we assume that the first year is 2014, it will select the following months:
2014: September, October, November, December
2015: June, August, October, December
2016: March, June, September, December
The % operator works by calculating the remainder when the number on the left is divided by the number on the right. Since we're working in years, we always need to divide by 12. You then want to check if the number left over is between 3 (i.e. September) and 0 (i.e. December).
$monthsToGo = $months % 12; // months remaining in the year
if ($monthsToGo >= 3) { // i.e. after September
echo "<span style=\"color:green;\">$u_tot_exp<span>";
} else {
echo "<span style=\"color:black;\">$u_tot_exp<span>";
}
Note that I have also fixed the code that outputs your HTML.
**Finally i have get the result using this code.**
<?php
$currentDate = date("d-m-Y");
// joining_date is name of field in DB.
$date1 = date_create("".$u_joining_date.""); //
$date2 = date_create("".$currentDate."");
$diff12 = date_diff($date2, $date1);
$hub_days = $diff12->days;
$months = $diff12->m;
$years = $diff12->y;
$tot_months = (($years * 12) + $months);
//$monthsToGo = $months % 12;
// months remaining in the year
//$monthsmod = $monthsToGo % 10;
if ($tot_months != 0)
{
if($months % 9 == 0 || $months % 10 == 0 || $months % 11 == 0 || $months % 12 == 0)
{
?>
<span style="color:green;"><?php echo $tot_months;?>month(s)<span>
<?php
}
else
{
?>
<span style="color:black;"><?php echo $tot_months;?>month(s)<span>
<?php
}
}
?>

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.

Categories