Example #1
0
 /**
  * Format a given date to an SQL formatted date.
  *
  * @param $date string The date to format
  * @param $locale string The locale in which the date is formatted. If no locale is given, the current locale is used
  * @param $with_time boolean Indicates wether the time part must be added if present
  * @return string
  */
 public static function date_to_sql($date, $locale = null, $with_time = true)
 {
     $date = trim($date);
     if ($with_time && strpos($date, ' ') !== false) {
         return DateTool::datetime_to_sql($date, $locale);
     } else {
         $format = DateTool::get_date_format($locale);
     }
     $format = str_replace('Y', '%Y', $format);
     $format = str_replace('m', '%m', $format);
     $format = str_replace('d', '%d', $format);
     $date_array = strptime($date, $format);
     if ($date_array !== false) {
         $day = sprintf("%02d", $date_array['tm_mday']);
         $month = sprintf("%02d", $date_array['tm_mon'] + 1);
         $year = DateTool::get_complete_year(1900 + $date_array['tm_year']);
         return $year . '-' . $month . '-' . $day;
     } else {
         return null;
     }
 }