Jump to content

Why won't my if statements work?


jakebur01

Recommended Posts

Todays date is between the criteria in if else, but it is not catching it.

 

<?PHP

$today = date('Y-m-d');

$sunday = date('Y-m-d', strtotime('last Sunday', strtotime('last Saturday')));
$saturday = date('Y-m-d', strtotime('last Saturday'));

$firstday = date('Y-m-d', strtotime('next Sunday', strtotime('last Friday', strtotime('first day of last month'))));
$lastday = date('Y-m-d', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month'))));

$thisfirstday = date('Y-m-d', strtotime('next Sunday', strtotime('last Friday', strtotime('first day of this month'))));
$thislastday = date('Y-m-d', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of next month'))));

echo"$today<hr />$firstday<br />$lastday<hr />$thisfirstday<br />$thislastday";

if($firstday>="$today" && $lastday<="$today")
{
$month = date('m', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month')))));
$year = date('Y', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month')))));

echo"1<br />$month|$year";
}






elseif($thisfirstday>="$today" && $thislastday<="$today")
{
$month = date('m', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of next month')))));
$year = date('Y', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of next month')))));
echo"3<br />$month|$year";

}


?>

 

output:

2011-10-10<hr />2011-08-28<br />2011-10-01<hr />2011-10-02<br />2011-10-29

Link to comment
Share on other sites

You can't do a 'numeric' comparison on a formatted date (i.e. 2011-10-10). Once you create a formatted date, it is a string as far as PHP is concerned. So, when you try a comparison such as <=, PHP converts the values to a format that would make sense with such a comparison (i.e. numeric). You might as well be asking

if("car">="boat")

 

Create the dates as timestamps and then compare those.

Link to comment
Share on other sites

This didn't work either:

 

<?PHP

$today = strtotime("now");

$sunday = strtotime('last Sunday', strtotime('last Saturday'));
$saturday = strtotime('last Saturday');

$firstday = strtotime('next Sunday', strtotime('last Friday', strtotime('first day of last month')));
$lastday = strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month')));

$thisfirstday = strtotime('next Sunday', strtotime('last Friday', strtotime('first day of this month')));
$thislastday = strtotime('next Saturday', strtotime('last Friday', strtotime('first day of next month')));

echo"$today<hr />$firstday<br />$lastday<hr />$thisfirstday<br />$thislastday";

if($firstday>="$today" && $lastday<="$today")
{
$month = date('m', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month')))));
$year = date('Y', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month')))));

echo"1<br />$month|$year";
}






elseif($thisfirstday>="$today" && $thislastday<="$today")
{
$month = date('m', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of next month')))));
$year = date('Y', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of next month')))));
echo"3<br />$month|$year";

}


?>

Link to comment
Share on other sites

A slight bit of number/string comparison theory.

 

As long as the strings being compared have the exact same format (requires leading zeros on the month and day and the same separator characters in corresponding positions), so that each corresponding position in each string has the same magnitude in each string and the fields making up the string are ordered left-to-right, most significant digit (year-thousands) to least significant digit (day-ones) you can perform greater-than/less-than and equal comparisons between dates. This is the primary reason why mysql's date/datetime formats are YYYY-MM-DD.

 

If you look at the actual values being used in your comparison statements, you will find that neither comparison is TRUE.

Link to comment
Share on other sites

Lets look at your theory, because I'm with PFM on this one.  The if statement should compare those dates correctly.

 

<?php
//according to your output.
$today = 2011-10-10;
$firstday = 2011-08-28;
$lastday = 2011-10-01;

//so if statement would be:
if(2011-08-28 is greater than or equal to 2011-10-10 AND 2011-10-01 is less than or equal to 2011-10-10) 
//is 2011-08-28 greater than 2011-10-10 == FALSE
//AND
//is 2011-10-01 less than 2011-10-10 == TRUE

So the if statement fails, because one condition is false.

Link to comment
Share on other sites

I suspect you are trying to find if $today is still part of the previous month range or if it is part of the current month range. If so, your comparisons would need to test for those conditions (if today is greater-than or equal to the start value and less-than or equal to the end value) -

 

<?php
if($firstday<=$today && $today<=$lastday)
{

$month = date('m', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month')))));
$year = date('Y', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month')))));

echo"1<br />$month|$year";
}

elseif($thisfirstday<=$today && $today<=$thislastday)
{
$month = date('m', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of next month')))));
$year = date('Y', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of next month')))));
echo"3<br />$month|$year";

}

Link to comment
Share on other sites

I suspect you are trying to find if $today is still part of the previous month range or if it is part of the current month range. If so, your comparisons would need to test for those conditions (if today is greater-than or equal to the start value and less-than or equal to the end value) -

 

<?php
if($firstday<=$today && $today<=$lastday)
{

$month = date('m', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month')))));
$year = date('Y', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month')))));

echo"1<br />$month|$year";
}

elseif($thisfirstday<=$today && $today<=$thislastday)
{
$month = date('m', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of next month')))));
$year = date('Y', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of next month')))));
echo"3<br />$month|$year";

}

 

That is exactly what i'm trying to do!

 

Does this code look ok for finding that out?

$sunday = date('Y-m-d', strtotime('last Sunday', strtotime('last Saturday')));
$saturday = date('Y-m-d', strtotime('last Saturday'));

$firstday = date('Y-m-d', strtotime('next Sunday', strtotime('last Friday', strtotime('first day of last month'))));
$lastday = date('Y-m-d', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month'))));

$thisfirstday = date('Y-m-d', strtotime('next Sunday', strtotime('last Friday', strtotime('first day of this month'))));
$thislastday = date('Y-m-d', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of next month'))));


if($firstday<=$sunday && $sunday<=$lastday)
{
$month = date('m', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month')))));
$year = date('Y', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month')))));
}

elseif($firstday<=$saturday && $saturday<=$lastday)
{
$month = date('m', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month')))));
$year = date('Y', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month')))));
}

elseif($thisfirstday<=$sunday && $sunday<=$thislastday)
{
$month = date('m', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of next month')))));
$year = date('Y', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of next month')))));
}

elseif($thisfirstday<=$saturday && $saturday<=$thislastday)
{
$month = date('m', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of next month')))));
$year = date('Y', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of next month')))));
}

else
{
$month="00";
$year="0000";
}

Link to comment
Share on other sites

If you were to simply echo out the values you *think* you have you would see the problem. I added these lines before the first if statements and the results are indicated in the comments.

 

echo "Firstday: $firstday<br>\n"; // Firstday: 1969-12-28
echo "Sunday: $sunday<br>\n"; // Sunday: 2011-10-02
echo "Lastday: $lastday<br>\n"; // Lastday: 1969-12-27

 

Your logic for determining the days seems overly complicated to me. If you were to explain what days you are trying to define, there might be a simpler method than all the nested strtotime() functions.

Link to comment
Share on other sites

I did echo them out, and it works.  There is no problem now as far as I know.  I just wanted a second eye on the code.  Basically, what ever day the last Friday of the month lands on...  the Saturday following will make up the last day of the months period.

 

I am running a scheduled task ever Thursday morning.  I am using this code to determine which month period the previous week falls into.

Link to comment
Share on other sites

I did echo them out, and it works.

 

Hmm. . . I ran your exact code and got the results above. $firstday and $lastday are set in 1969! You are saying you are getting different results? Some simple testing found that "first day of last month" is not a valid string to use with strtotime() (at least in my PHP install). What version are you using?

 

As I stated, your nested strtotime() functions seem overly complicated for determining the days. Is the Saturday following the last Friday of the previous month the only day you really need?

Link to comment
Share on other sites

Well, I'm on 5.1.6 and those strtotime() values don't work for me. I don't know that there have been updates to the parsing for strtotime() or not in those smaller updates. But, unless your production server is on the same version as you, using that would likely not work.

 

But, as I stated previously you are over complicating things. Here is one simpler solution that would get you the Saturday immediately following the last Friday of the previous month

$sat_following_last_friday_of_last_month =strtotime(date('Y-m').'-2 last Saturday');

 

It basically starts on the 2nd of the current month and looks for the previous (i.e. last) Saturday.

 

EDIT: Could you please post the values for all the variables you are using in your if/else statements, and what results you are getting for $month and $year?

Link to comment
Share on other sites

PHP Version 5.3.6

Windows NT WAVELINK 5.1 build 2600 (Windows XP Professional Service Pack 3) i586

Mar 17 2011 10:46:06

 

code

$sunday = date('Y-m-d', strtotime('last Sunday', strtotime('last Saturday')));
$saturday = date('Y-m-d', strtotime('last Saturday'));

$firstday = date('Y-m-d', strtotime('next Sunday', strtotime('last Friday', strtotime('first day of last month'))));
$lastday = date('Y-m-d', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month'))));

$thisfirstday = date('Y-m-d', strtotime('next Sunday', strtotime('last Friday', strtotime('first day of this month'))));
$thislastday = date('Y-m-d', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of next month'))));


if($firstday<=$sunday && $sunday<=$lastday)
{
$month = date('m', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month')))));
$year = date('Y', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month')))));
echo"1<br />$month|$year";
}

elseif($firstday<=$saturday && $saturday<=$lastday)
{
$month = date('m', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month')))));
$year = date('Y', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of this month')))));
echo"2<br />$month|$year";
}

elseif($thisfirstday<=$sunday && $sunday<=$thislastday)
{
$month = date('m', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of next month')))));
$year = date('Y', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of next month')))));
echo"3<br />$month|$year";
}

elseif($thisfirstday<=$saturday && $saturday<=$thislastday)
{
$month = date('m', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of next month')))));
$year = date('Y', strtotime('-10 days', strtotime('next Saturday', strtotime('last Friday', strtotime('first day of next month')))));
echo"4<br />$month|$year";
}

else
{
$month="00";
$year="0000";
}echo "<hr />Firstday: $firstday<br>\n"; // Firstday: 1969-12-28
echo "Sunday: $sunday<br>\n"; // Sunday: 2011-10-02
echo "Lastday: $lastday<br>\n"; // Lastday: 1969-12-27

 

output

3

10|2011

Firstday: 2011-08-28

Sunday: 2011-10-02

Lastday: 2011-10-01

Link to comment
Share on other sites

EDIT: Could you please post the values for all the variables you are using in your if/else statements, and what results you are getting for $month and $year?

 

With the data you have posted I can only validate that the first if() condition should resolve to false, and it is since you are getting the results from the third condition. Need the values for the other variables used to see why the third condition is resulting in true. Need values for $saturday, $thisfirstday, and $thislastday.

 

 

Link to comment
Share on other sites

OK, even though the strtotime() function isn't working for me with those strings, looking at how your data values are being calculated I was able to determine that your variables should be getting the following values.

$sunday = '2011-10-02';
$saturday = '2011-10-08';
$firstday = '2011-08-28';
$lastday = '2011-10-01';
$thisfirstday = '2011-10-02';
$thislastday = '2011-10-30';

 

Then analyzing your if/else conditions with those values, the third one would be the correct result. Which is what you got. However, I will reiterate that your logic for determining those dates is over complicated. I think you can make the entire script (determining the dates and the if/else statements) much, much simpler. Please provide and explanation of what you are trying to achieve and I'll be happy to give it a try.

Link to comment
Share on other sites

OK, I went back and read some of your comments and think I have a much simpler solution for you. You state:

Basically, what ever day the last Friday of the month lands on...  the Saturday following will make up the last day of the months period.

 

I am running a scheduled task ever Thursday morning.  I am using this code to determine which month period the previous week falls into.

 

So, a week starts on a Sunday and ends on a Saturday and the same is true for your month periods. So, there is no need to determine the range of the week and test against the range of the months. You only need to test the end of the previous week to see what month it falls into (or you could just test the beginning of the previous week).

 

But, if you need to get the start and end of the previous week you should use something like this

$prev_week_end   = date('Y-m-d', strtotime('last Saturday'));
$prev_week_start = date('Y-m-d', strtotime('last Sunday', $prev_week_end));

Note how I reused the variable $prev_week_end to calculate $prev_week_start instead of using nested strtotime() and having to recalculate.

 

Anyway, with just the $prev_week_end variable you can determine which month to apply. Based upon your previous statement, the Saturday following the last Friday in a month is the end of that month's period. So, logically, the Friday's are always in the actual month that the month period applies to. It is only with some Saturday's where the Saturday is actually in the month after the one it is supposed to apply to.

 

To make a long story short you can determine the applicable month to use for the report by checking what month of the Friday in the previous week.

$prev_week_end   = date('Y-m-d', strtotime('last Saturday'));
$prev_week_start = date('Y-m-d', strtotime('last Sunday', $prev_week_end));

$prev_week_friday = date('-1 day', $prev_week_end);
$report_month = date('m', $prev_week_friday);
$report_year  = date('Y', $prev_week_friday);

 

All that other code you had would be unnecessary. This is based upon what I understood from your previous statements. If it is not correct, please elaborate.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.