PHP Date() & Time Function: How to Get Current Timestamp?
PHP date() Function
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 a data in a database.
PHP Date Syntax & Example
PHP Date the following basic syntax
<?php date(format,[timestamp]); ?>
HERE,
- โdate(โฆ)โ is the function that returns the current timestamp in PHP on the server.
- โformatโ is the general format which we want our output to be i.e.;
- โY-m-dโ for PHP date format YYYY-MM-DD
- โYโ to display the current year
- โ[timestamp]โ is optional. If no timestamp has been provided, PHP will get the current PHP date time on the server.
Letโs look at a basic example that displays the current year.
<?php
echo date("Y");
?>
Output:
2018
What is a TimeStamp?
A timestamp in PHP is a numeric value in seconds between the current time and value as 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 date_default_timezone_set function.
The code below displays the current time stamp
<?php echo time(); ?>
Assuming you saved the file timestamp.php in phptuts folder, browse to the URL http://localhost/phptuts/timestamp.php
Note: the value of the timestamp PHP 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โs 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 DateandTime Zone built in class.The listIdentifiers method returns a list of constants that are assigned to the variable $timezone_identifiers.
- โforeach{โฆ}โ iterates through the numeric array and prints the values.
Assuming you saved the file list_time_zones.php in phptuts folder, browse to the URL http://localhost/phptuts/list_time_zones.php
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 in PHP function 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
PHP Mktime Function
The mktime function returns the timestamp in a Unix 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 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 day saving time (DST). 1 is for DST, 0 if it is not and -1 if it is unknown.
Letโs 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.
- โ13โ is the day of the month
- โ10โ is the month of the year
- โ2025โ is the year
Output:
1760328000
PHP Date function reference
The table below shows the common parameters used when working with the PHP date functions.
PHP 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 [Sub to Sat] |
<?php
echo date("D");
?>
|
| โlโ | Returns day name of the week [Sunday to Saturday] |
<?php
echo date("l");
?>
|
| โwโ | Returns day of the week without leading zeroes [0 to 6] Sunday is represent 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 [01 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โs a leap year and 0 if it is not a leap year |
<?php
echo date("L");
?>
|
| โYโ | Returns four digit year format |
<?php
echo date("Y");
?>
|
| โyโ | Returns two (2) digits year format (00 to 99) |
<?php
echo date("y");
?>
|
Summary
- The date function in PHP is used to format the timestamp into a human desired format.
- The timestamp is the number of seconds between the current time and 1st January, 1970 00:00:00 GMT. It is also known as the UNIX timestamp.
- All PHP date() functions use the default time zone set in the php.ini file
- The default time zone can also be set programmatically using PHP scripts.



