1

We need from the html form send a date in the "Croatian" format 29. 01. 2015 (pay attention to the space after the dot), but before we save the date in the database it is necessary to convert the string in mysql date format 2015-01-29.

We are using CakePHP 2.6 framework, so you can combine CakeTime helper and plain PHP.

Currently we use this function:

// $pickerDate = '29. 01. 2015';
public function pickerDateToMysql($pickerDate){
    $datetime = explode(' ', str_replace('.','',$pickerDate));
    $date = implode('-',array_reverse($datetime));
    debug($date);
    return $date;  // 2015-01-29
}

Is there a more elegant solution?

Salines
  • 5,674
  • 3
  • 25
  • 50

1 Answers1

1

You could use DateTime::createFromFormat() method to rearrange date format

public function pickerDateToMysql($pickerDate){
    $date = DateTime::createFromFormat('d. m. Y', $pickerDate);
    return $date->format('Y-m-d');
}  
$CroatianDate = '29. 01. 2015';
pickerDateToMysql($CroatianDate); 
Girish
  • 11,907
  • 3
  • 34
  • 51