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;
    }
} 

Answers


Rob February 2016

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

http://php.net/manual/en/datetime.createfromformat.php


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

Search




Leave an answer