All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Change Date Format In PHP

Last Updated : Mar 11, 2024

Change Date Format In PHP

In this tutorial we will show you the solution of change date format in PHP, as we know there are many formats with help we can change our date, which means the format of a date.

So, on behalf of this let us understand how to change the date format in PHP.

Step By Step Guide On Change Date Format In PHP :-

Now, below is an example given in PHP in which we see how to change the date format.

As there are many date formats so it is not possible to show all of them. But we try to show the most used format or the formats with help of which you can use or create more formats yourself.

<!DOCTYPE html>
   <title> change date format in PHP </title>
 Change date format in PHP
    $dateHere = "2012-05-27";
    $resultedDate = date("d-m-Y", strtotime($dateHere));
    echo "Our date is: ".$resultedDate. " (MM-DD-YYYY)";
    $resultedDate = date("m-d-Y", strtotime($dateHere));
    echo " Our date is: ".$resultedDate. " (MM-DD-YYYY)";
    $resultedDate = date("Y-m-d", strtotime($dateHere));
    echo " Our date is: ".$resultedDate. " (YYYY-MM-DD)";
  1. As, here we see that we that in above example we show you an example in which HTML and PHP codes are used.
  2. Here, first of all, we create a basic structure of HTML, in which we use <!DOCTYPE html> which defines the type of document. And next one is our HTML tags. These tags are paired tags and all the data regarding HTML is written inside these tags.
  3. After we use our head tag which is again paired tag and contains the title and meta information of the webpage. The data written inside the head is not shown on the webpage.
  4. Now, next is our title tag which defines the title of the webpage. The tag which has its closing tag is known as a paired tag. So, this is again a paired tag.
  5. Now, next is the body which is the main tag of HTML. The data which we have written inside the body is shown on the webpage. Mostly all tags which are helpful to show data or information on the screen are written under the body tag.
  6. Here, as above we see that we first store a custom date inside a variable. We can also apply this formula or code to the current date by using the date() function in PHP to get the current date.
  7. After that we just convert date to time using the strtotime() function and then choose a date format to print the date in the required format.

Conclusion :-

At last, in conclusion, here we can say that with the help of this article we can understand how to change the date format in PHP.

I hope this tutorial on change date format in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪