PHP Date and Time with Examples
When you are working with any web application, dealing with date and time is a very common task in PHP. For example, an application may need to display the current date and time, record timestamps when a user submits a form, calculate the difference between two dates, or schedule future events.
PHP provides several built-in functions and classes to easily handle date and time operations, such as date(), time(), mktime(), strtotime(), and the DateTime class. These functions and class allow you to format, compare, and manipulate dates and times efficiently in different ways.
In this tutorial, you’ll learn everything about the PHP Date and time functions with syntax, and examples (from basic to advanced).
PHP Date() Function
The date() function in PHP is one of the most commonly used functions for working with date and time. This function allows you to format a local date and time according to a specific pattern. You can use it to display:
- Current date and time
- Custom date formats
- Day, month, and year individually
- Time zones and timestamps
Basic Syntax of Date() Function
The general syntax for date() function in PHP is as follows:
date(format, timestamp);
Parameters
The date() function takes two parameters:
- format (Required): This parameter specifies the format of the output date/time.
- timestamp (Optional): This parameter represents a Unix timestamp (number of seconds since January 1, 1970). If omitted, it uses the current date and time by default.
Return
- The date() function returns a string representation of the current date and time according to the instructions specified by the format.
Basic Example of PHP Date
Let’s take some examples based on the date() function in PHP.
Example 1: Displaying current date
<?php
echo date("Y-m-d");
?>
Output: 2025-10-04
In this example, we have used the date() function to display current date. Here, y-m-d is a date format defined inside the parentheses of date() function, where y represents year (in 4 digit), m represents month (from 01 to 12), and d represents the day (from 01 to 31).
Example 2: Displaying current date and time
<?php
echo date("Y-m-d H:i:s");
?>
Output: 2025-10-04 12:03:06
In this example, H:i:s is a time format to display the current time. In the time format, H represents the 12 hour format of an hour, i represents minutes, and s represents the seconds.
Example 3: Automatic Copyright Year
We use the date() function to automatically refresh the copyright year on your website.
<!DOCTYPE html>
<html>
<body>
© 2018-<?php echo date("Y");?>
</body>
</html>
Common Date Format Characters in PHP
PHP provides different date and time formats that are as follows:
Character | Description | Example Output |
---|---|---|
d | Day of the month (in 2 digits) | 04 |
j | Day of the month (no leading zeros) | 4 |
D | Day name (in 3 letters) | Sat |
l | Full day name | Saturday |
m | Month (in 2 digits) | 10 |
M | Month name (in 3 letters) | Oct |
F | Full month name | October |
y | Year (in 2 digits) | 25 |
Y | Year (in 4 digits) | 2025 |
h | 12-hour format | 12 |
H | 24-hour format | 12 |
i | Minutes | 45 |
s | Seconds | 32 |
a | am/pm (lowercase) | pm |
A | AM/PM (uppercase) | PM |
Example 1: Display full date and time
<?php
echo date("l, F j, Y g:i A");
?>
Output: Saturday, October 4, 2025 12:45 PM
Example 2: Display only time
<?php
echo date("h:i:s A");
?>
Output: 12:45:32 PM
Understanding Unix Timestamp
Most computers store dates and times as Unix timestamps, or simply timestamps. A Unix timestamp is a way to represent a specific date and time as the number of seconds that have passed since midnight (UTC) on January 1, 1970, 00:00:00. This starting point is also called the Unix Epoch.
For example, the date and time “October 04, 2025, 16:45” in the GMT time zone is represented by the Unix timestamp 1764905100, because October 04, 2025, 16:45 occurs exactly 1764905100 seconds after midnight on January 1, 1970.
Unix timestamps are widely used in programming and databases because they make date and time calculations easier, faster, and more consistent across systems.
Example: Display the Current Date and Time from a Timestamp
You can convert a Unix timestamp back to a human-readable date using the date() function.
<?php
$timestamp = time();
echo "Current Timestamp: " . $timestamp . "\n";
echo "Current Date and Time: " . date("Y-m-d H:i:s", $timestamp);
?>
Output: Current Timestamp: 1759576871 Current Date and Time: 2025-10-04 13:21:11
PHP time() Function
The time() function in PHP is a built-in function that returns the current date and time as a timestamp in UTC. This function does not take any parameters. Its general syntax is as follows:
time();
Example 1: Display the Current Unix Timestamp
<?php
echo "Current Unix Timestamp: " . time();
?>
Output: Current Unix Timestamp: 1764921230
Example 2: Convert Timestamp to Readable Date
You can convert the timestamp into a readable format using the date() function.
<?php
$timestamp = time();
echo "Current Date and Time: " . date("Y-m-d H:i:s", $timestamp);
?>
Output: Current Date and Time: 2025-10-04 22:33:50
In this example, we have used the date() function, which formats the timestamp into a human-readable form.
Example 3: Calculate Time Differences
You can use the time() function to measure how long a process takes.
<?php
$start = time();
sleep(5); // Simulates a 5-second delay
$end = time();
$duration = $end - $start;
echo "The process took $duration seconds.";
?>
Output: The process took 5 seconds.
PHP Timezones
If the time is not matching with your current time, it might happen you are using server that is located in another country or set up for a different timezone.
Therefore, if you need the time to be correct according to a specific location, you can set the timezone you want to use. For this, PHP provides a built-in date_default_timezone_set() function. This function is used to set the default timezone for all date and time functions in a script.
By default, PHP uses the timezone set in the server’s configuration file (php.ini). However, if your web application is used in a different region or by global users, you can change the timezone dynamically using this function.
Basic Syntax
date_default_timezone_set(timezone);
Parameter:
- timezone (Required): This is a string representing the timezone identifier, such as “Asia/Kolkata”, “Europe/London”, or “America/New_York”.
Return Value:
- Returns true on success or false on failure.
Example 1: Set Default Timezone
<?php
date_default_timezone_set("Asia/Kolkata");
echo "Current Date and Time in India: " . date("Y-m-d H:i:s A");
?>
Output: Current Date and Time in India: 2025-10-04 19:10:46 PM
In this example, we have set the default timezone to Asia/Kolkata, so all date and time functions will now display time in Indian Standard Time (IST).
Example 2: Compare Different Timezones
<?php
// Set timezone to London.
date_default_timezone_set("Europe/London");
echo "London Time: " . date("Y-m-d H:i:s") . "<br>";
// Set timezone to New York.
date_default_timezone_set("America/New_York");
echo "New York Time: " . date("Y-m-d H:i:s") . "<br>";
// Set timezone to Tokyo.
date_default_timezone_set("Asia/Tokyo");
echo "Tokyo Time: " . date("Y-m-d H:i:s");
?>
Output: London Time: 2025-10-04 17:15:00 New York Time: 2025-10-04 12:15:00 Tokyo Time: 2025-10-04 23:15:00
If you want to check which timezone your PHP script is currently using, you can use date_default_timezone_get() function. This function is used to get the default timezone used by all date/time functions in a script.
Example 3: Check Current Default Timezone
<?php
echo "Current Default Timezone: " . date_default_timezone_get();
?>
Output: Current Default Timezone: Asia/Kolkata
Conclusion
The PHP date() function formats a timestamp into a human-readable date or time string. You can use it to display current date and time, custom date formats, etc. I hope you have understood the basic syntax of the date() function and practiced all example programs. Stay tuned with the next tutorial where you will learn mktime() function in PHP with the help of examples.