Pin Me

How to Use the PHP Date Function

written by: Sam OBrien•edited by: Michele McDonough•updated: 5/20/2010

Learn the basics of the PHP date function to be able to add a dynamic date to your website. Extensive knowledge of the PHP programming language is not required to take advantage of the flexibility of PHP date functions.

  • slide 1 of 4

    The PHP date function can be very useful on a website used by registered members in different time zones who submit or pick up information or content based on a schedule. It can also simply be a nice feature for your visitors to see what time it is in the region where the people behind your site live or have headquarters. PHP date functions aren't difficult to use, however, there are a few things you'll want to keep in mind when working with them.

  • slide 2 of 4

    Which Time Should You Display Using PHP Date Functions?

    Although an extensive knowledge of the PHP:Hypertext Preprocessor (PHP) language isn't required to use it to add a dynamic date to your website, you should at least be very familiar with it or some other programming language so that you understand even the few lines of code you'll need to write.

    Often, the location of the server hosting a website is not in the same geographical region as the team of people behind it. The members of the team of developers, owner/s of the site, content producers, customer service representatives, and the web master him or herself, might live in different nations on different continents. Which time zone will you want to represent on your site? That decision is yours to make based on the type of site you have and on your preferences.

    A site behind which there is only one individual or a just a few people who all live in the same region will probably choose to use the PHP date function to dynamically generate the time zone in which they live. Whatever the decision is that you make, you'll need to properly set the default time zone and indicate to visitors what that specific zone is.

  • slide 3 of 4

    Taking a Look at PHP Date Function Code

    A simple way of setting the default time zone is to use the date_default_timezone_set() function to which you'll supply a string argument that represents the chosen zone. In the first screen shot below you can see how I've use this function to display dates and times for four different regions all on the same page. If you click on the image, you'll see the examples I've used: America/New_York, Europe/London, Asia/Jerusalem, Africa/Cairo. You can't simply make up a string to represent the area you want. For example, you couldn't just enter "America/" plus whatever the name of your town or city is; the arguments must be valid. An exhaustive list of valid PHP time zones is available at the official PHP website.

    You can also see, if you click on the second screen shot, the different options available for formatting when working with PHP date functions. I included only some of the more popular options along with a brief explanation of how their use affects format which I show in parenthesis. None of this section is part of the actual code because it is commented and was included solely for the purpose of this article. You would probably not want to take up space in your PHP files that are to go live by including this list. Also, note that just because I used PHP to generate some of the hypertext mark-up language (HTML) such as the inclusion of the image and the page title, you don't have to do the same. You should, however, know how to use PHP to generate HTML to get the exact display you desire.

    You can see in the third screen shot just how flexible you can be with the PHP date function. If you take a look at the parsed code, you'll notice the differences in how the date and the time appear for each region. Yes, Jerusalem and Cairo are supposed to have the same time. PHP date functions allow you to decide whether to use a one or two digit day of the month, an abbreviated or spelled-out month, a 12 or 24-hour format for time, a 2 or 4-digit year, etc. Another nice feature of the PHP date function is that it allows you to accurately display the date and time for more than one region on a single page. This might be very useful for a site that receives many international visitors.

  • slide 4 of 4

    PHP Date Function - Screenshots