Formatting PHP Dates and Time: Step by Step Guide

PHP is by far the most popular web programming language. Almost for every project that’s done in PHP, you work with date and times. Working and formatting PHP dates and times might be a little hard and complicated at first, but they will be interesting and useful, and you become more expert in PHP language. In this article, we focus on formatting PHP dates and time and how we can display dates in a variety of different styles.

How to Create PHP Dates (Basic)

Let’s imagine we create a date by ourselves. Of course, you might bring it from database in your real applications. So, to create date and time in PHP, all you need is to use the DateTime() function.

If you want to use the procedural style.

So, now we have the $date variable. You can’t directly echo the $date variable, because it’s not string. In order to convert it to string, you must use format() function.

Once again, if you use procedural style, you need to code in a different way.

Since then, we will write only OOP code.

The output of our code will be like this. (at the time I am writing this article).

So now we have our date format.

Formatting PHP Dates

Let’s start formatting our date and make it more human readable. We can use date() and strtotime() functions for formatting our date.  date() is used to format timestamps into a human readable string, and takes a formatting argument and an optional time argument. You can use strtotime() function to convert a date string into a timestamp. However, strtotime() doesn’t recognize the y-m-d-h-i-s format. So, you must pass a string format or give it human readable strings like: now, +1 week, next month, etc.

Simply, use date() function and give strtotime() as second parameter.

The “r” formatting string returns the time formatted as specified by RFC 2822. Of course, you can use other specifier to define your own custom formats.

The output will show like this.

If you want to pass our own date that we have created, you can do that like this

You can check the list of all formats to display in php.net. Here are some more examples

 

Human Readable Date Time Using PHP

If you learned formatting PHP dates, let’s step more forward and find out how to display more human readable dates in PHP.  You might have seen this before in any websites, that shows, this article was posted three days ago or 2 hours ago.

How would you change a date to show the difference of now and the specified date.

We can create a function to do that for us. Here is the function.

Now you can use display_time_ago() function and give it date.

Here is the example.

Conclusion

We hope you have learned something new about formatting PHP dates and time. If you have any further questions, feel free to comment it bellow. 🙂

<p>Web Designer and Developer. I work with HTML5, CSS3, JavaSript, PHP, WordPress, Laravel … etc.</p>

2 Comments

  1. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care.As always, we appreciate you confidence and trust in us.

Leave a Reply