Im trying to convert a date format of mm-dd-yyyy to yyyy-mm-dd. The latter is how it will be entered into the database.

Heres the code:

$date = $_POST['sched_date'];
$date = date('Y-m-d', strtotime($date));

For example, if date is 09-29-2007, it will format it to 09-29-2019.

Any ideas?

    There are several ways. Here is one:

    $date = explode('-', $date);
    if (count($date) == 3) {
        $date = $date[2] . '-' . $date[0] . '-' . $date[1];
    } else {
        // format error
    }

      Due to the specifics of the sorts of string conversions strtotime() will do, this will work for you, too:

      $date = $_POST['sched_date'];
      $date = date('Y-m-d', strtotime(str_replace('-', '/', $date)));
      
        Write a Reply...