This tutorial will show you how to use the date() function within your PHP scripts.
The date() function in PHP allows you to print the date and time using the specified format.
You can get the date and time from any point by specifying a Unix timestamp. However, if you don’t specify a timestamp, then the date() function will use the current date and time as retrieved from the “time()” function.
While this function may initially seem confusing, it is relatively simple once you know what characters are replaced within the string.
Over the following sections, you will learn all there is to know about using this function within your scripts.
Syntax of the date() Function in PHP
Before we show you how to use the date() function within PHP, let us start by exploring its syntax. The syntax shows you how the function is written, what data it expects, and what data it returns.
When writing the date or time format, five characters are commonly used to separate each value. These are good values as they are guaranteed not to be replaced by the date() function.
date(string $format, ?int $timestamp = null): string
Let us explore this syntax by going more in-depth on the two parameters.
format– The first parameter allows you to specify the format for the outputted string. What you specify here will dictate the resulting output by this function.
For example, you can use PHP’s date() function to only print the current year. You can also format the string to print the day, month, year, and time if you choose to.
We will shortly explore the values you will use to format your date() strings.
timestamp– The second parameter is entirely optional but is where you can specify a Unix timestamp.
PHP’s date() function will use the current server time if this option is not included or is set to null.
A Unix timestamp is a number typically presented as an integer like the following “
Formatting the Timestamp for PHP’s date() Function
Formatting the timestamp for PHP’s date() function requires you to know the letters that PHP will substitute in the outputted string.
When writing the format for the date or time there is five different characters you will want to use to separate each value. These are good values as they are guaranteed not to be replaced by the date() function.
- Hyphen (
- Dot (
- Forward Slash (
- Semicolon (
- Space (
Please note that this string is case sensitive as certain letters will have a different replacement depending on whether they are lowercase or uppercase.
Date Replacement Characters
To get started, let us first explore the letters used to represent the date within PHP. We will split these into three different categories covering the “day“, “month“m and “year“.
d– This is the day of the month expressed as a two-digit number with leading zeroes (
j– Day of the month without leading zeroes (
S– English ordinal suffix for the day of the month. (
D– The day of the week expressed as an abbreviated string (
l– The full name for the day of the week (
w– A number representation of the current day of the week (
z– Current day of the year (
W– Current week number of the year using the ISO-8601 standard (Weeks start on Monday)
m– The month is expressed as a number with leading zeroes (
n– Month is expressed as a number without leading zeroes (
M– The month expressed as a three-letter string (
F– Replaced with the full name of the month (
t– The number of days for the month in the provided timestamp (
y– Outputs the year as a two-digit number (
Y– This letter is replaced with the year but as a four-digit number (
o– The year is output using the ISO-8601 standard (
Example of using the date() Function to Output the Day
Let us show you this in practice by writing a short PHP script that uses the date() function to print the current date.
With the output from this snippet, you will see the letter “
d” was replaced with the day of the month, “
m” was swapped with the month expressed as a number, and finally, “
Y” was changed with the year as a four-digit number.
We will use PHP’s echo statement to print out the string returned by the date() function.
<?php echo date("d/m/Y"); ?>
Below you can see an example of what was output by our above example.
Time Replacement Characters
The next lot of replacement characters we will explore are the ones that PHP’s date() function will replace with the current time.
h– Replaced with the hour using the 12-hour format with leading zeroes (
g– Swapped with the 12-hour format without leading zeroes (
H– The hour expressed using the 24-hour format with leading zeroes (
G– The current hour using the 24-hour format without leading zeroes (
a– Lowercase ante meridian or post meridian depending on the time (
A– Uppercase ante meridian or post meridian depending on the time (
i– Swapped with the minute (
s– Switched with the current second from the provided timestamp (
u– This letter is switched out with the microseconds from the timestamp (E.G.,
U– Seconds since the Unix epoch (E.G.,
e– This letter is replaced with the time zone (
T– Abbreviation of the timezone (
O– Difference to GMT without colon between hours and minutes (E.G.,
P– Difference to GMT with a colon between hours and minutes (E.G.,
Z– The timezone offset is expressed as seconds (
I– Whether the provided timestamp is in daylight saving time (
1for Daylight Saving Time, or
Example of using the date() Function to Output the Time
To showcase this in action, let us write a short PHP script that uses the date() function to print the current time using 24-hour format.
Since we want the time using the 24-hour format we start our format string with a capital “
H“, followed by “
i” for the minute and “
s” for the second.
<?php echo date("H:i:s"); ?>
Running the above code, you will end up with a result as shown below. Since we didn’t specify a timestamp, this will be your system’s current time.
Full Date/Time Character Replacements for use with the date() Function
The date() function in PHP has several characters that will be replaced with the full date and time using different standards.
c– The date and time using the ISO 8601 standard (E.G.,
r– The date and time using the RFC 2822/RFC 5322 standard (E.G.,
Thu, 21 Dec 2000 16:01:07 +0200)
Let us use the “
r” letter within the date() functions format to showcase how this works within PHP to output the date and time.
<?php echo date("r"); ?>
If you were to run the example above, you would get the date and time output with the following output.
Fri, 04 Nov 2022 16:12:23 +1100
Escaping Characters in the PHP date() Format String
To use letters within your PHP date() format string without it being swapped out with a date or time, then you need to escape it.
You can escape a reserved character by placing a backward slash (
\) in front of it. For example, to use the letter “
a” within the format string, you would use “
To give an example of how this works, let us output the current day and time.
<?php echo date("\D\a\y: D, \T\i\m\\e: H:i"); ?>
After running the above example, you will end up with a result similar to the one we have shown below.
Day: Fri, Time: 16:13
Getting the Current Date and Time in PHP
One of the key reasons you might want to use the date() function within your PHP script is to get the current date and time.
The previous sections covered the various characters you can use in your format string. We will now put these all to use within the following example.
1. You will need to decide whether you want to use your server’s time zone or a different one. By default, PHP will get the server time.
Instead of using a different time zone, you will first need to call the “
Within this function’s singular parameter, you will specify the time zone for your area. For our example, this will be “
You can find the time zone to specify for this function by browsing the official PHP time zone manual.
2. With the time zone set, you can now use PHP’s date() function to output the current date and time.
Here we put all of the characters we learned earlier to use. We start the format string by specifying the date, using a forward slash as the separator.
The letter “
d” is replaced with the current day of the month. We then use the “
m” letter to specify the current month. Finally, to finish the date, we use the capital letter “
Y” to print the year as a four-digit number.
Next, we utilize the letter “
h” to specify the current hour, “
i” to specify the minute, and finally, “
s” to specify the current second.
We end the format string by using the letter “
a” to print “
am” or “
pm“. If you prefer to use 24-hour time, remove “
a” from the timestamp, and change the letter “
h” to a capital “
echo date('d/m/Y h:i:s a');
3. Once put together, your script will look like the following. Using the date function like this, you can get the current date and time in one string.
<?php date_default_timezone_set('Australia/Hobart'); echo date('d/m/Y h:i:s a'); ?>
4. If you were to run this code, you should see the script output the current date and time.
04/11/2022 09:05:54 pm
At this point in the tutorial, you should know how to use the date() function in PHP.
This function is the best way to get the date or time within your PHP scripts.
Please comment below if you have questions about using PHP’s date() function.