0

I want to convert a date field(format: dd-mm-yyyy) to db date format. If 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;
    }
} 
  • Can you please show us your code? What is a sample input value? Where do you see a returned value? – Ben Feb 08 '16 at 12:08
  • function convertToDbDate($date) { if($date !="") { $result= date('Y-m-d', strtotime(str_replace('/', '-', $date))); if($result) { return $result; } return false; } } –  Feb 08 '16 at 12:10
  • 4
    Possible duplicate of [Convert one date format into another in PHP](http://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) – Chetan Ameta Feb 08 '16 at 12:12
  • Please try this $result= date('Y-m-d', strtotime($date)); – Vipin Sharma Feb 08 '16 at 12:12

1 Answers1

0
$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

Robbe
  • 192
  • 2
  • 13