Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : MCAL Functions : mcal_date_compare

mcal_date_compare

Compares two dates (PHP 4)
int mcal_date_compare ( int a_year, int a_month, int a_day, int b_year, int b_month, int b_day )


Code Examples / Notes » mcal_date_compare

asharm4

//this function compares any date to current date and returns results in form of integers..
function date_compare ( $b_year, $b_month, $b_day )
{
$a_year = $today["year"];
$a_month = $today["mon"];
$a_day = $today["mday"];
$final_result = 9; //set it to random  right now
if($a_year >$b_year)
 {
  $final_result = 1; //the year 1 is greater then year 2 so no more check required
 }//year greater
else if($a_year == $b_year) //the years are the same ..so check the month
 {
  if($a_month > $b_month)
   {
    $final_result = 2; //the months are greater and so no more checks needed
   }//greater month check
   else
   if($a_month == $b_month)//same months ..so check dates
   {
        if($a_day > $b_day)
        {
        $final_result = 3; //the days solve the problem :)
         }
         else if($a_day == $b_day)
         {
         $final_result = 4; //the dates are identical too..so date1 becomes equal to date 2
         }
         else
         {
          $final_result = -3; //the date1 < $date 2
         }
   }//equal month check
   else
   {
    $final_result = -2;
   }//month1  < month2
 }//year same
else
   {
  $final_result = -1; //the year 1 is smaller then year 2 ..hence date1 < date2
   }
return $final_result;
}


nepnep_

/* this is much a much better code */
/* a very short one and easy to understand */
/* reurns -1 if first given date is lower than the second */
/* reurns 0 if given dates are equal */
/* reurns 1 if second given date is lower than the first */
function date_compare($y1,$m1,$d1,$y2,$m2,$d2){
/* check the year */
if($y1 > $y2){
/* if $y1 is greater then therefore the first date is greater than second */
return 1;
}elseif($y1 == $y2){
/* equal year, check the month */
if($m1 > $m2){
/* year is equal but the first given month is greater
/* therefore the first given date is greater */
return 1;
}elseif($m1 == $m2){
/* if month are also equal, check the date */
if($d1 > $d2){
return 1;
}elseif($d1 == $d2){
return 0;
}else{
return -1;
}
}else{
/* if year is equal but month is less, therefore the first given date is less */
return -1;
}
}else{
/* if $y1 is less than $y2 */
return -1;
}
}


Change Language


Follow Navioo On Twitter
mcal_append_event
mcal_close
mcal_create_calendar
mcal_date_compare
mcal_date_valid
mcal_day_of_week
mcal_day_of_year
mcal_days_in_month
mcal_delete_calendar
mcal_delete_event
mcal_event_add_attribute
mcal_event_init
mcal_event_set_alarm
mcal_event_set_category
mcal_event_set_class
mcal_event_set_description
mcal_event_set_end
mcal_event_set_recur_daily
mcal_event_set_recur_monthly_mday
mcal_event_set_recur_monthly_wday
mcal_event_set_recur_none
mcal_event_set_recur_weekly
mcal_event_set_recur_yearly
mcal_event_set_start
mcal_event_set_title
mcal_expunge
mcal_fetch_current_stream_event
mcal_fetch_event
mcal_is_leap_year
mcal_list_alarms
mcal_list_events
mcal_next_recurrence
mcal_open
mcal_popen
mcal_rename_calendar
mcal_reopen
mcal_snooze
mcal_store_event
mcal_time_valid
mcal_week_of_year
eXTReMe Tracker