|
There are times when using your own date format is beneficial, but in most cases a timestamp is the best choice. Let's look at how PHP interacts with the timestamp date format. Formatting DatesIn Lesson 1, "Getting to Know PHP," you used the date function to display the current date by passing a format string as the argument, such as in the following example: echo date("j F Y H:i:s"); The date displayed looks something like this: 12 November 2004 10:23:55 The optional second argument to date is a timestamp value of the date that you want to display. For example, to display the date when a timestamp first requires a 10-digit number, you could use the following: echo date("j F Y H:I:s", 1000000000); The list of format codes for the date function is shown in Table 9.1.
Creating TimestampsDon't worry; you don't have to count from January 1, 1970, each time you want to calculate a timestamp. The PHP function mktime returns a timestamp based on given date and time values. The arguments, in order, are the hour, minute, second, month, day, and year. The following example would assign $timestamp the timestamp value for 8 a.m. on December 25, 2001: $timestamp = mktime(8, 0, 0, 12, 25, 2001); The Unix timestamp format counts from January 1, 1970, at midnight GMT. The mktime function returns a timestamp relative to the time zone in which your system operates. For instance, mktime would return a timestamp value 3,600 higher when running on a web server in Texas than on a machine in New York with the same arguments.
The mktime function is forgiving if you supply it with nonsense arguments, such as a day of the month that doesn't exist. For instance, if you try to calculate a timestamp for February 29 in a non-leap year, the value returned will actually represent March 1, as the following statement confirms: echo date("d/m/Y", mktime(12, 0, 0, 2, 29, 2003)); You can exploit this behavior as a way of performing date and time arithmetic. Consider the following example, which calculates and displays the date and time 37 hours after midday on December 30, 2001: $time = mktime(12 + 37, 0, 0, 12, 30, 2001); echo date("d/m/Y H:i:s", $time); By simply adding a constant to one of the arguments in mktime, you can shift the timestamp value returned by that amount. The date and time display as follows: 01/01/2002 01:00:00 The value returned in this example has correctly shifted the day, month, year, and hour values, taking into account the number of days in December and that December is the last month of the year. Converting Other Date Formats to TimestampsIf you have a date stored in a format like DD-MM-YYYY, it's a fairly simple process to convert this to a timestamp by breaking up the string around the hyphen character. The explode function takes a delimiter argument and a string and returns an array that contains each part of the string that was separated by the given delimiter. The following example breaks a date in this format into its components and builds a timestamp from those values: $date = "03-05-1974"; $parts = explode("/", $date); $timestamp = mktime(12, 0, 0, $parts[1], $parts[0], $parts[2]); For many date formats, there is an even easier way to create a timestampusing the function strtotime. The following examples all display the same valid timestamp from a string date value: $timestamp = strtotime("3 May 04"); $timestamp = strtotime("3rd May 2004"); $timestamp = strtotime("May 3, 2004"); $timestamp = strtotime("3-may-04"); $timestamp = strtotime("2004-05-03"); $timestamp = strtotime("05/03/2004"); Note that in the last examples, the date format given is MM/DD/YYYY, not DD/MM/YYYY. You can find the complete list of formats that are acceptable to strtotime at www.gnu.org/software/tar/manual/html_chapter/tar_7.html. Getting Information About a TimestampYou can use the date function to return part or all of the date that a timestamp represents as a formatted string, but PHP also provides the geTDate function, which returns useful values from a timestamp. Taking a single timestamp argument, geTDate returns an associative array that contains the indexes shown in Table 9.2.
The following example uses getdate to determine whether the current date falls on a weekday or weekend: $now = getdate(); switch ($now[wday]) { case 0: // Sunday case 6: // Saturday echo "It's the weekend"; break; default: echo "It's a weekday"; } Note that when getdate is called without a timestamp argument, it returns an array that contains the elements in Table 9.2 for the current time. |
|