Function to check next cron date

nrep

Well-known member
I'm coding something (not in the XF core) where I need to know the next available date that meets some cron-like criteria. For example, I will have a day (0-31), month (0-12) and year (0+) - where 0 acts as a wildcard.

I need to find the next date that meets those conditions. For example, if I had day=10, month=6, year=0 - it would give me the value 10-06-2014 (as of today).

I found a function that does some of this, but only using the day (ignoring month/year), however I can't figure out how to extend it. Has anyone does this before, or can see how I can modify it?

PHP:
function OLDnextDate($userDay){    
 
  $today = date('d'); // today
 
  $target = date('Y-m-'.$userDay);  // target day
 
  if($today <= $userDay){
 
  $return = strtotime($target);
 
  }
  else{
 
  $thisMonth = date('m') + 1;
  $thisYear = date('Y');
 
  if($userDay >= 28 && $thisMonth == 2){
      $userDay = 28;
  }
 
 
  while(!checkdate($thisMonth,$userDay,$thisYear)){
 
    $thisMonth++;
 
    if($thisMonth == 13){
 
      $thisMonth = 1;
      $thisYear++;
 
    }
 
  }    
 
  $return = strtotime($thisYear.'-'.$thisMonth.'-'.$userDay);
 
  }
 
  return $return;
 
}
 
Last edited:
Top Bottom