How to Display Date and Time in PHP
Create an activity in PHP that will display the current date and time. This activity is very common for those who are learning the basics of PHP. The current date and time can be displayed in PHP using the date() function. This function takes a format string as an argument and returns a string formatted according to the specified format. The format string can be any combination of characters, including literal characters, escape sequences, and format specifiers. Literal characters are simply copied to the output string, while escape sequences are used to indicate special processing.
The following are the requirements for this tutorial:
- Text editor (VS Code, Sublime, Brackets), download and install a text editor of your choice
By the end of this tutorial, you will be able to:
- Create a PHP script that display the current date and time based on server’s default time zone.
- Understand the different functions used in order to create PHP script.
- To integrate and apply the source code in your projects
Source code explanation
Line 19 – date_default_timezone_set(timezoneId),function sets the default timezone used by all date/time functions in the script.
It has a parameter, the timezone identifier, like UTC, Africa/Lagos, Asia/Hong_Kong, or Europe/Lisbon. The list of valid identifiers is available in the official documentation of PHP PHP: date_default_timezone_set – Manual.
Note: please set the time zone first before using the date function.
Line 20 – date() function formats a local date and time, and returns the formatted date string.
- y – A two digit representation of a year
- m – A numeric representation of a month (from 01 to 12)
- d – The day of the month (from 01 to 31)
Line 21 – we will use the date() function to format it and display the current time
- h – 12-hour format of an hour (01 to 12)
- i – Minutes with leading zeros (00 to 59)
- s – Seconds, with leading zeros (00 to 59)
- a – Lowercase am or pm
Complete Source code
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no"> <title>Display Date and Time in PHP</title> <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css"> </head> <body> <div class="row"> <div class="col-md-8 offset-md-2"> <div class="card"> <div class="card-header bg"> <h1>Display Date and Time in PHP</h1> </div> <div class="card-body"> <?php date_default_timezone_set('Asia/Manila'); ?> <h4>Current Date: <?php echo date('y-m-d'); ?></h4> <h4>Current Time: <?php echo date('h:i:s a'); ?></h4> </div> </div> </div> </div> <script src="assets/js/jquery.min.js"></script> <script src="assets/bootstrap/js/bootstrap.min.js"></script> </body> </html>
This simple tutorial shows us on how to display the time and date based on the specific time zone. You may download the source code and check our YouTube Channel for more PHP tutorials and projects.
We hope you found this tutorial to be helpful! Wishing you the best of luck with your projects! Happy Coding!
You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.
Hire our team to do the project.
Related Topics and Articles: