comparing two values doesn't seem to work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

comparing two values doesn't seem to work

Post by someguyhere »

I've used echo elsewhere in this script to double check the values being compared, and they both appear to contain the same value (2011-01 in this case)

Code: Select all

		if(date('Y-m') == $current_page_year . '-' . $current_page_month){
			$upcoming_events_start = $current_page_year . '-' . $current_page_month . '-' . date('d');
			$upcoming_events_end = date('Y') . '-' . date('m') . '-31';
			echo "success";
		} 
What am I doing wrong here?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: comparing two values doesn't seem to work

Post by Jonah Bron »

I would compare them a bit differently.

Code: Select all

if (date('Y') == $current_page_year && date('m') == $current_page_month) {
If that doesn't work, try this for debugging:

Code: Select all

echo (date('Y') == $current_page_year && date('m') == $current_page_month);
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: comparing two values doesn't seem to work

Post by someguyhere »

Ok, tried that...it didn't work, so I echoed the statement, which returned 1. I assume that means that the statement is true, so maybe I have something wrong with how my if/else statements are nested. Is there something wrong here?

Code: Select all

		if($current_page == "http://domain.com/calendar/"){
			$upcoming_events_start = date('Y-m-d');
			$upcoming_events_end = date('Y') . '-' . date('m') . '-31';
		}
		if(date('Y') == $current_page_year && date('m') == $current_page_month){
			$upcoming_events_start = $current_page_year . '-' . $current_page_month . '-' . date('d');
			$upcoming_events_end = date('Y') . '-' . date('m') . '-31';
		}
		else{
		$current_page_year = substr( $current_page, strrpos( $current_page, '&yr=' )+4 );
		$upcoming_events_start = $current_page_year . '-' . $current_page_month . '-01';
		$upcoming_events_end = $current_page_year . '-' . $current_page_month . '-31';
		}
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: comparing two values doesn't seem to work

Post by Jonah Bron »

I don't see anything wrong, but then again, I don't know what it's supposed to do. Try echoing something random inside of the problematic if statement, just to make sure.
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: comparing two values doesn't seem to work

Post by someguyhere »

The variables in the 2nd if statement were being overwritten by the else statement for some reason. I solved it by just declaring the variables to begin with, followed by the if statements, which will change the variables if needed.

I don't know if this is the *correct* solution, but it works 8O

As such:

Code: Select all

		$current_page_year = substr( $current_page, strrpos( $current_page, '&yr=' )+4 );
		$upcoming_events_start = $current_page_year . '-' . $current_page_month . '-01';
		$upcoming_events_end = $current_page_year . '-' . $current_page_month . '-31';

		if($current_page == "http://domain.com/calendar/"){
			$upcoming_events_start = date('Y-m-d');
			$upcoming_events_end = date('Y') . '-' . date('m') . '-31';
		}
		if(date('Y') == $current_page_year && date('m') == $current_page_month){
			$upcoming_events_start = $current_page_year . '-' . $current_page_month . '-' . date('d');
			$upcoming_events_end = date('Y') . '-' . date('m') . '-31';
		}

Post Reply