I have the following if statement:
$current=date('d/m/Y');
$next_year=date('y')+1;
$q1a='01/04/'.date('Y');
$q1b='30/06/'.date('Y');
$q2a='01/07/'.date('Y');
$q2b='30/09/'.date('Y');
$q3a='01/09/'.date('Y');
$q3b='31/12/'.date('Y');
$q4a='01/01/20'.$next_year;
$q4b='31/03/20'.$next_year;
if (($current > $q1a) && ($current < $q1b))
{
$currentquarter='Q1';
}
elseif (($current > $q2a) && ($current < $q2b))
{
$currentquarter='Q2';
}
elseif (($current > $q3a) && ($current < $q3b))
{
$currentquarter='Q3';
}
elseif (($current > $q4a) && ($current < $q4b))
{
$currentquarter='Q4';
}
echo $currentquarter;
BUT its returning Q1 even though it should be Q3 as todays date falls between 01/09/2017 and 31/12/2017
You're not comparing dates, you're lexically comparing strings. E.g.:
'21/03/2017' > '01/04/2017'
2 is larger than 0, so this will be true. Character by character the first string sorts "larger than" the second. Again, it's a lexical comparison, not a numeric comparison.
At the very least you need to reverse the endianness to Y/m/d to get the correct result; but you should really construct DateTime objects or UNIX timestamps to do actual date/numeric comparisons properly.
For date comparison in PHP you can use strtotime() function
$date1 = '07/28/2018';
$date2 = '07/28/2017';
if(strtotime($date1) < strtotime($date2))
{
echo 'Date2 is greater then date1';
}
else
{
echo 'Date1 is greater then date2';
}
This would help you simplify the comparison to retrieve the current quarter.
function CurrentQuarter(){
$n = date('n');
if($n < 4){
return "1";
} elseif($n > 3 && $n <7){
return "2";
} elseif($n >6 && $n < 10){
return "3";
} elseif($n >9){
return "4";
}
}
Code Source
Related
I'm a newbie on php and I need help on how to get a loop such as for or foreach to run an array to then define what year in the array is leap or not leap.
$year = '2012,396,300,2000,1100,1089';
$y2 = explode(',',$year);
if (($year % 4 == 0) || ($year % 400 == 0 && $year % 4 != 0)) {
echo "True";
} else{
echo "False";
}
Thank you for your time
Try like this,
// this will check a year is leap year or not and return true/false
function check_leap_year($year){
return ((($year % 4) == 0) && ((($year % 100) != 0) || (($year % 400) == 0)));
}
$years = '2012,396,300,2000,1100,1089';
$y2 = explode(',',$years);
foreach($y2 as $year){
$test_leap_year = check_leap_year($year);
if ($test_leap_year) {
echo "True";
} else{
echo "False";
}
}
put conditions inside loop
if((leapyear%4==0 && leapyear%100!==0)||leapyear%400==0)
{
its a leap year
}
I'm having an issue with some code that someone else has previously worked on.
The goal is to iterate through a directory and push any files that are within a certain date range to an array (files are in mmddyyy.txt format).
The (terribly named, not by my own doing) variables in the code represent the following:
$aYear - A given year, read in from a text file. This variable changes during every iteration of the loop. The same goes for $aMonth and $aDay.
$sYear1 - Start year. $sMonth1 and $sDay1 are used in respect to $sYear1.
$sYear2 - End year. $sMonth2 and $sDay2 are used in respect to $sYear2.
$isGood - File will be added to the array.
$isGood = false;
if($aYear >= $sYear1 && $aYear <= $sYear2)
{
if($aYear == $sYear1)
{
if($aMonth == $sMonth1)
{
if($aDay >= $sDay1 && $aDay <= $sDay2)
{
$isGood = true;
}
}
else
{
if($aMonth >= $sMonth1 && $aMonth <= $sMonth2)
{
$isGood = true;
}
}
}
else if($aYear == $sYear2)
{
if($aMonth == $sMonth2)
{
if($aDay <= $sDay2)
{
$isGood = true;
}
}
else
{
if($aMonth <= $sMonth2)
{
$isGood = true;
}
}
}
else
{
$isGood = true;
}
}
if($isGood)
{
//echo "Found good article";
$a = $a . "===" . $file;
array_push($result, $a);
}
I'm not getting the results that I expected. I'm looking for some help as to how I can simplify this code and get it working properly. I do need to keep this solution in PHP.
Thank you in advance.
It seems to me Month statement if($aMonth >= $sMonth1 && $aMonth <= $sMonth2) needs work
eg start date- 03 Aug 2013 end date- 04 Sep 2016 and check date say 08 Nov 2013
would make isGood=false whereas it should be true.
Removing && $aDay <= $sDay2 and && $aMonth <= $sMonth2 should work.
As #Sandeep pointed out you're issues are with:
if ($aMonth >= $sMonth1 && $aMonth <= $sMonth2)
and
if ($aDay >= $sDay1 && $aDay <= $sDay2)
as you don't need to be comparing the date with end dates as well.
That being said you can clear up your code completely by doing something like:
$date = (new DateTime)->setDate($aYear, $aMonth, $aDay);
$start = (new DateTime)->setDate($sYear1, $sMonth1, $sDay1);
$end = (new DateTime)->setDate($sYear2, $sMonth2, $sDay2);
if ($start <= $date && $date <= $end) {
//echo "Found good article";
$a = $a . "===" . $file;
array_push($result, $a);
}
Hope this helps!
im trying to create a script that generates a number and depending on the number generated it prints something specific but it's not working properly, Heres the code:
<?php
for($zz = 1; $zz <= 20; $zz++) {
$rangen = rand(1,100);
$a = (1 <= 0) && (0 <= 7);
$b = (8 <= 0) && (0 <= 17);
echo ("<br>".$rangen . "<br>");
if($a) {
echo "a";
} elseif ($b) {
echo "b";
} else {
echo "c";
}
}
?>
The error is that it keeps printing "c" no matter what the number is.
If anyone could help that would be great, thanks.
Your conditions are all wrong. Your comparing the same numbers and never using $rangen, this is why you obtain the same result each time.
1 <= 0 and 8 <= 0 will always return false which is why you always go to the else statement.
I have an array of objects (goals) that I want to sort based on 2 of their properties, the Status and the DueDate.
Here are the rules:
Statuses:
Design
Approved
In Progress
Completed
Archived
If a goal has a status of 4 (completed) or 5 (archived) then the DueDate doesn't matter.
If a goal is neither 4 nor 5 and its DueDate is less than now then it is "Overdue" and should be at the top
If a goal is not "Overdue" then the order of statuses determines it's position (lowest to highest)
If $a and $b are both "Overdue" then the one with the earliest DueDate is more important
The order should be:
Overdue
Design
Approved
In Progress
Completed
Archived
Here is the last thing I tried:
function cmp($a, $b)
{
$now = new DateTime("now");
$aDueDate = new DateTime($a->GetDueDate());
$bDueDate = new DateTime($b->GetDueDate());
if($a->GetStatus() != 4 && $a->GetStatus() != 5 && $b->GetStatus() != 4 && $b->GetStatus() != 5){
if($aDueDate < $now || $bDueDate < $now){
if($aDueDate == $bDueDate){
return 0;
}
return ($aDueDate < $bDueDate) ? -1 : 1;
}
}
elseif(($a->GetStatus() == 4 || $a->GetStatus() == 5) && ($b->GetStatus() != 4 && $b->GetStatus() != 5)) {
return -1;
}
elseif(($a->GetStatus() != 4 && $a->GetStatus() != 5) && ($b->GetStatus() == 4 || $b->GetStatus() == 5)){
return 1;
}
if ($a->GetStatus() == $b->GetStatus()) {
return 0;
}
return ($a->GetStatus() < $b->GetStatus()) ? -1 : 1;
}
Which orders the array like so:
Completed
Archived
Overdue
Design
Approved
In Progress
The following should meet your requirements:
function cmp($a, $b) {
$now = new DateTime("now");
$aDueDate = new DateTime($a->GetDueDate());
$bDueDate = new DateTime($b->GetDueDate());
$aStatus = $a->GetStatus();
$bStatus = $b->GetStatus();
$incompleteStatuses = array(1, 2, 3);
// use date if same status (might not be needed)
if ($aStatus == $bStatus) {
return ($aDueDate < $bDueDate ? -1 : 1);
}
// if A is overdue:
if (in_array($aStatus, $incompleteStatuses) && $aDueDate < $now) {
// if B is overdue too, only consider dates
if (in_array($bStatus, $incompleteStatuses) && $bDueDate < $now) {
return ($aDueDate < $bDueDate ? -1 : 1);
}
return -1; // A definitely first
}
// if B is overdue:
elseif (in_array($bStatus, $incompleteStatuses) && $bDueDate < $now) {
return 1; // B definitely first (we know A is not overdue from above)
}
// both A and B are not overdue; order by status
return $aStatus - $bStatus;
}
Here's a test codepad.
im not sure this is a good question to post but here is my issue. I have an if statement that is getting way too long and i was wondering if there is some other kind of syntax to shorten it out:
if (($time1 <= $one_day)&&
($time2 <= $one_day)&&
($time3 <= $one_day)&&
($time4 <= $one_day)&&
($time5 <= $one_day)&&
($time1 != NULL)&&
($time2 != NULL)&&
($time3 != NULL)&&
($time4 != NULL)&&
($time5 != NULL)){
//do sometihng
}
this is one example but i have a similar one that goes up to ..&&($time15 <= $one_day).
the statement is pretty self explanatory, $time1, $time2, etc can come back empty so i have to check if they are NULL or not
any ideas?
thanks
You can put the common stuff in a function:
function validate_time($time, $one_day) {
return $time <= $one_day && $time != NULL;
}
if (validate_time($time1, $one_day) &&
validate_time($time2, $one_day) &&
validate_time($time3, $one_day) &&
validate_time($time4, $one_day) &&
validate_time($time5, $one_day)) {
// do something
}
You may want to refactor code and eliminate the need for copying & pasting those checks. Another way to get the job done:
while (true) {
foreach (array($time1, $time2, $time3, $time4, $time5) as $time) {
if ($time > $one_day || $time == NULL) {
break 2;
}
}
// do something
break;
}
The above could be put in a function as well which would make the while-loop and break keyword redundant. Replace break 2 by return then.
Using an array for you variables would help. The you can iterate over them and check.
Put the times in an array and have a for loop to do the checking.
Instead of using 15 similar but different variables, consider using an array.
If you must (or want to) keep the original variable names and not use an array here is the good solution (for $time1 to $time5):
$ok = true;
for ($i = 1; $i <= 5; $i++)
{
$var =& ${'time'.$i};
if ( ! ($var <= $one_day && $var != NULL))
{
$ok = false;
}
}
if ($ok)
{
//do something
}
You can set all the values into an Array and compare it using an For Loop.
A functionised version which should help with your reuse. This is similar to Lekensteyns code.
$times = array(
'time',
'time',
'time',
'time',
'time',
);
function validateTime($checks, $limit)
{
foreach($checks as $check) {
if($check == null || $check > $limit) {
return false;
}
}
return true;
}
if(validateTime($times,$one_day) == true) {
//codey code.
}