Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»PHP»How do I Convert Date Format in PHP

    How do I Convert Date Format in PHP

    RahulBy RahulApril 20, 20181 Min ReadUpdated:March 27, 2019

    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.

    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
    Previous ArticleHow to Add Apt Repository In Ubuntu & Debian
    Next Article How To Install Apache, MySQL, PHP on Ubuntu 18.04 LTS

    Related Posts

    How to Install Composer on Ubuntu 22.04

    Updated:June 24, 20223 Mins Read

    How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04

    Updated:June 19, 20223 Mins Read

    How To Install Linux, Nginx, MySQL, & PHP (LEMP Stack) on Ubuntu 22.04

    Updated:April 7, 20227 Mins Read

    How to Install Apache, MySQL, PHP (LAMP Stack) on Ubuntu 22.04

    Updated:June 28, 20225 Mins Read

    How To Setup Apache, PHP & MongoDB in Ubuntu & Debian

    Updated:October 8, 20213 Mins Read

    How To Install and Use PHP Composer on Debian 11

    Updated:February 16, 20224 Mins Read

    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

    Recent Posts
    • Filesystem Hierarchy Structure (FHS) in Linux
    • How to accept user input in Python
    • What is difference between var, let and const in JavaScript?
    • What is CPU? – Definition, Types and Parts
    • What is the /etc/aliases file
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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