Convert dd/mm/yyyy to yyyy-mm-dd in codeigniter

Updated at 11-May-2021, By samar

Convert dd/mm/yyyy to yyyy-mm-dd in codeigniter

In this session, we will try our hand at solving the "Convert dd/mm/yyyy to yyyy-mm-dd in codeigniter".

To convert the date-time format PHP provides strtotime() and date() functions. We can convert date format from dd/mm/yyyy to yyyy-mm-dd in codeigniter using str_replace() method before date() and strtotime() methods.
  • Change date format from dd/mm/yyyy to yyyy-mm-dd in PHP

    <?php
        $orgDate = "11/05/2021";  // (DD/MM/YYYY)
        $date = str_replace('/', '-', $orgDate);  
        $newDate = date("Y-m-d", strtotime($date));  
        echo "New date format is: ".$newDate. " (YYYY-MM-DD)";  
    ?>
    

    Suppose we have a date 11/05/2021 in DD/MM/YYYY format separated by dash (/) sign. We want to convert this to 2021-05-11 (YYYY-MM-DD) format, which will be separated by the slash (-). We can change the date format using strtotime() method after str_replace() method on a given date which is used to replace (/) with (-).

Back to code snippet queries related codeIgniter