Table of contents
1.
Introduction
2.
How to Get the Current Date and Time in PHP?
2.1.
Syntax
2.2.
Parameters
3.
Example 1: Displaying the Current Date and Time
4.
Example 2: Other Ways of Using date() Function
5.
Additional Considerations when Working with Dates in PHP
6.
Commonly Used Characters for Dates
7.
Formatting Examples
8.
PHP Tip - Automatic Copyright Year
9.
Get a Time
10.
Commonly Used Characters for Times
11.
Note on Server Date/Time
12.
Get Your Time Zone
13.
Create a Date With mktime()
13.1.
Syntax of mktime() Function
13.2.
Example of mktime() Function
14.
Create a Date From a String With strtotime()
14.1.
Syntax of strtotime() Function
14.2.
Example of strtotime() Function
14.3.
Using strtotime() With Various Values
14.4.
Limitations of strtotime()
15.
More Date Examples
16.
Frequently Asked Questions
16.1.
What is the purpose of the `date()` function in PHP?
16.2.
How can I get the current date and time in PHP?
16.3.
What is the difference between `strtotime()` and `mktime()` functions in PHP?
17.
Conclusion
Last Updated: Dec 20, 2024
Easy

PHP Current DateTime

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Getting the current date and time is a common task for many PHP developers. PHP provides powerful functions like date() and strtotime() to retrieve and manipulate date and time information. Whether you are building a web application or working on a backend system, understanding how to get and format the current date and time is crucial. 

PHP Current DateTime

This article will cover various ways to display and manage date and time in PHP with clear explanations, examples, and tips.

How to Get the Current Date and Time in PHP?

In PHP, you can use the date() function to get the current date and time. The date() function formats a local date and time, and it is the most common way to display the current date and time.

Syntax

To work with dates in PHP, you can use the built-in `date()` function. The basic syntax for the `date()` function is:

date(format, timestamp)

 

  • The `format` parameter is a string that specifies the desired format of the date. It consists of various characters that represent different parts of the date, such as the year, month, day, hour, minute, & second. For example, `"Y-m-d"` represents the date in the format "YYYY-MM-DD".
  • The `timestamp` parameter is optional & represents the Unix timestamp. If omitted, the current date & time will be used.
     

For example:

<?php
echo date("Y-m-d");
?>


This code will output the current date in the format "YYYY-MM-DD", such as "2023-06-10".

Parameters

The `date()` function accepts a wide range of characters in the `format` parameter to customize the output. Here are some commonly used characters:

  • `Y`: A four-digit representation of the year (e.g., 2023)
  • `m`: A two-digit representation of the month (e.g., 01 for January)
  • `d`: A two-digit representation of the day of the month (e.g., 01 to 31)
  • `H`: A two-digit representation of the hour in 24-hour format (e.g., 00 to 23)
  • `i`: A two-digit representation of the minute (e.g., 00 to 59)
  • `s`: A two-digit representation of the second (e.g., 00 to 59)


You can combine these characters to create various date & time formats. For example:

<?php
echo date("Y-m-d H:i:s"); // Output: 2023-06-10 15:30:45
echo date("F j, Y"); // Output: June 10, 2023
echo date("m/d/Y"); // Output: 06/10/2023
?>


In addition to the `format` parameter, you can also pass a `timestamp` as the second argument to the `date()` function. This allows you to work with specific dates & times. For example:

<?php
$timestamp = mktime(0, 0, 0, 12, 25, 2023); // Christmas Day 2023
echo date("F j, Y", $timestamp); // Output: December 25, 2023
?>


The `mktime()` function is used to create a Unix timestamp based on the provided parameters: hour, minute, second, month, day, & year.

Example 1: Displaying the Current Date and Time

Here is an example that shows how to display the current date and time using the date() function:

<?php
// Display current date and time
echo "The current date and time is: " . date("Y-m-d H:i:s");
?>
You can also try this code with Online PHP Compiler
Run Code


Output:

The current date and time is: 2024-07-17 10:30:00


Explanation:

  • Y represents the year (4 digits).
     
  • m represents the month (2 digits).
     
  • d represents the day (2 digits).
     
  • H represents the hour (24-hour format).
     
  • i represents the minutes.
     
  • s represents the seconds.

Example 2: Other Ways of Using date() Function

You can customize the format of the date and time as per your needs. Here are a few examples:

<?php
// Display only the current date
echo "Today's date is: " . date("Y-m-d");

// Display only the current time
echo "\nThe current time is: " . date("H:i:s");

// Display day, month, and year separately
echo "\nDay: " . date("d");
echo "\nMonth: " . date("F");
echo "\nYear: " . date("Y");
?>
You can also try this code with Online PHP Compiler
Run Code


Output:

Today's date is: 2024-07-17
The current time is: 10:35:15
Day: 17
Month: July
Year: 2024

Additional Considerations when Working with Dates in PHP

When working with dates in PHP, there are a few additional considerations to keep in mind:

  • Timezone Settings- By default, PHP uses the server's timezone settings. However, you can change the timezone using the `date_default_timezone_set()` function. This is particularly important when dealing with dates & times across different regions or when working with user-specific timezones. 
     
  • Date Calculations- PHP provides functions like `strtotime()` & `mktime()` to perform date calculations. The `strtotime()` function allows you to parse a human-readable date string & convert it into a Unix timestamp. You can then use the resulting timestamp with the `date()` function to format the date as needed. 
     
  • Localization- If your application needs to display dates in different languages or formats based on the user's locale, you can use PHP's built-in localization functions, such as `strftime()` & `setlocale()`. These functions allow you to format dates according to locale-specific conventions. 

Commonly Used Characters for Dates

Here is a table of commonly used characters for formatting dates:

CharacterDescriptionExample
Y4-digit year2024
y2-digit year24
mNumeric month07
FFull month nameJuly
dDay of the month17
lDay of the weekWednesday

Formatting Examples

<?php
// Full date and time
echo date("l, F d, Y H:i:s");

// Display date in a specific format
echo "\nDate: " . date("d/m/Y");
?>
You can also try this code with Online PHP Compiler
Run Code


Output:

Today's date is: 2024-12-20
The current time is: 16:12:23
Day: 20
Month: December
Year: 2024

PHP Tip - Automatic Copyright Year

You can use the date() function to keep your copyright year up-to-date automatically:

<?php
echo "&copy; " . date("Y") . " Your Company Name.";
?>
You can also try this code with Online PHP Compiler
Run Code


Output:

&copy; 2024 Your Company Name.

Get a Time

To get the current time only:

<?php
echo "Current time: " . date("H:i:s");
?>
You can also try this code with Online PHP Compiler
Run Code


Output:

Current time: 10:45:00

Commonly Used Characters for Times

CharacterDescriptionExample
HHour (24-hour)10
hHour (12-hour)10
iMinutes45
sSeconds00
AAM/PMAM

Note on Server Date/Time

The date() function depends on the server's time zone. If your server is in a different region, the time may not match your local time. To adjust this, use the date_default_timezone_set() function.

Example:

<?php
// Set time zone to Asia/Kolkata
date_default_timezone_set("Asia/Kolkata");
echo "The current time is: " . date("H:i:s");
?>

Get Your Time Zone

To check your server's current time zone:

<?php
echo "Server Time Zone: " . date_default_timezone_get();
?>

Create a Date With mktime()

The mktime() function allows you to create a date from specific values like hour, minute, and second.

Syntax of mktime() Function

int mktime (int $hour, int $minute, int $second, int $month, int $day, int $year)

Example of mktime() Function

<?php
$customDate = mktime(12, 30, 0, 7, 17, 2024);
echo "Custom Date and Time: " . date("Y-m-d H:i:s", $customDate);
?>
You can also try this code with Online PHP Compiler
Run Code


Output:

Custom Date and Time: 2024-07-17 12:30:00

Create a Date From a String With strtotime()

The strtotime() function converts a string representation of a date and time into a Unix timestamp.

Syntax of strtotime() Function

int strtotime (string $time)

Example of strtotime() Function

<?php
$futureDate = strtotime("+5 days");
echo "Date after 5 days: " . date("Y-m-d", $futureDate);
?>
You can also try this code with Online PHP Compiler
Run Code


Output:

Date after 5 days: 2024-07-22

Using strtotime() With Various Values

<?php
echo date("Y-m-d", strtotime("next Monday"));
?>
You can also try this code with Online PHP Compiler
Run Code


Output

2024-07-22

Limitations of strtotime()

The strtotime() function may not correctly interpret ambiguous or non-standard date formats. It works well with standard strings like +1 day, tomorrow, etc.

More Date Examples

Let’s take a look at a few more examples to showcase the versatility of working with dates in PHP:

1. Calculating the difference between two dates:

<?php
$date1 = strtotime("2023-06-01");
$date2 = strtotime("2023-07-15");
$difference = $date2 - $date1;
$days = floor($difference / (60 * 60 * 24));
echo "The difference between the dates is " . $days . " days.";
?>
You can also try this code with Online PHP Compiler
Run Code


Output:

The difference between the dates is 44 days.

 

2. Formatting a date in a specific style:

<?php
$timestamp = strtotime("2023-06-10");
echo date("l, F jS, Y", $timestamp); // Output: Saturday, June 10th, 2023
?>

 

3. Checking if a date is valid:

<?php
$date = "2023-02-30"; // Invalid date (February 30th doesn't exist)
if (strtotime($date) === false) {
    echo "Invalid date";
} else {
    echo "Valid date";
}
?>
You can also try this code with Online PHP Compiler
Run Code


Output:

Invalid date

Frequently Asked Questions

What is the purpose of the `date()` function in PHP?

The `date()` function in PHP is used to format dates and times according to a specified format string. It allows you to display dates in various formats and perform date-related operations.

How can I get the current date and time in PHP?

To get the current date and time in PHP, you can use the `date()` function without passing any arguments. For example: `echo date("Y-m-d H:i:s");` will output the current date and time in the format "YYYY-MM-DD HH:MM:SS".

What is the difference between `strtotime()` and `mktime()` functions in PHP?

The `strtotime()` function parses a human-readable date/time string and returns a Unix timestamp, while the `mktime()` function creates a Unix timestamp based on the provided parameters (hour, minute, second, month, day, year). `strtotime()` is more flexible as it can parse various date/time formats, whereas `mktime()` requires specific parameters.

Conclusion

In this article, we have discussed the basics of working with dates in PHP. We covered the syntax of the `date()` function, the various parameters it accepts to format dates, and additional considerations such as timezone settings, date calculations, and localization. We also looked at practical examples to demonstrate how to format dates, calculate date differences, and validate date values. 

Live masterclass