Help with IF statement/query that is looking for 2 other variables

i’m trying to modify a custom shortcode that was written for me in the past.

The overall code defines a ‘tag’ that is used in the shortcode…this is called “$month_filter”.
The overall code defines a date by “this month” and “last month” which is only a ‘first day of month’ format for the data being looked at.

Current IF statements is:
if ( ($month_filter == ‘current’) and $lead_date < $first_of_month)

I’m trying to create a new statement looking for data that is dated less than the “first of this month” and greater than or equal to the “first of last month”.

I have tried a lot of variations of this:
if ( ($month_filter == ‘last’) and $lead_date < $first_of_last && $lead_date > $first_of_month)

But this is not working.
Keep in mind this is actually a reverse type of look up…my code is saying "if he wants data from last month, look at the data and see if it was dated prior to the First of Last Month and also if it was dated after the First of This Month…and if it meets both of those criteria…skip past that data.
This way, it is only displaying what is left over in between those two criteria.

I’ve been racking my brain for hours on this and have tried many different variations of that line…Please shoot me some guidance…I feel like i’m overlooking something simple.

It would be more helpful if you posted all the code.

[left]here is the snippet relative to the issue i’m having.
The first “if” looks to see if the tag “current” was used in the shortcode…and if so IGNORE any data where the “lead date” is less than the First of the Current Month.
The second “if” is the one I’m having issues with. I need it to see if the tag used in the shortcode is “last” (which is defined and tested working with the $fist_of_last string above)…and if that tag is used…IGNORE any data were the “lead date” is LESS THAN the First of Last Month AND IGNORE any data were the “lead date” is greater than or equal to the First of Current Month.

The version of the code below is actually in use right now. It is showing data where lead date is from Last month…and current month. I just need help reworking that second IF to exclude the current month data.

Thanks in advance.

[code]
$lead_date = date(substr($lead->createdTime, 0, 10));
$first_of_month = date(‘Y-m-01’);
$first_of_last = date(‘Y-m-01’, strtotime("-1 month"));

        #if month_filter is defined, skip unwanted leads

		if ( ($month_filter == 'current') and $lead_date < $first_of_month)
			continue;

		if ( ($month_filter == 'last') and $lead_date < $first_of_last)
			continue;

		if ( ($month_filter == 'before') and $lead_date > $first_of_last)
			continue;

[/code][/left]

Kevin,
I can appreciate your desire to see the entire code, however it is not relevant to the fact I only need help forming a proper IF statement with multiple variables.

I just am not comfortable posting the entire code…when 98% of it has nothing to do with this question.

If you are unable or unwilling to help because of this, thank you for for the initial attempt.

[php]<?php
$lead_date = ‘2014-01-10’;
$first_of_month = date(‘Y-m-01’);
$first_of_last = date(‘Y-m-01’, strtotime("-1 month"));

if ($lead_date >= $first_of_last and $lead_date < $first_of_month) {
echo ‘yay’;
} else {
echo ‘nay’;
}[/php]

I would probably just do a daterange for last month and check if lead_date is in that range though.

Jim:
Thanks for the input.

I have to account for the initial part of the statement though: IF ($month_filter == ‘last’)

This represents a ‘tag’ in a custom shortcode (in Wordpress). So what my statement needs to do (literally) is this:

If the tag used is “last”, then ignore all data with Lead_Date older than the First of Last Month as well as ignore all data with the Lead_Date the same as or newer than the First of the Current Month.

I don’t need the ‘echo’ because the false is to “continue” thru the filtering list (as shown in the snippet I included).

I’ve tried variations of what you provided in your IF line…(although it should be < $first_of_last and >= $first_of_month since this is an If that determines what data to IGNORE not what data to include).

For example…I have tried this…
if ( ($month_filter == ‘last’) and (($lead_date < $first_of_last) && ($lead_date >= $first_of_month)))
as well as this…
if ( ($month_filter == ‘last’) and (($lead_date < $first_of_last) and ($lead_date >= $first_of_month)))

But it failed and brought in all data from all lead dates.
It sure feels like i’m just missing something simple.

I used some old code that I had laying around and modified it ;D, so ignore the function and it’s not elegant as JimL’s (I would use his), but this is one way how you could get user interaction.
[php]<?php
$firstOfMonthTS = strtotime(“First Day of This Month”);
$firstOfMonth = date(‘Y-m-d’, $firstOfMonthTS);
$firstofLastMonthTS = strtotime($firstOfMonth . ’ -1 Month’);
$firstOfLastMonth = date(‘Y-m-d’, $firstofLastMonthTS);
date_default_timezone_set(‘America/Detroit’);
function daysTill($timestamp, $dateCheck) {
$datetime1 = new DateTime($timestamp);
$datetime2 = new DateTime($dateCheck);
$interval = $datetime1->diff($datetime2);

return $msg = 'There are ’ . $interval->days . ’ days until ’ . date(“m-d-Y”, strtoTime($dateCheck));
}
if (isset($_POST[‘submit’]) && $_POST[‘submit’] == ‘submit’) {

$timestamp = strtotime("Now");
$todaysDate = date('Y-m-d', $timestamp);

$dateCheckTS = strtotime($_POST[‘userDate’]);
$dateCheck = date(‘Y-m-d’, $dateCheckTS);

if ( $dateCheckTS < $firstOfMonthTS && $dateCheckTS >= $firstofLastMonthTS) {
	$msg = 'The ' . $dateCheck . ' is between ' . $firstOfMonth . ' and ' . $firstOfLastMonth . ' so data is used';
} else {
	$msg = 'The ' . $dateCheck . ' is NOT between ' . $firstOfMonth . ' and ' . $firstOfLastMonth . ' so data isn\'t used';
}

}

?>
<!doctype html>

How Many Days Until? body { font-size: 100%; } form { display: block; width: 250px; border: 2px solid green; padding: 30px; margin: 0 auto; } .msgBox { display: block; color: green; width: 800px; height: 50px; margin: 10px auto; } h1 { font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif; font-size: 1.4rem; text-align: center;
	}
	.dateStyle {
		display: block;
		width: 120px;
		height: 40px;
		font-family: Arial, Helvetica, sans-serif sans-serif;
		font-size: 1.4rem;
		color: #C00;
		margin: 0 auto;
	}
	.dateInput {
		display: block;
		font-family: Arial, Helvetica, sans-serif;
		font-size: 1.4rem;
		text-align: center;
		border: 2px solid green;
		color: #090;
		width: 190px;
		padding: 5px;
		margin: 0 auto;
	}
	.submitStyle {
		display: block;
		font-family: Arial, Helvetica, sans-serif;
		font-size: 1.8rem;
		text-align: center;
		text-transform: uppercase;
		color: #fff;
		background-color: #C00;
		border: none;
		width: 120px;
		height: 40px;
		margin: 10px auto;
	}
</style>

<?php echo (isset($msg)) ? $msg : "To use or not to use?"; ?>

Enter Date

[/php]

Just add it to the condition

This works:
[php]<?php

$month_filter = ‘last’;
$lead_date = ‘2014-02-10’;
$first_of_month = date(‘Y-m-01’);
$first_of_last = date(‘Y-m-01’, strtotime("-1 month"));

if ($month_filter == ‘last’ and $lead_date >= $first_of_last and $lead_date < $first_of_month) {
echo ‘yay’;
} else {
echo ‘nay’;
}[/php]

updated to:

[code] $lead_date = date(substr($lead->createdTime, 0, 10));
$first_of_month = date(‘Y-m-01’);
$first_of_last = date(‘Y-m-01’, strtotime("-1 month"));

        #if month_filter is defined, skip unwanted leads

		if ( ($month_filter == 'current') and $lead_date < $first_of_month)
			continue;

		if ( ($month_filter =='last') and $lead_date < $first_of_last and $lead_date >= $first_of_month)
			continue;

		if ( ($month_filter == 'before') and $lead_date >= $first_of_last)
			continue;[/code]

Still not filtering properly. It’s trying to pull in all data from all dates which causes an error from my database that we’re pulling from due to limits in place.

Just to be clear…

the “Current” and “Before” tags/filters work perfectly fine.

I’m trying to ADD IN this “Last” tag because I have the need now to show more than just those two previous options.

And the only real difference in what i’m doing is i’m adding one more “and” to the statement.

Charles, this is exactly why I asked for the whole code. You are not making it easy to help you. You have had the best active programmers this board has to offer respond to you. If you dont want your code public, which I can understand, you can privately message any one of us with it. You may not think it is relevant code, but your still not getting a solution from the best of this board. This probably is a very simple problem, but you have not given us what we need to properly help you.

We would like to help you, but we need all the pieces.

Kevin: Again I appreciate the help…and I’m far from being any type of experienced programmer myself, so believe me it is appreciated.

That being said…I’ve given a snippet of code around where I’m trying to edit.

I stated that certain pieces work exactly as they should, as well as I have defined what they do:
if ( ($month_filter == ‘current’) and $lead_date < $first_of_month)
(looks for my custom shortcode with the additional tag of “current” and displays only ‘current month’ data by way of ignoring all other data with a lead date less than the first of the month)

I have provided the text and required definition of the customization I am attempting to make:
if ( ($month_filter ==‘last’) and $lead_date < $first_of_last and $lead_date >= $first_of_month)
(looks for my custom shortcode with the additional tag of “last” and displays only the data with a lead date that does not have a less than the First of Last Month and ALSO does not have a lead date greater than or equal to the first of the current month.

I know the problem does not lie elsewhere in my code.
I know the problem lies somewhere in the structure of this one line of the code:
if ( ($month_filter ==‘last’) and $lead_date < $first_of_last and $lead_date >= $first_of_month)

I am fairly confident it has to do with the addtional lead_date filter.
If i use the code like this:
if ( ($month_filter ==‘last’) and $lead_date < $first_of_last))
It will do exactly what it shows it should do… If the shortcode tag “last” is present, ignore all data with lead date less than First of Last Month.

I need to take that one tiny step further and show only “last month” by adding on to the string additional filtering to ignore any data with a lead date that is greater or equal to the first of this month.

Does that help?

I was not thinking the problem was elsewhere. I don’t want to get into debugging techniques, but I liken it to if your car does not work. You don’t just take the alternator to the mechanic, you take the whole car. There could be other things we need to see to get at the heart of the problem. There are just certain steps to problem solving and debugging.

You just recently revealed we are dealing with wordpress. That can be a very important factor even down to the version which really should have been told in the first post. We can understand code much better than your descriptions although we need that too.

JimL is one of the most knowledgeable programmers on this board and you still dont have the answer. If your code is so private, see if someone will sign a NDA. You obviously want a solution and we obviously want to help you, but as you can see by the many posts and no solution, what you have “said” is not enough for this issue.

No. I know my code has other flaws actually. And I am not interested in trying to rewrite it at this point…and I absolutely am not trying to get anyone to do it for free.

I’m simply trying to get some help understand why a specific “IF” statement isn’t working for me the way I feel it should.

There’s nothing about this section of the PHP code that would be effected by WP. It’s a complex IF statement that I’m having trouble getting the syntax correct for.

What if I changed things up and said…

hey guys…can you find the syntax error in this statement:
if ( ($month_filter ==‘last’) and $lead_date < $first_of_last and $lead_date >= $first_of_month)
The symptom is
“and $lead_date < $first_of_last”
and
“and $lead_date >= $first_of_month”
are being ignored

BTW…if I know my alternator is bad…i WOULD take it directly to the ‘mechanic’. And that’s what I’m doing here. But the alternator is this dang-blasted IF statement…and the car is the rest of my code which is working perfectly fine (given that it does have some performance flaws I am aware of)

If my above revision of the question doesn’t spark a solid answer from anyone…I’ll consider posting the full code to someone privately.

Thanks again.

There is not any errors in the syntax. I would end up giving you something like Jim gave you which you said does not work for you. Since we are dealing with wordpress, I dont need the entire wordpress files, Just the version you are using and ANY code pages that have been modified.

And I said if the CAR does not work, not the alternator. You suspect the problem is just the alternator, but the alternator is just a single part of the entire system.

On another note, we all only have so much time to volunteer our help here. If you make it harder for us to help, you are less likely to get a solution. I for one have exhausted my effort on this. If and when you provide code I will take a look again.

Sponsor our Newsletter | Privacy Policy | Terms of Service