Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»PHP»How do I Convert Date Format in PHP

    How do I Convert Date Format in PHP

    By RahulMarch 27, 20191 Min Read

    This tutorial uses PHP strtotime() and date() functions to convert date time format. For example you have stored a date YYYY-MM-DD format in a variable and need to change this to MM-DD-YYYY format.

    Advertisement

    We can achive this by converting date first to seconds using strtotime() function. After that reconstruct date to any format using date() function. Below is few examples of conversion:

    1. Change YYYY-MM-DD => MM-DD-YYYY

    Here we have date yyyy-mm-dd (“2019-01-15”) format and converting it to mm-dd-yyyy (“01-15-2019”) format.

    1
    2
    3
    4
    $origDate = "2019-01-15";
     
    $newDate = date("m-d-Y", strtotime($origDate));
    echo $newDate;

    Output:

    01-15-2019
    

    2. Change YYYY-MM-DD => DD-MM-YYYY

    Here we have date yyyy-mm-dd (“2019-01-15”) format and converting it to dd-mm-yyyy (“15-01-2019”) format.

    1
    2
    3
    4
    $origDate = "2019-01-15";
     
    $newDate = date("d-m-Y", strtotime($origDate));
    echo $newDate;

    Output:

    15-01-2019
    

    3. Change DD/MM/YYYY => YYYY-MM-DD

    If you have slashes in date format like “15/01/2019” and need to convert / with hyphens (-). The following example will help you to convert DD/MM/YYYY (“15/01/2019”) to YYYY-MM-DD (2019-01-15).

    1
    2
    3
    4
    5
    $origDate = "15/01/2019";
     
    $date = str_replace('/', '-', $origDate );
    $newDate = date("Y-m-d", strtotime($date));
    echo $newDate;

    Output:

    2019-01-15
    

    date PHP strtotime
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    PHP Arrays: A Beginner’s Guide

    Running Laravel Queue Worker as a Systemd Service

    How to Change PHP Session Timeout

    View 10 Comments

    10 Comments

    1. Krunal on October 15, 2019 2:54 pm

      Thanxx

      Reply
    2. Freddy on August 25, 2019 5:33 am

      Hello Paul, this is a great tutorial but I have question regarding my current problem in MySQL and PHP in finding the difference between 2 dates(variables: start date and end date). MySQL date format is YYYY-mm-dd while in bootstrap is mm/dd/YYYY. I want to calculate the difference between two dates from MySQL and display the output from PHP. I hope you could help me on this Paul ?

      Reply
    3. Lavish Rajora on July 30, 2019 6:55 am

      what do i do if i want to convert any format into a specified format
      whether it is in string format i.e february or feb
      whether it is 2 or 02
      whether it is short form of year i.e 1973 or 73

      or format listed in
      https://docs.oracle.com/cd/E41183_01/DR/Date_Format_Types.html

      Reply
    4. paul on July 21, 2019 10:56 am

      Great article however what do you do if the original date is in the format of a posted string ie let’s say “15/01/2019” .

      $origDate = $_POST[‘Date1’];

      $date = str_replace(‘/’, ‘-‘, $origDate );
      $newDate = date(“Y-m-d”, strtotime($date));
      echo $newDate;

      Reply
    5. Pedro Henrique on July 2, 2019 3:00 am

      Good article this post helped me, thanks very good.

      Reply
    6. mukesh on June 27, 2019 9:20 am

      good but i want to convert
      29-Apr-2019 to Apr 29, 2019

      how to do that?

      Reply
      • help on July 11, 2019 8:40 am

        date(“d F, Y”, strtotime($date));?>

        Reply
        • Baha on September 26, 2019 6:33 am

          Excellent!!! I have found that I looked for. But, how can I do the name of months in other language?

          Reply
    7. Marathon Keto Review on April 29, 2019 7:47 pm

      I like this website very much, Its a real nice situation to
      read and receive info.

      Reply
    8. Fardin on December 19, 2018 4:15 pm

      Thank you …

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to List Manually Installed Packages in Ubuntu & Debian
    • 10 Bash Tricks Every Developer Should Know
    • How to Validate Email Address in JavaScript
    • Firewalld: Common Firewall Rules and Commands
    • 12 Apk Commands in Alpine Linux Package Management
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.