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.
In this tutorial, you will learn date and time function in PHP-
PHP Date Syntax & Example
PHP Date the following basic syntax
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.
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
Assuming you saved the file timestamp.php in phptuts folder, browse to the URL //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.
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 //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.
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.
Assuming you have saved the file set_time_zone.php in the phptuts folder, browse to the URL //localhost/phptuts/set_time_zone.php
PHP Mktime Function
The mktime function returns the timestamp in a Unix format.
It has the following syntax.
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.
The microtime[] function returns the current Unix timestamp with microseconds. By default, this function returns a string value which contains microseconds and seconds separated by space [msec sec].
Syntax
microtime[$get_as_float]
Parameters
Sr.NoParameter & Description1get_as_float[Optional]
This is a boolean value which is used to specify whether the result should be a floating point value or not. If you pass the boolean value true as a parameter, this function returns result as floating point value.
Return Values
PHP microtime[] function returns the current Unix timestamp. By default this returns a string value in the form msec sec. If you pass the boolean value true as a parameter to this method, it returns the current time in seconds since the Unix epoch accurate to the nearest microsecond.