[SOLVED]Another date comparison question

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
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

[SOLVED]Another date comparison question

Post by mcccy005 »

I'm trying to work out how to determine whether a particular date is between two other dates.
I have done an extensive search of these forums and the Internet but none of the solutions seem to work with my code!
The values in $days_booked are accessed from a date value in MYSQL.

Heres the basic code: (Assume that the current day is 17 November 2006; a day in between the two mentioned dates)

Code: Select all

$days_booked[0]=2006-11-15;
$days_booked[1]=2006-11-20;

$current_year=date("o");
$current_month=date("n");
$current_day_tracker=17;
$days_booked_tracker=1;

if(strtotime($current_year."-".$current_month."-".$current_day_tracker) >= strtotime($days_booked[$days_booked_tracker-1])
	&& strtotime($current_year."-".$current_month."-".$current_day_tracker) <= strtotime($days_booked[$days_booked_tracker]))
$is_booking_period=true;
else $is_booking_period=false
;

I've also tried this method aswell without success (whereby $temp_booked_from and $temp_booked_until is an array as a result of using explode( ) on $days_booked[0] and $days_booked[1])

Code: Select all

if(date("d-M-Y", mktime(0, 0, 0, $current_month, $current_day_tracker, $current_year)) >= date("d-M-Y", mktime(0, 0, 0, $temp_booked_from[1], $temp_booked_from[2], $temp_booked_from[0]))
&& date("d-M-Y", mktime(0, 0, 0, $current_month, $current_day_tracker, $current_year)) <= date("d-M-Y", mktime(0, 0, 0, $temp_booked_until[1], $temp_booked_until[2], $temp_booked_until[0])))
Last edited by mcccy005 on Wed Nov 01, 2006 4:54 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There's no need for strtotime() if the strings are in YYYYMMDD or YYYY-MM-DD type formats; PHP's own string comparison can determine if they are different.
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Post by mcccy005 »

Even when I simply convert them to an integer (eg. if current date is 2006-11-01 and I convert this to an integer like 20061101 the code still wont work....like as follows:

Code: Select all

if((int)($current_year+$current_month+$current_day_tracker)>=(int)$days_booked[$days_booked_tracker-1]
&&(int)($current_year+$current_month+$current_day_tracker)<=(int)$days_booked[$days_booked_tracker])
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You're adding the components together, not concatenating them.
angelena
Forum Commoner
Posts: 53
Joined: Mon Nov 22, 2004 4:10 am

Post by angelena »

mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Post by mcccy005 »

feyd wrote:You're adding the components together, not concatenating them.
Of course!!! How stupid of me!!! Thanks.

ANd thanks for those mcal functions....never heard of their existance or come across it at all in my searching for this stuff.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: Another date comparison question

Post by volka »

Code: Select all

$days_booked = array();
$days_booked[0]=2006-11-15;
$days_booked[1]=2006-11-20;

echo $days_booked[0], "\n", $days_booked[1];
1980
1975
You want the dates as string literals not as numbers.
'2006-11-15' and '2006-11-20'
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Post by mcccy005 »

After looking at that mcal_date_compare functions and finding out I would have to install it and then finding out the link to the file was dead; I ended up writing my own function which does the exact same thing as the mcal_date_compare and returns the same values (-1 if the first date is less than the second date; 0 if the dates are equal; and 1 if the first date is later than the second date)
Here it is:

Code: Select all

/*Compares two dates.
		* Returns 1 if the first date is AFTER the second date
		* Returns 0 if the two dates are equal
		* Returns -1 is the first date is PRIOR to the second date
		*/
		function date_compare($a_year, $a_month, $a_day, $b_year, $b_month, $b_day)
		{
			//Checks to see if there is any difference in years to each date
			if($a_year > $b_year)
				return 1;
			else if($a_year < $b_year)
				return -1;
				
			//Checks to see if there is any difference in months to each date
			if($a_month > $b_month)
				return 1;
			else if($a_month < $b_month)
				return -1;
				
			/*If the Year and month of each date are equal, then compare each day */
			if($a_day > $b_day)
				return 1;
			else if($a_day < $b_day)
				return -1;
				
			//Else if both dates are equal
			else return 0;		
		}
Post Reply