How to convert a date format in PHP

Today, we’ll discuss how to convert a date format in PHP. For example if we have date in YYYY-MM-DD format and need to convert it to DD-MM-YYYY format.

Using built-in PHP functions strtotime() and date(), we can easily convert date format.

  • strtotime() function – To convert any textual datetime to a Unix timestamp.
  • date() function – To convert timestamp into desired date format.

You can also check Add or Subtract days, month and year to a date using PHP.

Let’s start with an example.

Convert a date format

  1. Convert YYYY-MM-DD to DD-MM-YYYY
  2. Convert YYYY-MM-DD to MM-DD-YYYY
  3. Convert DD/MM/YYYY to YYYY-MM-DD

1. Convert YYYY-MM-DD to DD-MM-YYYY

In the following example, we will change the date from yyyy-mm-dd format to dd-mm-yyyy.

2. Convert YYYY-MM-DD to MM-DD-YYYY

In this second example, we will change the date from yyyy-mm-dd format to mm-dd-yyyy.

3. Convert DD/MM/YYYY to YYYY-MM-DD

In the last example, we will change the date from dd/mm/yyyy format to yyyy-mm-dd.

That’s it for today.
Thank you for reading. Happy Coding..!! 🙂

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *