Exemple #1
0
 function arrayToDate($date_input, $timestamp = false)
 {
     // possible year values
     if (isset($date_input['Y'])) {
         $year = $date_input['Y'];
     } else {
         if (isset($date_input['y'])) {
             $year = $date_input['y'];
         }
     }
     // possible month values
     if (isset($date_input['F'])) {
         $month = $date_input['F'];
     } else {
         if (isset($date_input['m'])) {
             $month = $date_input['m'];
         } else {
             if (isset($date_input['M'])) {
                 $month = $date_input['M'];
             } else {
                 if (isset($date_input['n'])) {
                     $month = $date_input['n'];
                 }
             }
         }
     }
     // possible day values
     if (isset($date_input['d'])) {
         $day = $date_input['d'];
     } else {
         if (isset($date_input['j'])) {
             $day = $date_input['j'];
         }
     }
     // possible hour values
     if (isset($date_input['g'])) {
         $hour = $date_input['g'];
     } else {
         if (isset($date_input['h'])) {
             $hour = $date_input['h'];
         } else {
             if (isset($date_input['G'])) {
                 $hour = $date_input['G'];
             } else {
                 if (isset($date_input['H'])) {
                     $hour = $date_input['H'];
                 }
             }
         }
     }
     // possible am/pm values
     if (isset($date_input['a'])) {
         $ampm = $date_input['a'];
     } else {
         if (isset($date_input['A'])) {
             $ampm = $date_input['A'];
         }
     }
     // instantiate date object
     $datestr = '';
     if (isset($year) || isset($month) || isset($day)) {
         if (isset($year) && !empty($year)) {
             if (strlen($year) < 2) {
                 $year = '0' . $year;
             }
             if (strlen($year) < 4) {
                 $year = substr($year, 0, 2) . $year;
             }
         } else {
             $year = '0000';
         }
         if ($year != '0000' && isset($month) && !empty($month)) {
             if (strlen($month) < 2) {
                 $month = '0' . $month;
             }
         } else {
             $month = '00';
         }
         if ($year != '0000' && $month != '00' && isset($day) && !empty($day)) {
             if (strlen($day) < 2) {
                 $day = '0' . $day;
             }
         } else {
             $day = '00';
         }
         $datestr .= "{$year}-{$month}-{$day}";
     }
     if (isset($hour) || isset($date_input['i']) || isset($date_input['s'])) {
         // set the hour
         if (isset($hour) && !empty($hour)) {
             if (strlen($hour) < 2) {
                 $hour = '0' . $hour;
             }
         } else {
             $hour = '00';
         }
         if (isset($ampm)) {
             if (strtolower($ampm) == 'pm' && strlen($hour) == 1) {
                 $hour += 12;
             }
         }
         // set the minutes
         if (isset($date_input['i']) && !empty($date_input['i'])) {
             if (strlen($date_input['i']) < 2) {
                 $date_input['i'] = '0' . $date_input['i'];
             }
         } else {
             $date_input['i'] = '00';
         }
         $datestr .= ($datestr != '' ? ' ' : '') . "{$hour}:{$date_input['i']}";
         // set the seconds
         if (isset($date_input['s']) && !empty($date_input['s'])) {
             $datestr .= ':' . (strlen($date_input['s']) < 2 ? '0' : '') . $date_input['s'];
         } else {
             $datestr .= ':00';
         }
     }
     // feed it into the date object
     $dateobj = new Date($datestr);
     // set the time zone
     $dateobj->setTZ(NDate::getClientTZ());
     // pull the string back out
     $datestr = $dateobj->getDate();
     unset($dateobj);
     return $datestr;
 }