What does date() do in PHP?

Learn about the powerful date() function in PHP with our comprehensive guide. Discover how to format dates and times, manipulate timezones, and more using this essential PHP function. Start optimizing your PHP code today!

Talking from experience, being able to manipulate time as developer is a great asset that many beginners and even senior developers fail to invest the time and effort to learn how it can be used effectively. With pretty much every system, there’s the need to work with timestamps and dates to keep track application cycles and recursive operations that may be executed in a period of time.

What date() does is to create a string of the current unix timestamp and store it to a format of your choosing depending what parameters you pass. It will return either the current time if no timestamp is given or the formatted string should such timestamp integer is provided.

Display the date and time

In order to display the date in the traditional YYYY-MM-DD format you only need to create a variable that you will store the return value of date(). To display this particular format, you need to pass it into the date function like so:

$current = date('Y-m-d');
echo $current; // prints: XXX-XX-XX

What if we want to make a timestamp in order to record the time in hours minutes and seconds? In this case you’ll have to extend the initial string format that you pass in the function. Let see how you do this.

$current = date('Y-m-d H:i:s');
echo $current; // prints: XXXX-XX-XX 19:46:45

How to Add Days to Date in PHP

Extending a date by a certain period is a very powerful asset to your developer journey. Here’s how you can add or remove days from a date. It’s really simple if you get the hang of it. It only needs practice and perhaps certain use case to put it into perspective.

$date = date('Y-m-d');
echo $date . '<br>';
echo date('Y-m-d', strtotime($date.' + 5 days'));

$date = date('Y-m-d');
echo $date . '<br>';
echo date('Y-m-d', strtotime($date.' - 15 days'));

You may want to do it more dynamic and reusable by passing the action (add or remove), the date of your choice and the number of days in a function that will spit out the new date. If you don’t know how to create functions, read here. That’s more practical and neat. Go ahead and add it to your toolkit.

function date_days($action = 'add', $date = '', $days = '5') {
if ($date == '') return;
if ($action == 'add') {
return date('Y-m-d', strtotime($date.' + ' . $days . ' days'));
}
if ($action == 'remove') {
return date('Y-m-d', strtotime($date.' - ' . $days . ' days'));
}
}
$date = date('Y-m-d');
echo $date . '<br>';
echo date_days('add', $date, '19'); // add 19 days to the current date
echo '<hr>';
echo $date . '<br>';
echo date_days('remove', $date, '355'); // remove 355 days from the current date

Display the difference between two dates

This is a very occurring change request that you are going to be tasked to make at some point. Now that you know how to record time down to seconds, it’s time to learn how to determine a delta. Deltas are useful when working with some kind of reservation or billing module. You always want to leave a few days to the user to pay an invoice or extend a reservation to a hotel. To add or remove days from a date, refer to the previous paragraph. But how do we calculate the difference between two dates? Well, you only have to find each timestamp of the two designated dates (this operation returns a number!). We’re going to use the previous function to create two distinctive dates and find their delta.

$start = date_days('add', date('Y-m-d'), '19');
$end = date_days('remove', date('Y-m-d'), '293');

$start = strtotime($start);
$end = strtotime($end);

echo '<pre>';
print_r(getdate(abs($start-$end)));

You can see below that this operation will return an array. Now it’s just a matter of putting everything together in any format you deem fit. It’s really up to you.

Array
(
    [seconds] => 0
    [minutes] => 0
    [hours] => 1
    [mday] => 9
    [wday] => 1
    [mon] => 11
    [year] => 1970
    [yday] => 312
    [weekday] => Monday
    [month] => November
    [0] => 26956800
)