---
description: PHP date function is an in-built function that simplify working with date data types. The PHP date function is used to format a date or time into a human readable format. It can be used to display the date of article was published. record the last updated
title: PHP Date() &#038; Time Function: How to Get Current Timestamp?
image: https://www.guru99.com/images/php-date-time-functions.png
---

 

[Skip to content](#main) 

**⚡ Smart Summary**

PHP date() Function formats timestamps into human-readable dates and times. This walkthrough explains the date syntax, the Unix timestamp, how to list and set time zones, the mktime function for building timestamps, and a full reference of the time, day, month, and year format characters.

* 📅 **What date() Does:** The date function turns a timestamp into a formatted string using characters such as Y, m, and d for year, month, and day.
* ⏱️ **Unix Timestamp:** A timestamp is the number of seconds since 1 January 1970 GMT, and time() returns the current value.
* 🌍 **Time Zones:** date\_default\_timezone\_set changes the active time zone, and DateTimeZone::listIdentifiers lists every supported zone.
* 🔧 **mktime:** The mktime function builds a timestamp from a specific hour, minute, second, month, day, and year.
* 🔤 **Format Characters:** Single letters control the output, for example H for 24-hour time, l for the weekday name, and F for the month name.
* 📐 **Reference Tables:** Grouped tables list the time, day, month, and year format characters with a runnable example for each.
* 🤖 **AI Assist:** AI tools can generate date formatting and difference code and help resolve confusing time zone bugs.

[ Read More ](javascript:void%280%29;) 

![PHP date\(\) Function](https://www.guru99.com/images/php-date-time-functions.png)

## PHP date() Function

The PHP date function is a built-in function that simplifies working with date data types. The PHP date function is used to format a date or time into a human readable format. It can be used to display the date an article was published or record when data in a database was last updated.

## PHP Date Syntax & Example

PHP date() has the following basic syntax.

<?php
date(format,[timestamp]);
?>

HERE,

* “date(…)” is the function that returns the current timestamp formatted in [PHP](https://www.guru99.com/php-tutorials.html) on the server.
* “format” is the general format we want our output to be, for example:  
  * “Y-m-d” for the PHP date format YYYY-MM-DD
  * “Y” to display the current year
* “\[timestamp\]” is optional. If no timestamp has been provided, PHP will use the current date and time on the server.

Let us look at a basic example that displays the current year.

<?php

echo date("Y");

?>

**Output:**

2026

## What is a TimeStamp?

A timestamp in PHP is a numeric value in seconds between the current time and the value at 1st January 1970 00:00:00 Greenwich Mean Time (GMT).

The value returned by the time function depends on the default time zone.

The default time zone is set in the php.ini file. It can also be set programmatically using the date\_default\_timezone\_set function.

The code below displays the current timestamp.

<?php

echo time();

?>

Assuming you saved the file timestamp.php in the phptuts folder, browse to the URL **http://localhost/phptuts/timestamp.php**

[](https://www.guru99.com/images/2013/04/timestamp.png)

Note: the value of the timestamp is not a constant. It changes every second.

## Getting a list of available time zone identifiers

Before we look at how to set the default time zone programmatically, let us look at how to get a list of supported time zones.

<?php

$timezone_identifiers = DateTimeZone::listIdentifiers();

foreach($timezone_identifiers as $key => $list){

echo $list . "<br/>";

}
?>

HERE,

* “$timezone\_identifiers = DateTimeZone::listIdentifiers();” calls the listIdentifiers static method of the DateTimeZone built-in class. The listIdentifiers method returns a list of identifiers that are assigned to the variable $timezone\_identifiers.
* “foreach{…}” iterates through the array and prints the values.

Assuming you saved the file list\_time\_zones.php in the phptuts folder, browse to the URL **http://localhost/phptuts/list\_time\_zones.php**

[](https://www.guru99.com/images/2013/04/list%5Ftime%5Fzones.png)

## PHP set Timezone Programmatically

The date\_default\_timezone\_set function allows you to set the default time zone from a PHP script.

The set time zone will then be used by all date [PHP functions](https://www.guru99.com/functions-in-php.html) in your scripts. It has the following syntax.

<?php
date_default_timezone_set(string $timezone_identifier);
?>

HERE,

* “date\_default\_timezone\_set()” is the function that sets the default time zone
* “string $timezone\_identifier” is the time zone identifier

The script below displays the time according to the default time zone set in php.ini. It then changes the default time zone to Asia/Calcutta and displays the time again.

<?php
echo "The time in " . date_default_timezone_get() . " is " . date("H:i:s");

date_default_timezone_set("Asia/Calcutta");
echo "The time in " . date_default_timezone_get() . " is " . date("H:i:s");
?>

Assuming you have saved the file set\_time\_zone.php in the phptuts folder, browse to the URL **http://localhost/phptuts/set\_time\_zone.php**

[](https://www.guru99.com/images/2013/04/timezone.png)

### RELATED ARTICLES

* [How to Send Email using PHP mail() Function ](https://www.guru99.com/php-mail.html "How to Send Email using PHP mail() Function")
* [PHP MVC Framework Tutorial ](https://www.guru99.com/php-mvc-frameworks.html "PHP MVC Framework Tutorial")
* [PHP Control Structures: If else, Switch Case ](https://www.guru99.com/control-structures-and-loops.html "PHP Control Structures: If else, Switch Case")
* [Top 100 PHP Interview Questions and Answers (PDF) ](https://www.guru99.com/php-interview-questions-answers.html "Top 100 PHP Interview Questions and Answers (PDF)")

## PHP Mktime Function

The mktime function returns the timestamp in a [Unix](https://www.guru99.com/unix-linux-tutorial.html) format.

It has the following syntax.

<?php
mktime(hour, minute, second, month, day, year, is_dst);
?>

HERE,

* “mktime(…)” is the make PHP timestamp function
* “hour” is optional; it is the number of the hour
* “minute” is optional; it is the number of minutes
* “second” is optional; it is the number of seconds
* “month” is optional; it is the number of the month
* “day” is optional; it is the number of the day
* “year” is optional; it is the number of the year
* “is\_dst” is optional; it is used to determine the daylight saving time (DST). 1 is for DST, 0 if it is not, and -1 if it is unknown.

Let us now look at an example that creates a timestamp for the date 13/10/2025 using the mktime function.

<?php

echo mktime(0,0,0,10,13,2025);

?>

HERE,

* “0,0,0” is the hour, minute, and seconds respectively.
* “10” is the month of the year
* “13” is the day of the month
* “2025” is the year

**Output:**

1760328000

## PHP Date Format Reference

The tables below show the common format characters used when working with the PHP date functions.

### Time parameters

| Parameter | Description                                                                   | Example                                  |
| --------- | ----------------------------------------------------------------------------- | ---------------------------------------- |
| “r”       | Returns the full date and time                                                | <?php echo date("r"); ?>                 |
| “a”, “A”  | Returns whether the current time is am or pm, AM or PM respectively           | <?php echo date("a"); echo date("A"); ?> |
| “g”, “G”  | Returns the hour without leading zeroes \[1 to 12\], \[0 to 23\] respectively | <?php echo date("g"); echo date("G"); ?> |
| “h”, “H”  | Returns the hour with leading zeros \[01 to 12\], \[00 to 23\] respectively   | <?php echo date("h"); echo date("H"); ?> |
| “i”, “s”  | Returns the minutes/seconds with leading zeroes \[00 to 59\]                  | <?php echo date("i"); echo date("s"); ?> |

### Day parameters

| Parameter | Description                                                                                                                                 | Example                  |
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------ |
| “d”       | Returns the day of the month with leading zeroes \[01 to 31\]                                                                               | <?php echo date("d"); ?> |
| “j”       | Returns the day of the month without leading zeroes \[1 to 31\]                                                                             | <?php echo date("j"); ?> |
| “D”       | Returns the first 3 letters of the day name \[Sun to Sat\]                                                                                  | <?php echo date("D"); ?> |
| “l”       | Returns the day name of the week \[Sunday to Saturday\]                                                                                     | <?php echo date("l"); ?> |
| “w”       | Returns the day of the week without leading zeroes \[0 to 6\]; Sunday is represented by zero (0) through to Saturday represented by six (6) | <?php echo date("w"); ?> |
| “z”       | Returns the day of the year without leading spaces \[0 through to 365\]                                                                     | <?php echo date("z"); ?> |

### Month Parameters

| Parameter | Description                                                  | Example                  |
| --------- | ------------------------------------------------------------ | ------------------------ |
| “m”       | Returns the month number with leading zeroes \[01 to 12\]    | <?php echo date("m"); ?> |
| “n”       | Returns the month number without leading zeroes \[1 to 12\]  | <?php echo date("n"); ?> |
| “M”       | Returns the first 3 letters of the month name \[Jan to Dec\] | <?php echo date("M"); ?> |
| “F”       | Returns the month name \[January to December\]               | <?php echo date("F"); ?> |
| “t”       | Returns the number of days in a month \[28 to 31\]           | <?php echo date("t"); ?> |

### Year Parameters

| Parameter | Description                                                   | Example                  |
| --------- | ------------------------------------------------------------- | ------------------------ |
| “L”       | Returns 1 if it is a leap year and 0 if it is not a leap year | <?php echo date("L"); ?> |
| “Y”       | Returns the four digit year format                            | <?php echo date("Y"); ?> |
| “y”       | Returns the two digit year format (00 to 99)                  | <?php echo date("y"); ?> |

## FAQs

🔄 What is the difference between date() and the DateTime class in PHP?

date() is a simple procedural function that formats a timestamp. The DateTime class is object oriented and supports arithmetic, comparison, time zones, and immutability, making it better for complex date logic in larger applications.

🗓️ How do I convert a string to a timestamp in PHP?

Use strtotime(), which reads formats like ‘2025-10-13’ or ‘next Monday’ and returns a Unix timestamp. For strict parsing of a known format, DateTime::createFromFormat is safer because it fails clearly on invalid input.

➖ How do I calculate the difference between two dates in PHP?

Create two DateTime objects and call diff(), which returns a DateInterval. You can then read days, months, or years, for example $start->diff($end)->days to get the total number of days between the dates.

🤖 Can AI generate PHP code to format or manipulate dates?

Yes. Describe the input and the output format, and AI can produce date(), DateTime, or strtotime code, including adding intervals or converting time zones. Test it with edge cases like leap years and month ends.

🤖 Can AI help fix timezone bugs in my PHP date code?

Yes. AI can spot a missing date\_default\_timezone\_set call, a server versus display time zone mismatch, or daylight saving issues, then recommend using DateTime with explicit time zones to make the behavior predictable.

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter]() Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/php-date-time-functions.png","url":"https://www.guru99.com/images/php-date-time-functions.png","width":"700","height":"250","caption":"PHP Date() &amp; Time Functions","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/php-date-functions.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/php","name":"PHP"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/php-date-functions.html","name":"PHP Date() &#038; Time Function: How to Get Current Timestamp?"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/php-date-functions.html#webpage","url":"https://www.guru99.com/php-date-functions.html","name":"PHP Date() &#038; Time Function: How to Get Current Timestamp?","dateModified":"2026-07-25T10:03:27+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/php-date-time-functions.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/php-date-functions.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/fiona","name":"Fiona Brown","description":"I'm Fiona brown, a Full Stack Developer with over a decade of experience, sharing practical guides on robust and scalable application development.","url":"https://www.guru99.com/author/fiona","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/fiona-brown-author.png","url":"https://www.guru99.com/images/fiona-brown-author.png","caption":"Fiona Brown","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"PHP","headline":"PHP Date() &#038; Time Function: How to Get Current Timestamp?","description":"PHP date function is an in-built function that simplify working with date data types. The PHP date function is used to format a date or time into a human readable format. It can be used to display the date of article was published. record the last updated","keywords":"php","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/fiona","name":"Fiona Brown"},"dateModified":"2026-07-25T10:03:27+05:30","image":{"@id":"https://www.guru99.com/images/php-date-time-functions.png"},"copyrightYear":"2026","name":"PHP Date() &#038; Time Function: How to Get Current Timestamp?","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What is the difference between date() and the DateTime class in PHP?","acceptedAnswer":{"@type":"Answer","text":"date() is a simple procedural function that formats a timestamp. The DateTime class is object oriented and supports arithmetic, comparison, time zones, and immutability, making it better for complex date logic in larger applications."}},{"@type":"Question","name":"How do I convert a string to a timestamp in PHP?","acceptedAnswer":{"@type":"Answer","text":"Use strtotime(), which reads formats like '2025-10-13' or 'next Monday' and returns a Unix timestamp. For strict parsing of a known format, DateTime::createFromFormat is safer because it fails clearly on invalid input."}},{"@type":"Question","name":"How do I calculate the difference between two dates in PHP?","acceptedAnswer":{"@type":"Answer","text":"Create two DateTime objects and call diff(), which returns a DateInterval. You can then read days, months, or years, for example $start-&gt;diff($end)-&gt;days to get the total number of days between the dates."}},{"@type":"Question","name":"Can AI generate PHP code to format or manipulate dates?","acceptedAnswer":{"@type":"Answer","text":"Yes. Describe the input and the output format, and AI can produce date(), DateTime, or strtotime code, including adding intervals or converting time zones. Test it with edge cases like leap years and month ends."}},{"@type":"Question","name":"Can AI help fix timezone bugs in my PHP date code?","acceptedAnswer":{"@type":"Answer","text":"Yes. AI can spot a missing date_default_timezone_set call, a server versus display time zone mismatch, or daylight saving issues, then recommend using DateTime with explicit time zones to make the behavior predictable."}}]}],"@id":"https://www.guru99.com/php-date-functions.html#schema-1149957","isPartOf":{"@id":"https://www.guru99.com/php-date-functions.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/php-date-functions.html#webpage"}}]}
```
