How to match date with a group of dates?

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
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

How to match date with a group of dates?

Post by eshban »

Hello,

I have a query regarding comparing date. I have some date groups like

01/04/2010 - 15/04/2010
16/04/2010 - 30/04/2010
05/05/2010 - 23/05/2010

Suppose user select the below date from calender control on my website:
11/04/2010

How can I check that the 11/04/2010 (user selected date) exists in which date group (mentioned above)?

ps: I have checked between and <> but not benefit.

Kindly help in this regard.

Eshban
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: How to match date with a group of dates?

Post by cpetercarter »

Something like this:

Code: Select all

$startDates = array('04/01/2010', '04/16/2010', '05/05/2010'); //NB We have to use the American notation 'month/day/year'
$endDates = array('04/15/2010', '04/30/2010', '05/23/2010');

foreach ($startDates as $key=>$date)  {
$startDateUnix[$key] = strtotime($date);
}

foreach ($endDate as $key=>$date)  {
$endDateUnix[$key] = strtotime($date);
}

$targetDateUnix = strtotime('11/04/2010');

foreach ($startDateUnix as $key=>$start)  {
if ($targetDateUnix >= $start && $targetDateUnix =< $endDateUnix[$key]) {
echo "Date is in group " . $key;
}
}
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

Re: How to match date with a group of dates?

Post by eshban »

many thanks for that. This is really very helpful
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How to match date with a group of dates?

Post by AbraCadaver »

Too many loops, plus you had a parse error:

Code: Select all

$startDates = array('04/01/2010', '04/16/2010', '05/05/2010'); //NB We have to use the American notation 'month/day/year'
$endDates = array('04/15/2010', '04/30/2010', '05/23/2010');

$targetDateUnix = strtotime('04/11/2010');

foreach ($startDates as $key => $start)  {
	if ($targetDateUnix >= strtotime($start) && $targetDateUnix <= strtotime($endDates[$key])) {
		echo "Date is in group " . $key;
	}
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
eskio
Forum Commoner
Posts: 66
Joined: Tue Apr 01, 2008 1:00 am

Re: How to match date with a group of dates?

Post by eskio »

Hi,
you can also convert your dates to "numbers" using mktime(). It is easier to compare numbers than dates (us format "mm/dd/yyyy", european format "dd/mm/yyyy" ???? :crazy: )
Post Reply