Mittels Zend_Date Monatsbereich berechnenDetermine month range using Zend_Date

The following function can be used to determine a range of days for a particular month, optionally based on a start and end date. This function comes handy for cron jobs, etc.

Please refer to https://www.kerstner.at/en/2011/10/zend_date-and-mysqls-datetime-format/ for an explanation of the constants MYSQL_DATETIME and MYSQL_DATE.

/**
 * Determines month-range specified, based on begin and end date specified.
 * @param Zend_Date|string? $beginDate if not specified will automatically 
 *                          calculate first day of *current* month, unless
 *                          $endDate is specified, in which case $beginDate
 *                          will default to the first day of the month 
 *                          specified by $endDate.
 *                          If specified as string must conform to 
 *                          MYSQL_DATETIME||MYSQL_DATE.
 * @param Zend_Date|string? $endDate if not specified will automatically 
 *                          calculate last day of month specified by 
 *                          $beginDate. If $beginDate is not specified will 
 *                          calculate the range from the first to last day 
 *                          of *current* month.
 *                          If specified as string must conform to 
 *                          MYSQL_DATETIME||MYSQL_DATE.
 * @return array
 */
 function getMonthRange($beginDate=null, $endDate=null) {
   $_beginDate = null;
   $_endDate = null;

   if ($beginDate) { //begin date specified
     if ($beginDate instanceof Zend_Date) {
       $_beginDate = $beginDate;
     } else {
       $_beginDate = new Zend_Date();
       if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $beginDate)) {
         $_beginDate->set($beginDate, MYSQL_DATE);
       } else if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $beginDate)) {
         $_beginDate->set($beginDate, MYSQL_DATETIME);
       } else {
         throw new Exception('invalid date syntax specified');
       }
     }

     if ($endDate) { //end date specified
       if ($endDate instanceof Zend_Date) {
         $_endDate = $endDate;
       } else {
         $_endDate = new Zend_Date();
         if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $endDate)) {
           $_endDate->set($endDate, MYSQL_DATE);
         } else if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $endDate)) {
           $_endDate->set($endDate, MYSQL_DATETIME);
         } else {
           throw new Exception('invalid date syntax specified');
         }
       }

       if (!$_endDate->compare($_beginDate)) {
         throw new Exception('end date is newer than begin date');
       }
     } else { // no end date specified, take last day of month from begin date
       $_beginDateData = $_beginDate->toArray();
       $_beginDateData['day'] = $_beginDate->get(Zend_Date::MONTH_DAYS); //last day of month specified by begin date
       $_endDate = new Zend_Date();
       $_endDate->set($_beginDateData);
     }
   } else { //no begin date specified (take first day of month)
     if ($endDate) { //take first day of month specified by end date
       if ($endDate instanceof Zend_Date) {
          $_endDate = $endDate;
       } else {
         $_endDate = new Zend_Date();
         if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $endDate)) {
           $_endDate->set($endDate, MYSQL_DATE);
         } else if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $endDate)) {
           $_endDate->set($endDate, MYSQL_DATETIME);
         } else {
           throw new Exception('invalid date syntax specified');
       }
     }

    $_endDateData = $_endDate->toArray();
    $_endDateData['day'] = 1; //first day of $endDate month
    $_beginDate = new Zend_Date();
    $_beginDate->set($_endDateData);
  } else { //no end date specified, take first day of *current* month
    $today = Zend_Date::now();
    $todayData = $today->toArray();
    $todayData['day'] = 1; //first day of month
    $_beginDate = new Zend_Date();
    $_beginDate->set($todayData);
    $todayData['day'] = $today->get(Zend_Date::MONTH_DAYS); //last day of month
    $_endDate = new Zend_Date();
    $_endDate->set($todayData);
   }
  }

  return array($_beginDate, $_endDate);
}

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.