How To Format Date Time In PHP – Simple Examples

Welcome to a quick tutorial on how to format date and time in PHP. Need to change the format of a date-time in PHP?

There are 2 common ways to format date and time in PHP:

  1. Parse the date into a DateTime object and use the format() function.
    • $dt =  new DateTime("2011-02-03 04:05:06");
    • echo $dt->format("Y-m-d H:i:s");
    • echo $dt->format("d-M-Y, D");
  2. Parse the date into a Unix Timestamp and format it.
    • $dt = strtotime("2011-02-03 04:05:06");
    • echo date("Y-m-d H:i:s", $dt);
    • echo date("d-M-Y, D");

Both format() and date() follows the date-time format.

That should cover the basics, but if you need more examples – Read on!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

REAL QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Format Date Time Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

DATE TIME FORMATTING

All right, let us now get into the examples of formatting the date-time in PHP.

 

1) FORMATTING DATE-TIME OBJECT

1-date-time.php
<?php
// (A) DATE TIME OBJECT
$datetime = new DateTime("2019-08-07 06:05:04");
 
// (B) STANDARD DATE/TIME FORMATS
// (B1) ISO 8601 - USE ATOM, NOT DATETIMEINTERFACE::8601!
echo $datetime->format(DateTimeInterface::ATOM) . "<br>";
// (B2) RFC2822
echo $datetime->format(DateTimeInterface::RFC2822) . "<br>";
// (B3) COOKIE
echo $datetime->format(DateTimeInterface::COOKIE) . "<br>";
 
// (C) COMMON DATE TIME FORMAT
// (C1) MYSQL
echo $datetime->format("Y-m-d H:i:s") . "<br>";
// (C2) DAY DD/MM/YYYY HH:MM:SS AM/PM
echo $datetime->format("l d/m/Y h:i:s A") . "<br>";
// (C3) DD-MMM-YYYY, DAY
echo $datetime->format("d-M-Y, D") . "<br>";
// (C4) DD MONTH YYYY HHMM, DAY 
echo $datetime->format("j F Y Hi, l") . "<br>";
// (C5) WEEK-YYYY
echo $datetime->format("W-Y") . "<br>";

This should be pretty self-explanatory now.

  • Simply parse the date string into a new DateTime() object.
  • Then use format() to turn it into whatever format required – PHP offers a few predefined common date formats, or you can manually specify your own. All of which, are detailed in the Date Time Format manual.

 

 

2) FORMATTING UNIX TIMESTAMP

2-unix.php
<?php
// (A) UNIX TIMESTAMP
$datetime = strtotime("2019-08-07 06:05:04");
 
// (B) STANDARD DATE/TIME FORMATS
// (B1) ISO 8601 - USE ATOM, NOT DATETIMEINTERFACE::8601!
echo date(DateTimeInterface::ATOM, $datetime) . "<br>";
// (B2) RFC2822
echo date(DateTimeInterface::RFC2822, $datetime) . "<br>";
// (B3) COOKIE
echo date(DateTimeInterface::COOKIE, $datetime) . "<br>";
 
// (C) COMMON DATE TIME FORMAT
// (C1) MYSQL
echo date("Y-m-d H:i:s") . "<br>";
// (C2) DAY DD/MM/YYYY HH:MM:SS AM/PM
echo date("l d/m/Y h:i:s A", $datetime) . "<br>";
// (C3) DD-MMM-YYYY, DAY
echo date("d-M-Y, D", $datetime) . "<br>";
// (C4) DD MONTH YYYY HHMM, DAY 
echo date("j F Y Hi, l", $datetime) . "<br>";
// (C5) WEEK-YYYY
echo date("W-Y", $datetime) . "<br>";

This is the so-called “alternative” way.

  • We parse the date string into a Unix Timestamp using strtotime().
  • Then use date() to format the date – It follows the exact same format as above.

For you guys who are new, the Unix Timestamp is the number of seconds that has elapsed since 1 Jan 1970. So the difference here is pretty much – If you want to work with a DateTime object, or a Unix Timestamp (integer).

 

 

USEFUL BITS & LINKS

That’s all for the main tutorial, and here is a small section on some extras and links that may be useful to you.

 

INFOGRAPHIC CHEAT SHEET

Format Date-Time In PHP (click to enlarge)

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

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