Sunday, June 26, 2011

PHP Date Function

While PHP's date() function may seem to have an overwhelming amount of options available, isn't it always better to have more choices than not enough? With PHP's date function you format timestamps, so they are more human readable.

This lesson will teach you how to display the current time, formating PHP's timestamp, and show you all the various date arguments for reference purposes.

PHP Date - The Timestamp

The date function always formats a timestamp, whether you supply one or not. What's a timestamp? Good question!

  • Timestamp: A timestamp is the number of seconds from January 1, 1970 at 00:00. Otherwise known as the Unix Timestamp, this measurement is a widely used standard that PHP has chosen to utilize.

What Time Is It?

The date function uses letters of the alphabet to represent various parts of a typical date and time format. The letters we will be using in our first example are:

  • d: The day of the month. The type of output you can expect is 01 through 31.
  • m: The current month, as a number. You can expect 01 through 12.
  • y: The current year in two digits ##. You can expect 00 through 99

We'll tell you the rest of the options later, but for now let's use those above letters to format a simple date! The letters that PHP uses to represent parts of date and time will automatically be converted by PHP.

However, other characters like a slash "/" can be inserted between the letters to add additional formatting. We have opted to use the slash in our example.

PHP Code: