Table of contents
1.
Introduction
2.
Syntax  of PHP date() function  
3.
Parameters
4.
Return Value 
5.
Formatting Options of Date Function in PHP
6.
Examples of PHP Date() Function  
6.1.
1. Use the Date function 
6.2.
PHP
6.3.
2. Using multiple date formats 
6.4.
PHP
7.
Frequently Asked Questions
7.1.
Can the PHP date() function handle custom formats?
7.2.
What is the default timezone used by the date() function in PHP?
7.3.
How does PHP handle invalid timestamps passed to the date() function?
7.4.
How to convert yyyy-mm-dd format to dd-mm-yyyy in PHP? 
7.5.
What is the most used date format in PHP?
8.
Conclusion
Last Updated: Dec 8, 2024
Easy

PHP Date() Function

Author Rinki Deka
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The date() function is in PHP, which locally formats a date and time according to a specified string. The PHP date() function is used to print dates in multiple formats. The function helps in converting the date in a human-readable format. There are various date functions that converts dates into different formats as per the need. 

PHP Date() Function

PHP date() function converts the date function into a readable format. The data is stored in a format called UNIX timestamp. This measures time in seconds like March 1, 1925, i.e. March 1, 1925, 00:00:00 GMT ). The mentioned time frame is not human-readable, so the PHP date function is used to make it more understandable. 

Syntax  of PHP date() function  

One mandatory and one optional parameter is available in the date function. 

date(format, timestamp)

Parameters

format: The format argument here specifies the date and time format of the return type. 


timestamp: This is the optional parameter. If this is not given, the current date format will be taken. 

Return Value 

On success: A formatted string is received when conversion is successful. 

On failure: E_warning message. 

Formatting Options of Date Function in PHP

There are various formatting options available in the PHP date() function. The format parameter is a string that contains characters and helps to generate date in multiple formats. 

The year can be presented in multiple ways by inserting separators like dots(.), slashes(/), or hyper(-).  All these formatting options are supported by the date format. We will look at the examples of each later in this article. 

Some commonly used data formats are discussed below: 

FormatDescriptionExample
dDisplays the day of the month (01 to 31)01, 15, 31
mDisplays month with leading zeros (01 to 12)01, 06, 12
yDisplays year in 2 digits (89 to 24)89, 20, 24
DDisplays the day of the week in text format (Mon to Sun)Mon, Wed, Sun
MDisplays month in text format (Jan to Dec)Jan, May, Dec
YDisplays the year in 4-digit format (1989 to 2024)1989, 2024
hDisplays hour in 12-hour format with leading zeros (01 to 12)01, 07, 12
iDisplays minutes value (01 to 59)05, 30, 59
sDisplays seconds value (01 to 59)01, 30, 59
aDisplays lowercase ante meridiem and post meridiem (am or pm)am, pm
HDisplays hour in 24-hour format with leading zeros (01 to 24)01, 15, 23
ADisplays uppercase ante meridiem and post meridiem (AM or PM)AM, PM

Examples of PHP Date() Function  

1. Use the Date function 

Below is an example of using the PHP date() function. 

  • PHP

PHP

<?php

echo "Current date is : ";

$today = date("d-m-Y");

echo $today;

?>
You can also try this code with Online PHP Compiler
Run Code


Output

The current date is : 27-09-2024

2. Using multiple date formats 

  • PHP

PHP

<?php

echo "Today's date and time in various formats:" . "\n";

echo "Day of the month: " . date("d") . "\n";

echo "Month with leading zeros: " . date("m") . "\n";

echo "Year in 2 digits: " . date("y") . "\n";

echo "Day of the week: " . date("D") . "\n";

echo "Month in text format: " . date("M") . "\n";

echo "Year in 4 digits: " . date("Y") . "\n";

echo "12-hour format with leading zeros: " . date("h") . "\n";

echo "Minutes: " . date("i") . "\n";

echo "Seconds: " . date("s") . "\n";

echo "Lowercase am/pm: " . date("a") . "\n";

echo "24-hour format with leading zeros: " . date("H") . "\n";

echo "Uppercase AM/PM: " . date("A") . "\n";

?>
You can also try this code with Online PHP Compiler
Run Code


Output

Today's date and time in various formats:
Day of the month: 27
Month with leading zeros: 09
Year in 2 digits: 24
Day of the week: Sat
Month in text format: Sep
Year in 4 digits: 2024
12-hour format with leading zeros: 03
Minutes: 15
Seconds: 45
Lowercase am/pm: pm
24-hour format with leading zeros: 15
Uppercase AM/PM: PM

Frequently Asked Questions

Can the PHP date() function handle custom formats?

Yes, the PHP date() function does have the functionality of custom formats by using various characters to format and display specific date and time components as needed. Developers can create unique date formats by combining different characters.

What is the default timezone used by the date() function in PHP?

The default timezone used by the date() function in PHP is generally the system's default timezone. It can be set using the date_default_timezone_set() function if needed.

How does PHP handle invalid timestamps passed to the date() function?

When an invalid timestamp is passed to a date() function, it returns the string "False" or empty. There should always be validation of timestamps before employing them with date().

How to convert yyyy-mm-dd format to dd-mm-yyyy in PHP? 

For converting the below format, first use the strtotime() function to convert textual date format into UNIX timestamp, and then you can use the date function to convert the time in the format you desire. 

What is the most used date format in PHP?

The most general and widely used format is yyyy-mm-dd format which is acceptable in all formal documents. There are many other formats that are supported by PHP. Some of them are dd-mm-yyyy, dd/mm/yyyy, and many more. 

Conclusion

The PHP date() function is one of the essential tools for handling dates and times in web development. It allows developers to format date and time values in multiple ways, thus making it easier to display accurate and customized time information. Whether displaying timestamps, managing time zones, or creating dynamic content, the date() function plays a key role in performing time-related operations effectively in PHP.

You can also check out our other blogs on Code360.

Live masterclass