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.
Related
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'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...
}
?>
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
}
}
?>
I'm quite new to PHP and I've been asked to write a script that displays an image based on time. I've deducted that I need to use PHP's native 'date' function for this and up until I was asked to implement bank holidays and Christmas into it, all went well.
I tried to add the Christmas and bank holidays in as a separate if statement but that was when disaster struck and I haven't been able to figure out why it's come to a halt. I've tried using an online PHP validator to no avail.
Please don't be afraid to throw details and jargon at me as I really wish to expand my knowledge of PHP and rely less on the support of web forums in the future!
Here is the code:
<?php
// Date-time (day of month, month)
$d = date('j'); // Day of month: Numeric without leading zeroes
$m = date('n'); // Month: As above
$D = date('w'); // Day of week
$H = date('G'); // Hour of day
// REGULAR OPENING TIMES
// Closed before 9am on weekdays
if ($H < 9 && $D <= 5) :
$showroom_img = '/media/gbu0/pagehead/showroom_closed.jpg';
// Open before 5pm (but after 9, as first if statement overrides this) on weekdays
elseif ($H < 17 && $D <= 5) :
$showroom_img = = '/media/gbu0/pagehead/showroom_open.jpg';
// Closed before 10am on Saturdays
elseif ($H < 10 && $D == 6) :
$showroom_img = = '/media/gbu0/pagehead/showroom_closed.jpg';
// Open between 10am and 2pm on Saturdays
elseif ($H < 14 && $H > 10 && $D == 6) :
$showroom_img = = '/media/gbu0/pagehead/showroom_open.jpg';
// Any other time, display closed image :)
else :
$showroom_img = = '/media/gbu0/pagehead/showroom_closed.jpg';
endif;
// Holidays 2014
$xmas_starts = ($d == 23 && $m == 12 && $d > 17);
$xmas_ends = ($d == 26 && $m == 12);
$nyd = ($d == 1 && $m = 1);
// Easter Friday: April 18
// Easter Monday: April 21
// Early Spring B/H: May 5
// Late Spring B/H: May 26
// Summer B/H: August 25
// All bank holidays: 10am - 2pm
if( date >= $xmas_starts && $day <= $xmas_ends ) :
$xmas = true;
elseif( date = $nyd ) :
$nyd = true;
else :
$bankhol = false;
endif;
if( $xmas = true ) :
$showroom_img = "/media/gbu0/pagehead/showroom_xmas.jpg";
elseif( $bankhol = true && date('F', 'j') = strpos('April 18' || 'April 21' || 'May 5' || 'May 26' || 'August 25') || date('w') = 6 ) :
$showroom_img = "/media/gbu0/pagehead/showroom_bhol.jpg";
elseif( date('w') = 7 ) :
$showroom_img = "/media/gbu0/pagehead/showroom_closed.jpg";
else :
$showroom_img = "/media/gbu0/pagehead/showroom_open.jpg";
endif;
?>
First of all. I noticed a couple of bugs :
elseif( date = $nyd ) :
$nyd = true;
and
if( $xmas = true ) :
$showroom_img = "/media/gbu0/pagehead/showroom_xmas.jpg";
In PHP, '=' is the assignment operator, used to assign value to a variable. What you need here is the comparison operator, '=='. For example :
if( $xmas == true ) :
Or you could also do (it's pointed out in a comment already) :
if( $xmas ) :
Rgarding priority, the if statements are going to be checked in the order you write them in your code. TO ensure that a condition gets the highest priority, that has to be checked first.
Have you considered using the switch case function instead of elseif?
Switch case method:
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
}
Read more here: http://www.php.net/manual/en/control-structures.switch.php
if statements cannot conflict, and have no concept of priority. Each one is evaluated and executed in the order it appears, unless it gets skipped by code branching (e.g. an elseif would get skipped if a previous if or elseif in the same chain evaluated to true).
The cause of your problems is most likely your confusion of assignment and equality operators (= and ==).
In all situations, x = y is an assignment; i.e. it assigns the value of y to the variable x. This is the case even if you put it in an if statement, so it's usually something you want to avoid (although there are situations where it's useful).
In all situations, x == y is an equality comparison; i.e. it checks if the value of x is (or can be converted to) the same as the value of y. This is the one you usually want in an if condition.
To put it another way, you want to change this:
if( $xmas = true )
To this:
if( $xmas == true )
You'll need to do the same thing in a couple of other places as well.
While
$w is an Array ( [0] => 4, [1] => 6 )
what does this statement mean:
$day == $w[0] || $day == $w[1] || $day < ((7 + $w[1] - $w[0]) % 7);
Please help. I have not seen the || operator inside other than an if or while statement. Thank you.
EDIT 01:
This is the original function where it is used to find the number of a particular day in a date range:
// find number of a particular day (sunday or monday or etc) within a date range
function number_of_days($day, $start, $end){
$w = array(date('w', $start), date('w', $end));
return floor( ( date('z', $end) - date('z', $start) ) / 7) + ($day == $w[0] || $day == $w[1] || $day < ((7 + $w[1] - $w[0]) % 7));
}
This was not created by me. But I wanted to edit this function because when the end day is a Saturday, it is taking the following Sunday into account too, which is wrong.
It's just a compound boolean expression that returns true if any of the following four sub-expressions is true:
$day == $w[0]
$day == $w[1]
$day < ((7 + $w[1] - $w[0]) % 7)
You were right in one of your comments that the boolean expression gets added as to the integer as 1 or 0.
If you cast a boolean value to an integer then FALSE gets 0 and TRUE gets 1.
If you add variables with different datatypes and one of the variable is an integer, then the other variables are casted to integers, which makes:
var_dump(1+true);
// Result: int(2)
Two links that explain what happens if you use + on different data types and what happens if a certain datatype is casted to an integer:
http://php.net/manual/en/language.types.type-juggling.php
http://www.php.net/manual/en/language.types.integer.php#language.types.integer.casting
$day == $w[0] || $day == $w[1] || $day < ((7 + $w[1] - $w[0]) % 7);
The statement will evaluate (nothing is assigned in the example) to a boolean value of true/false.
The statements are effectively computed in order
For example
true || false || false => true
false || false || false => false
This means that if any of the "subexpressions" are true then the whole expression will evaluate to true. This can be assigned to a variable $v = expression, or use in an if (expression)
|| is the logical OR operator. Please see the documentation for more