00 Votes

PHP: Current Date and Time

Tutorial by Stefan Trost | Last update on 2021-05-10 | Created on 2016-07-23

In this tutorial, I want to show you how to determine, output and display the current state and time using PHP.

For showing date and time, we can use the function date() provided by PHP. As the first parameter, we can specify how the timestamp should be formatted, as the second parameter, we can optionally pass the time we would like to output. If we omit the second parameter, PHP automatically takes the current time.

Show Date and Time

Let's look at an simple example of this first:

echo date("d.m.Y H:i:s");      // output current date and time

This code prints out the current date and the current time in the form day.month.year hour:minute:second.

Each of the letters used stands for a component of the date or the time (the periods, spaces or semicolons remain). For example, the "d" stands for the day of the month, the "m" for the month, the "Y" for the year, "H" for the hour, "i" for the minute and "s" for the second - each with leading zeros. An overview of all available placeholders can be found on this page.

The use of this format string gives us the option of structuring the date and/or the time according to our own wishes and needs, for example as follows:

echo date("Y-m-d H:i:s");      // output current date and time 

Here we start with the year and use hyphens instead of periods as separators.

By the way, it is important to pay attention to the correct use of upper and lower case letters with those placeholders, otherwise the letters mean something different. For example, "Y" writes the year with 4 digits and "y" the year with 2 digits.

Show only Parts of Date and Time

If we only want to print out the date, only the time or for example only the year, we can change the format correspondingly.

echo date("d.m.Y");            // output current date
echo date("h:i:s");            // output current time
echo date("Y");                // output current year

Until now we have always left out the second parameter of the date function, so that PHP has by default taken the current time as the basis for the output. Now let's see how we can do the same by passing a variable.

Work with Variables

If we want to store the current time in a variable, we can do this with the help of the time() function:

$d = time();                   // save current date and time in $d
echo date("d.m.Y h:i:s", $d);  // output $d

Here, the function time() returns the current timestamp which we are saving in the variable $d. After that, we are using the second parameter of date() for the first time in order to output the timestamp, by passing $d as the second parameter after our format string.

Modify current Date

Of course, this way also gives us the option of modifying our time stamp before output and thus outputting a date relative to the current time:

$d = time();                    // save current date and time in $d
$d = strtotime("+1 days", $d);  // add a day
echo date("d.m.Y h:i:s", $d);   // output $d

In the example above, we are adding one day to the timestamp stored in $d with the help of the function strtotime, so that automatically the date of tomorrow with the same time as today will be echoed.

In addition to "days", among others, we have the following keywords that we can use in the function: year, years, month, months, day, days, hour, hours, minute, minutes, second, seconds.


About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile


Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.


Ask your own question or write your own article on askingbox.com. That’s how it’s done.