Home Ask Login Register

Developers Planet

Your answer is one click away!

Sruthy February 2016

Convert date mm-dd-yyyy to yyyy-mm-dd in php

I want to convert a date field(format: dd-mm-yyyy) to db date format. Check input date format is equal to dd-mm-yyyy format then convert date to yyyy-mm-dd format else return false.

function convertToDbDate($date) {
    if($date !="") {
        $result= date('Y-m-d', strtotime(str_replace('/', '-', $date))); 
        if($result) { 
            return $result; 
        return false;


Rob February 2016

$date = date_create_from_format('m-d-Y', '12-31-2016');
echo $date->format('Y-m-d');


fait Lover February 2016

You can use DateTime::createFromFormat

$result_date = DateTime::createFromFormat('d/m/Y', "23/02/2016");

echo $date->format('Y-m-d');

Post Status

Asked in February 2016
Viewed 2,691 times
Voted 5
Answered 2 times


Leave an answer

Quote of the day: live life