PHP MySQL Date Convert (php)

2016/06/16 23:18:02
Author Profile Picture

posted by

@florianpreusner

Convert german date field into "MySQL Date"


/**
 * Converts Date Field from dd.mm.yyyy to yyyy-mm-dd or yyyy-mm-dd hh:ii:ss
 *
 * @param  string  $date
 * @param  boolean $with_time
 * @return string  mysql-fitting date
 */
function generateMySQLDate($date, $with_time) {

  if($with_time) :

    $buffer = explode(' ', $date);
    $date   = explode('.', $buffer[0]);
    $time   = explode(':', $buffer[1]);
    return $buffer[2].'-'.$buffer[1].'-'.$buffer[0].' '.$time[0].':'.$time[1].':'.$time[2];
  else :

    $buffer = explode('.', $date);
    return $buffer[2].'-'.$buffer[1].'-'.$buffer[0];
  endif;
}
Share on Google+