Change Date from dd/mm/yyyy to yyyy-dd-mm

Conversion

$date_array = explode("/",$date); // split the array
$var_day = $date_array[0]; //day seqment
$var_month = $date_array[1]; //month segment
$var_year = $date_array[2]; //year segment
$new_date_format = "$var_year-$var_day-$var_month"; // join them together

Possibly a more MySQL friendly format in some circumstances.

Change period-separated to slash-separated or vice versa (and reverse order)

Convert date from YYYY/MM/DD to DD.MM.YYYY (and from DD.MM.YYYY to YYYY/MM/DD)

/**
 * @param string $date (d.m.y, y-m-d, y/m/d)
 * @return string|bol
 */

function convertDate($date) {
       // EN-Date to GE-Date
       if (strstr($date, "-") || strstr($date, "/"))   {
               $date = preg_split("/[\/]|[-]+/", $date);
               $date = $date[2].".".$date[1].".".$date[0];
               return $date;
       }
       // GE-Date to EN-Date
       else if (strstr($date, ".")) {
               $date = preg_split("[.]", $date);
               $date = $date[2]."-".$date[1]."-".$date[0];
               return $date;
       }
       return false;
}

Comments

  1. User Avatar
    kukat
    Permalink to comment#

    date(‘Y-m-d’, strtotime(’23/10/2009′));

  2. User Avatar
    Fabricio Anzorena
    Permalink to comment#

    implode(‘-‘, array_reverse(explode(‘/’,$date)));

    Won’t have the 1970 limit problem of the timestamp.

  3. User Avatar
    Emma Davis
    Permalink to comment#

    Your last line of code in preparing the date for databse is incorrect as it puts the day before the month:

    $new_date_format = “$var_year-$var_day-$var_month”;

    Should be:

    $new_date_format = “$var_year-$var_month-$var_day”;

  4. User Avatar
    Prashant Palikhe
    Permalink to comment#

    Fabricio’s answer is much more elegant and works just fine:)

  5. User Avatar
    nile
    Permalink to comment#

    kukat’s is the best though.

  6. User Avatar
    Kira
    Permalink to comment#

    Thank you. Simply effective :)

  7. User Avatar
    Jitendra Joshi
    Permalink to comment#

    DateTime::createFromFormat(‘d/m/Y’, “21/1/2013”)->format(“Y-m-d”)

  8. User Avatar
    Olivier C
    Permalink to comment#

    /**
    * @name Date Change Format
    * @note PHP > 5.3
    * @params date, from date format, to date format
    * @use date_change_format()
    */
    function date_change_format($setDate, $from=’Y-m-d’, $to=’d/m/Y’) {
    if($setDate !=”) {
    $date = DateTime::createFromFormat($from, $setDate);
    return $date->format($to);
    } else {
    return ”;
    }
    }

    • User Avatar
      amlette
      Permalink to comment#
      /**
      * @name Date Change Format
      * @note PHP > 5.3
      * @params date, from date format, to date format
      * @use date_change_format()
      */
      function date_change_format($setDate, $from='Y-m-d', $to='d/m/Y') {
          if ($setDate != '') {
              $date = DateTime::createFromFormat($from, $setDate);
              return $date->format($to);
          } else {
              return '';
          }
      }
      

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

Submit a Comment

icon-closeicon-emailicon-linkicon-menuicon-searchicon-tag