How to format or manipulate dates and times with PHP

PHP has a a great many options for formatting and manipulating dates. I always find myself going back and forth to find the same examples for reference online or through previous projects, so I have compiled a cheat sheet of sorts of all the frequently used date and time options in PHP.

Getting or setting the date in PHP

First things first, we need to set a date to run any examples. I will use the $currentdate varible in all of the following examples for ease of understanding.

Your date could come from many places, you might set it manually like below, in the standard database style format of year, month, date:

$currentdate = "2017-12-01";

Or you might want to get the current date from the server, like this:

$currentdate = date();

Converting human readable date formats using the awesome strtotime() PHP function

You could also have the date provided in a number of possible formats from apis, a database, user input, javascript and the list goes on. In all of these cases using the strtotime() PHP function will be of great assistance to you. Here is what it looks like:

strtotime($currentdate)

This built in PHP function works to convert a human readable date into the Unix time, which is basically a number, like 1512164814 for the date 1/12/2017, then you are able to reformat the date any way you like.

<?php echo date('Y-m-d', strtotime($currentdate)); ?>

I have found that wrapping every date with strtotime() just make life so much easier.

Formatting Dates

Now that you have a date, we can formatting of the date can all be done within the first property of the PHP date function, here are some examples, all of which I have used the $currentdate variable, which you may replace with your own variable name, database or array code.

Desired Format Sample Code
2017-12-01
<?php echo date('Y-m-d', strtotime($currentdate)); ?>
01/12/17
<?php echo date('d/m/y', strtotime($currentdate)); ?>
1 Dec 2017
<?php echo date('j M Y', strtotime($currentdate)); ?>
Friday 1st of December 2017
<?php echo date('l jS \of F Y', strtotime($currentdate)); ?>
it is the 1st day of the month.
<?php echo date('\i\t \i\s \t\h\e jS \d\a\y.', strtotime($currentdate)); ?>
December
<?php echo date('F', strtotime($currentdate)); ?>
2017-12-01 22:56:20
<?php echo date('Y-m-d H:i:s', strtotime($currentdate)); ?>
10:56 pm
<?php echo date('g:i a', strtotime($currentdate)); ?>

The great thing about the above examples is that you can mix and match to make up almost any date and time format you can think of. For a full reference visit the PHP Date Function page

Adding, subtracting or other date manipulation.

PHP also has some great built-in functions for manipulating dates that are so simple to implement.

// Add 1 month
echo date('d M Y', strtotime($currentdate.'+ 1 month')) . "<br>"; 
// Add 7 days
echo date('d M Y', strtotime($currentdate.'+ 7 days')) . "<br>"; 
// Subtract 3 days
echo date('d M Y', strtotime($currentdate.'- 3 days')) . "<br>";
 
// Subtract 3 years
echo date('d M Y', strtotime($currentdate.'- 3 years')) . "<br>";

Other interesting date functions

// Date of monday this week
echo date('d M Y', strtotime('Monday this week'));

// Date of monday last week
echo date('d M Y', strtotime('Saturday last week'));

// Whether it's a leap year or not
echo date('L', strtotime($currentdate));

// Number of days in the given month
echo date('t', strtotime($currentdate));

// The day of the year (starting from 0)
echo date('z', strtotime($currentdate));

// Week number of year, weeks starting on Monday
echo date('W', strtotime($currentdate));

Some other great date resources:

http://php.net/manual/en/function.date.php
https://www.w3schools.com/php/func_date_date.asp
https://www.epochconverter.com/

Leave a Reply

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