PHP date and time
PHP provides the functionality to display date and time on a webpage.
This tutorial focuses on:
- The date() function
- Timestamps
- Displaying a date on a webpage
- Displaying time on a webpage
- Displaying date and time on a webpage
NOTE: Whenever you display the time or date on a webpage using PHP, it will get that data from the web server. If the user has a different time or date set on their computer, PHP will still display the same data from the web server whether it matches up with what the user has on their computer. So if the current date and time on the web server is January 15th 12:30, but the users computer is set to January 10th 8:30, January 15th 12:30 will be displayed. This is in contrast to client-side scripting languages like Javascript which display the data based on what is set on the users computer.
The date() function
The date() function is used in PHP to display time and date on a webpage.
The $dateFormat parameter specifies how a date or time should appear on a webpage. You can specify this with a set of characters PHP uses for date and time elements.
Date() function formatting characters:
- d - A numeric representation of the day of the month (0 - 31)
- d - A numeric representation of the day of the month (0 - 31)
- m - A numeric representation of the month of the year (1 - 12)
- y - A numeric representation (two digit) of a year (99, 00, 01, 02)
- H - 24 hour format current hour (00 - 23)
- i - Minutes (00 - 59)
- s - Seconds (00 - 59)
Timestamps
A timestamp in PHP is the number of seconds that have elapsed since January 1, 1970 00:00. This is also known as the Unix timestamp and it is a widely used standard.
The date() function can actually take two parameters - adate format and a time stamp
The second parameter $timeStamp is optional. If you do not set a time stamp when using the date() function, the default timestamp (mentioned above) is used. A timestamp specifies from when the date() function should begin counting. For example, if you use the date() function with a default timestamp to find out the number of years that have elapsed since the timestamp and today, the value returned will be 38 (since 1970).
Creating a timestamp
You can create your own timestamp using the second parameter of the date() function. When you create your own timestamp, the date() function starts counting time from the time specified with your timestamp. If you set a timestamp starting April 1st, 1980 10:15, then the date() function you assign this timestamp to will begin counting from that timestamp. You can set your own timestamp using the mktime() function.
Displaying a date on a webpage
You can display a date on a webpage using the appropriate characters for the $dateFormat parameter in the date() function:
Displaying time on a webpage
You can display time on a webpage using the appropriate characters for the $dateFormat parameter in the date() function:
Current time in 24 hour format: 21:39:15
Displaying date and time on a webpage
You can display the current date and time together on a webpage using the appropriate characters for the $dateFormat parameter in the date() function:
Date and time (24 hour format): 05/17/22 21:39:15