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.
$date = new DateTime('2017-02-20');
If you want to use the procedural style.
$date = date_create('2017-02-20');
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.
echo $date->format('Y-m-d');
Once again, if you use procedural style, you need to code in a different way.
echo date_format($date, 'Y-m-d');
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).
2017-02-20
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.
echo date("r", strtotime("now"));
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.
Wed, 22 Feb 2017 18:03:55 +0000
If you want to pass our own date that we have created, you can do that like this
echo date('d M Y', strtotime($date->format('d-m-Y'))); // Output // 20 Feb 2017
You can check the list of all formats to display in php.net. Here are some more examples
echo date('F j, Y, g:i a', strtotime($date->format('d-m-Y'))); // Output // February 20, 2017, 12:00 am echo date('jS \of F Y', strtotime($date->format('d-m-Y'))); // Ouput // 20th of February 2017
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.
function display_time_ago($datetime, $full = false) { $now = new DateTime; $ago = new DateTime($datetime); $diff = $now->diff($ago); $diff->w = floor($diff->d / 7); $diff->d -= $diff->w * 7; $string = array( 'y' => 'year', 'm' => 'month', 'w' => 'week', 'd' => 'day', 'h' => 'hour', 'i' => 'minute', 's' => 'second', ); foreach ($string as $k => &$v) { if ($diff->$k) { $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : ''); } else { unset($string[$k]); } } if (!$full) $string = array_slice($string, 0, 1); return $string ? implode(', ', $string) . ' ago' : 'just now'; }
Now you can use display_time_ago() function and give it date.
Here is the example.
$date = date_create('2017-02-20'); echo display_time_ago($date->format('d-m-Y')); // 2 days ago
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. 🙂
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.
usefull