w3docs logo

Understanding PHP's Date Function

PHP's date function is an essential tool for developers to display, format, and manipulate dates and times in their web applications. Whether you're working with dates in a database, or need to display the current date and time on a website, the date function in PHP is the go-to tool for the job.

In this article, we'll dive into the details of PHP's date function and how to use it to its full potential. From basic usage to advanced time manipulation, we'll cover everything you need to know to use the date function like a pro.

Basic Usage

The basic syntax of PHP's date function is as follows:

date(format, timestamp);

The format parameter is a string that specifies the format you want the date to be displayed in. For example, you might use d/m/Y to display the date in the format dd/mm/yyyy. The timestamp parameter is an optional value that represents the number of seconds that have passed since January 1, 1970. If no timestamp is provided, the function will use the current date and time.

Here's a simple example of using the date function to display the current date:


echo date("d/m/Y");


This code would output the current date in the format dd/mm/yyyy.

Formatting Options

There are many formatting options available when using the date function in PHP. The following table lists some of the most commonly used options:

Format CodeDescriptionExample
dDay of the month (2 digits)01 - 31
mMonth (2 digits)01 - 12
YYear (4 digits)1970 - 2038
HHour (2 digits, 24-hour format)00 - 23
iMinutes (2 digits)00 - 59
sSeconds (2 digits)00 - 59

By combining different format codes, you can create custom date and time formats to suit your specific needs. For example, you might use the format Y-m-d H:i:s to display the date and time in the format yyyy-mm-dd hh:mm:ss.

Advanced Time Manipulation

In addition to basic date formatting, the date function in PHP also provides several advanced time manipulation options. For example, you can use the strtotime function to convert human-readable strings into timestamps.

Here's an example of using strtotime to convert a date string into a timestamp:


$timestamp = strtotime("2022-01-01");
echo date("d/m/Y", $timestamp);


This code would convert the string "2022-01-01" into a timestamp, and then use the date function to display the date in the format dd/mm/yyyy.


In conclusion, PHP's date function is a versatile and essential tool for web developers. With its wide range of formatting options and advanced time manipulation capabilities, it is the go-to tool for date and time management in PHP

Do you find this helpful?