jDateTime::checkdate PHP Method

checkdate() public static method

Checks the validity of the date formed by the arguments. A date is considered valid if each parameter is properly defined. works like php's built in checkdate() function. Leap years are taken into consideration. e.g: $obj->checkdate(10, 21, 1390); // Return true $obj->checkdate(9, 31, 1390); // Return false You can force gregorian checkdate if system default is jalali and you need to check based on gregorian date $check = $obj->checkdate(12, 31, 2011, false);
Author: Omid Pilevar
public static checkdate ( $month, $day, $year, $jalali = null ) : boolean
$month int The month is between 1 and 12 inclusive.
$day int The day is within the allowed number of days for the given month.
$year int The year is between 1 and 32767 inclusive.
$jalali bool (Optional) pass false if you want to input gregorian time
return boolean
    public static function checkdate($month, $day, $year, $jalali = null)
    {
        //Defaults
        $month = intval($month) == 0 ? self::date('n') : intval($month);
        $day = intval($day) == 0 ? self::date('j') : intval($day);
        $year = intval($year) == 0 ? self::date('Y') : intval($year);
        //Check if its jalali date
        if ($jalali === true || $jalali === null && self::$jalali === true) {
            $epoch = self::mktime(0, 0, 0, $month, $day, $year);
            if (self::date('Y-n-j', $epoch, false) == "{$year}-{$month}-{$day}") {
                $ret = true;
            } else {
                $ret = false;
            }
        } else {
            $ret = checkdate($month, $day, $year);
        }
        //Return
        return $ret;
    }

Usage Example

Beispiel #1
0
 /**
  * @param $format
  * @param $date
  * @return array
  */
 public function parseFromFormat($format, $date)
 {
     // reverse engineer date formats
     $keys = array('Y' => array('year', '\\d{4}'), 'y' => array('year', '\\d{2}'), 'm' => array('month', '\\d{2}'), 'n' => array('month', '\\d{1,2}'), 'M' => array('month', '[A-Z][a-z]{3}'), 'F' => array('month', '[A-Z][a-z]{2,8}'), 'd' => array('day', '\\d{2}'), 'j' => array('day', '\\d{1,2}'), 'D' => array('day', '[A-Z][a-z]{2}'), 'l' => array('day', '[A-Z][a-z]{6,9}'), 'u' => array('hour', '\\d{1,6}'), 'h' => array('hour', '\\d{2}'), 'H' => array('hour', '\\d{2}'), 'g' => array('hour', '\\d{1,2}'), 'G' => array('hour', '\\d{1,2}'), 'i' => array('minute', '\\d{2}'), 's' => array('second', '\\d{2}'));
     // convert format string to regex
     $regex = '';
     $chars = str_split($format);
     foreach ($chars as $n => $char) {
         $lastChar = isset($chars[$n - 1]) ? $chars[$n - 1] : '';
         $skipCurrent = '\\' == $lastChar;
         if (!$skipCurrent && isset($keys[$char])) {
             $regex .= '(?P<' . $keys[$char][0] . '>' . $keys[$char][1] . ')';
         } else {
             if ('\\' == $char) {
                 $regex .= $char;
             } else {
                 $regex .= preg_quote($char);
             }
         }
     }
     $dt = array();
     $dt['error_count'] = 0;
     // now try to match it
     if (preg_match('#^' . $regex . '$#', $date, $dt)) {
         foreach ($dt as $k => $v) {
             if (is_int($k)) {
                 unset($dt[$k]);
             }
         }
         if (!jDateTime::checkdate($dt['month'], $dt['day'], $dt['year'], false)) {
             $dt['error_count'] = 1;
         }
     } else {
         $dt['error_count'] = 1;
     }
     $dt['errors'] = array();
     $dt['fraction'] = '';
     $dt['warning_count'] = 0;
     $dt['warnings'] = array();
     $dt['is_localtime'] = 0;
     $dt['zone_type'] = 0;
     $dt['zone'] = 0;
     $dt['is_dst'] = '';
     if (strlen($dt['year']) == 2) {
         $now = self::forge('now');
         $x = $now->format('Y') - $now->format('y');
         $dt['year'] += $x;
     }
     $dt['year'] = isset($dt['year']) ? (int) $dt['year'] : 0;
     $dt['month'] = isset($dt['month']) ? (int) $dt['month'] : 0;
     $dt['day'] = isset($dt['day']) ? (int) $dt['day'] : 0;
     $dt['hour'] = isset($dt['hour']) ? (int) $dt['hour'] : 0;
     $dt['minute'] = isset($dt['minute']) ? (int) $dt['minute'] : 0;
     $dt['second'] = isset($dt['second']) ? (int) $dt['second'] : 0;
     return $dt;
 }