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

Created 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

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.