All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Date Format yyyy-mm-dd

Last Updated : Mar 11, 2024

PHP Date Format yyyy-mm-dd

In this article we will show you the solution of PHP date format yyyy-mm-dd, since having php you can modify date to any format and here we are going to change any date to ‘yyyy-mm-dd’ format.

When it comes to date related topic everyone remember to use date() function.

We are achieving this with the help of date() and strtotime() methods.

You have to give input date as per your wish to convert that string to datetime format we using strtotime().

The date() helps to convert date to specific format which we defined inside this method with input.

Step By Step Guide On PHP Date Format yyyy-mm-dd :-

As foremost step, you have to define your input as your wish within double quotes.

Next you need to define date() method with two mandatory parameters.

First one is format of your requirement and second one is strtotime() method with given date input string.

This date() function return you converted format date as result to left hand side provide variable.

At last step, through echo method or property we printing date input and converted date result.

<?php
//date format yyyy-mm-dd
$dt="22-12-1969";
$ndt=date("Y-m-d",strtotime($dt));
echo "Input date: ". $dt;
echo "<br>New Format Date: ". $ndt;
?>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon. In php we need to specify ‘$’ symbol before the variable name also default rule.
  3. We created variable ‘$dt’ with date string value ’22-12-1969’. If you want changes you can modify this as your wish with this same format.
  4. As upcoming step, we defining format ‘Y-m-d’ that is helps to get date with format ‘yyyy-mm-dd’ as first parameter to date(). Second parameter is ‘strtotime()’ with input ‘$dt’ to convert string to date type and passed it to date() method.
  5. Now date() will return you successfully changed format that’s stored on variable ‘ndt’.
  6. To show the difference of before and after date format changes both are printed at client side as result using echo statement. It helps user to understand this program work procedure and try on your own with some other values or definition.
  7. Instead echo() you can prefer print() method if you want and that not going to affects your result.

Conclusion :-

In conclusion, hope now you know how to format date using php.

When you work with php we need to create and process php files at server location and then we need to start the server before execute the program.

Otherwise sometimes you will get error so ensure it if you get any error while running output.

When we execute this program on browser it will print ‘Input date: 22-12-1969 New Format Date: 1969-12-22’ as output.

With this help of sample code you can test with any other values and you will get result according to your changes.

I hope this article on PHP date format yyyy-mm-dd helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪