Page 1 of 1
How to match date with a group of dates?
Posted: Tue Apr 06, 2010 12:56 am
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
Re: How to match date with a group of dates?
Posted: Tue Apr 06, 2010 1:45 am
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;
}
}
Re: How to match date with a group of dates?
Posted: Tue Apr 06, 2010 4:23 am
by eshban
many thanks for that. This is really very helpful
Re: How to match date with a group of dates?
Posted: Tue Apr 06, 2010 10:28 am
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;
}
}
Re: How to match date with a group of dates?
Posted: Wed Apr 07, 2010 1:57 am
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" ????

)