Page 1 of 1

[SOLVED]Another date comparison question

Posted: Tue Oct 31, 2006 8:22 pm
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])))

Posted: Tue Oct 31, 2006 8:39 pm
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.

Posted: Tue Oct 31, 2006 8:54 pm
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])

Posted: Tue Oct 31, 2006 9:04 pm
by feyd
You're adding the components together, not concatenating them.

Posted: Tue Oct 31, 2006 9:08 pm
by angelena

Posted: Tue Oct 31, 2006 9:27 pm
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.

Re: Another date comparison question

Posted: Wed Nov 01, 2006 3:01 am
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'

Posted: Wed Nov 01, 2006 4:54 am
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;		
		}