The PHP Date Function: Add a Dynamic Date to Your Website

Posted by

Adding the time and date to a website using the PHP date function is not only a way of providing your visitors with the time in their area, but also in other regions of the world. Many websites have international visitors from every part of the Earth who interact with one another through chat rooms, instant messaging, and email systems provided by the website. Sometimes it’s nice to know what time it is in another part of the world when surfing the world wide web, and it’s necessary when making arrangements to meet up with someone in cyberspace or by phone if they don’t live in your region.

You don’t need to have mastered PHP or any other programming language to make use of PHP date functions for pretty basic web development. However, you should have some familiarity with server-side scripting, extensible hypertext mark-up language (XHTML), and know how to generate XHTML using PHP. If you’re comfortable with these tasks, you’ll do just fine in adding a date to your site using the PHP date function.

Set the Correct Default Date Before You Begin
If you skip this step, you could end up indicating an incorrect date and time to your visitors. If you’re not running PHP on your own machine, but rather using the installation on a remote server, you’ll probably want to set the timezone for where you and the people who work with you on the site are. For example, if you live in Chicago, United States, you could use the PHP date_default_timezone_set(); function to which you would pass the string: America/Chicago. Be sure to enclose the string within quote marks (“America/Chicago”) or your code won’t work. I used the PHP date function to generate the dates and times for three regions for this article: Buenos Aires, Casablanca, Morocco, and the Tahitian Islands.

You can see in the screen shots included just how the code looks that is behind the visual display of these dates and times. I supplied the needed option (string) for each zone. For example, date_default_timezone_set(America/Buenos_Aires”); sets a default time zone for the region in which the South American city of Buenos Aires is located. That default had to be changed for each subsequent region, but this allows you to display multiple international time zones on your site.

PHP Date Function: Learn How to Format Display
There’s more than one way to display dates and times. Days and months can be written out or they can be abbreviated. They can be represented by one or two digits and time can be based on the twelve or twenty-four hour format. The letters “am” can be in lowercase or capitalized, seconds can appear or be left out altogether. Fortunately, the PHP date function allows such flexibility if you make use of the options available. You might want to consult the official online manual for all of your options as well as a list of valid time zone strings. However, I’ve included some that you might want to use. These options are used when printing the date. For example: echo ”

Today is ” . date(‘F j, Y’) . ”
Time: ” . date(‘g:i A’) . “”;.

Below is a list of some of the most widely used options for formatting:

1. j – displays day of the month as one or two digits
2. g – time in 12-hour format, one or two digits
3. i – displays the minutes
4. Y – displays the year as four digits
5. M – abbreviates the month
6. F – full name of month
7. A – displays the letters for morning and evening in lowercase (am/pm)
8. a – displays the letters for morning and evening in capitals (AM/PM)