Section 11.2. Date and Time Functions


11.2. Date and Time Functions

PHP uses the standard Unix-style timestamp to work with dates. This is simply the number of seconds since January 1, 1970. You get the current timestamp using the time function, shown in Example 11-15.

Example 11-15. A simple echo of the timestamp

 <?php $timestamp= time(); echo $timestamp; ?> 

This results in Figure 11-13.

Figure 11-13. A Unix timestamp


 1134511981 

This is not exactly the most meaningful representation of the date and time. So instead, you can use the date function to translate the timestamp into a meaningful string. The date function takes a timestamp and a format string, as shown in Example 11-16.

Example 11-16. Making the date and time appear like we expect

 <?php $timestamp= time(); echo date("m/d/y G.i:s",$timestamp); ?> 

This code returns the screen shown in Figure 11-14.

Figure 11-14. An easy-to-read date and time from the date function


Dates and times can be displayed in a variety of formats; these will be discussed next.

11.2.1. Display Formats

Date and times are displayed in a variety of formats. In Example 11-16, we used a date format string of m/d/y G.i:s. Table 11-2 shows other possible components for those formats.

Table 11-2. Time-formatting values

Format

Meaning

Example value

a

am or pm

Am

A

AM or PM

AM

d

Day of the month

01

D

Day or the week

Sun

F

Month name

January

h

Hours in 12-hour format with leading zeros

04

H

Hours in 24-hour format with leading zeros

16

g

Hours in 12-hour format without leading zeros

4

G

Hours in 24-hour format without leading zeros

16

i

Minutes

35

j

Day of the month

2

l

Day of the week as a name

Sunday

L

Leap year (1 for yes, 0 for no)

1

m

Month of the year abbreviated to three characters

Jul

M

Month of the year

July

N

Month of the year as number without leading zeros

7

s

Seconds of the hour

58

S

Suffix for the day

th, nd, st, rd

R

Standardized date format

Thu, 15 Dec 2005 16:49:39-0600

U

Timestamp

1134512479

y

Two-digit year

25

Y

Four-digit year

2025

z

Day of year

234

Z

GMT offset in seconds (Greenwich Mean Time)

-21600 (-6*60*60)


11.2.2. Arithmetic

Adding or subtracting days and hours can be done by adding or subtracting seconds. While this may sound odd, it's not hard. To add two days to a timestamp, add 2*24*60*60 (2 days*24 hours*60 minutes*60 seconds) to the timestamp, as shown in Example 11-17.

Example 11-17. Adding two days to the date

 <?php $timestamp= time(); echo date("m/d/y G.i:s",$timestamp); $seconds=2*24*60*60; $timestamp+=$seconds; echo "<br>new dates   is:"; echo date("m/d/y G.i:s",$timestamp); ?> 

This outputs:

 12/13/05 16.28:32 new dates is:12/15/05 16.28:32 

Let's see what else you need to create dates with validation.

11.2.3. Validating Dates

When you receive a user-supplied date, it's good practice, as with any other user-supplied data, to check that it's valid. You can use the checkdate function, shown in Example 11-18, to validate a date. It takes three parametersthe month, day, and yearfor a date to validate. If the date is valid, it returns trUE; otherwise, it returns FALSE.

Example 11-18. Validating two dates

 <?php   echo("Validating: 4/31/2005<br>");   if (checkdate(4,31,2005)) {     echo('Date accepted.');   }   else {     echo ('Invalid date.');   }   echo("<br>");   echo("Validating: 5/31/2005<br>");   if (checkdate(5,31,2005)) {     echo('Date accepted.');   }   else {     echo ('Invalid date.');   } ?> 

As you can tell by our example in Figure 11-15, the 31 April 2005 date was invalid, yet 31 May 2005 was valid. This can happen because of a typo or a user just entering wrong information.

Figure 11-15. Only some months have 31 days


Once you know that you have the valid segments of a date, you can create a timestamp.

11.2.4. Using mktime to Create a Timestamp

It's fairly easy to get the current time and date using date, but if you're trying to create a date and all you have are the components of the date such as month, day, and year, you'll need to use the mktime function. The mktime function takes these parameters:

  • Hour

  • Minute

  • Second

  • Month

  • Day of the month

  • Year

You can omit some of the parameters when calling mktime, and they'll be filled in from the current time. mktime is a timestamp, which is an integer containing the number of seconds between the Unix Epoch of January 1 1970 00:00:00 GMT and the time specified. You can't omit them out of order though. Example 11-19 checks whether the date is valid, and then creates a timestamp.

Example 11-19. Creating a timestamp from the components of a date

 <?php   echo("Validating: 5/31/2005<br>");   if (checkdate(5,31,2005)) {     echo('Date accepted: ');     $new_date=mktime(18,05,35,5,31,2005);     echo date("r",$new_date);   }   else {     echo ('Invalid date.');   }   echo("<br>"); ?> 

When run, this code produces the screen shown in Figure 11-16.

Figure 11-16. A timestamp created from its components


Now that we've covered dates and times, it's time to head onto more exciting topics. Note that the number of the year might be a two- or four-digit value, between 0 to 69 mapping to 2000 to 2069 and 70 to 100 to 1970 to 2000. On systems where time_t is a 32-bit signed integer, which is most common today, the valid range for the year is somewhere between 1901 and 2038. This limitation is fixed since the release of PHP 5.1.0.



Learning PHP and MySQL
Learning PHP and MySQL
ISBN: 0596101104
EAN: 2147483647
Year: N/A
Pages: 135

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net