Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (7.37 MB, 561 trang )
This document is created with the unregistered version of CHM2PDF Pilot
[ Team LiB ]
This document is created with the unregistered version of CHM2PDF Pilot
Using Date and Time Functions in PHP
The several sections that follow introduce you to the date- and time-related functions specifically in PHP. You learn
about the MySQL functions later in this hour. Try each listing yourself, as you march along through this hour.
Getting the Date with time()
PHP's time() function gives you all the information that you need about the current date and time. It requires no
arguments and returns an integer. For us humans, the returned number is a little hard on the eyes, but it's extremely
useful nonetheless.
print time();
// sample output: 1127732399
The integer returned by time() represents the number of seconds elapsed since midnight GMT on January 1, 1970.
This moment is known as the Unix epoch, and the number of seconds that have elapsed since then is referred to as a
timestamp. PHP offers excellent tools to convert a timestamp into a form that humans are comfortable with. Even so,
isn't a timestamp a needlessly convoluted way of storing a date? In fact, the opposite is true. From just one number,
you can extract enormous amounts of information. Even better, a timestamp can make date arithmetic much easier
than you might imagine.
Think of a homegrown date system in which you record days of the month as well as months and years. Now
imagine a script that must add one day to a given date. If this date happened to be 31 December 1999, rather than
adding 1 to the date, you'd have to write code to set the day of the month to 1, the month to January, and the year to
2000. Using a timestamp, you need only add a day's worth of seconds to your current figure and you're done. You
can convert this new figure into something more friendly at your leisure.
Converting a Timestamp with getdate()
Now that you have a timestamp to work with, you must convert it before you present it to the user. getdate()
optionally accepts a timestamp and returns an associative array containing information about the date. If you omit the
timestamp, getdate() works with the current timestamp as returned by time(). Table 11.1 lists the elements contained
in the array returned by getdate().
Table 11.1. The Associative Array Returned by getdate()
Key
Description
Example
seconds
Seconds past the minute (0–59)
28
minutes
Minutes past the hour (0–59)
7
hours
Hours of the day (0–23)
12
This document is created with the unregistered version of CHM2PDF Pilot
[ Team LiB ]
This document is created with the unregistered version of CHM2PDF Pilot
[ Team LiB ]
This document is created with the unregistered version of CHM2PDF Pilot
Using Date and Time Functions in MySQL
MySQL's built-in date-related functions can be used in SELECT statements, with or without specifying a table, to
retrieve a result of the function. Or you can use the functions with any type of date field: date, datetime, timestamp,
and year. Depending on the type of field in use, the results of the date-related functions are more or less useful.
Working with Days
The DAYOFWEEK() and WEEKDAY() functions do similar things with slightly different results. Both functions are
used to find the weekday index of a date, but the difference lies in the starting day and position.
If you use DAYOFWEEK(), the first day of the week is Sunday, at position 1, and the last day of the week is
Saturday, at position 7. For example:
mysql> select dayofweek('2001-11-13');
+-------------------------+
| DAYOFWEEK('2001-11-13') |
+-------------------------+
|
3 |
+-------------------------+
1 row in set (0.00 sec)
The result shows that November 13, 2001 was weekday index 3, or Tuesday. Using the same date with
WEEKDAY() gives you a different result with the same meaning:
mysql> select weekday('2001-11-13');
+-----------------------+
| WEEKDAY('2001-11-13') |
+-----------------------+
|
1 |
+-----------------------+
1 row in set (0.00 sec)
The result shows that November 13, 2001 was weekday index 1. Because WEEKDAY() uses Monday as the first
day of the week at position 0 and Sunday as the last day at position 6, 1 is accurate: Tuesday.
The DAYOFMONTH() and DAYOFYEAR() functions are more straightforward, with only one result and a range
that starts at 1 and ends at 31 for DAYOFMONTH() and 366 for DAYOFYEAR(). Some examples follow:
mysql> select dayofmonth('2001-11-13');
+--------------------------+
| DAYOFMONTH('2001-11-13') |
+--------------------------+
|
13|
+--------------------------+
1 row in set (0.00 sec)
mysql> select dayofyear('2001-11-13');
+-------------------------+
| DAYOFYEAR('2001-11-13') |
+-------------------------+
|
317|
+-------------------------+
This document is created with the unregistered version of CHM2PDF Pilot
[ Team LiB ]
This document is created with the unregistered version of CHM2PDF Pilot
[ Team LiB ]
Summary
In this hour, you learned how to use various PHP functions to perform date- and time-related actions. The time()
function gets a date stamp for the current date and time, and you can use getdate() to extract date information from a
timestamp and date() to convert a timestamp into a formatted string. You learned how to create a timestamp using
mktime(), and how to test a date for validity with checkdate().
Additionally, you discovered that MySQL's built-in date and time functions can definitely take some of the load off
your application by internally formatting dates and times and performing the date and time arithmetic. The formatting
options used for the DATE_FORMAT() function provide a simple method to produce a custom display string from
any sort of date field. The DATE_ADD() and DATE_SUB() functions and their numerous available interval types
help you determine dates and times in the past or future. Additionally, functions such as DAY(), WEEK(),
MONTH(), and YEAR() are useful for extracting parts of dates for use in WHERE or ORDER BY clauses.
[ Team LiB ]
This document is created with the unregistered version of CHM2PDF Pilot
[ Team LiB ]
This document is created with the unregistered version of CHM2PDF Pilot
Workshop
The workshop is designed to help you anticipate possible questions, review what you've learned, and begin putting
your knowledge into practice.
Quiz
1:
Using PHP, how do you acquire a Unix timestamp
that represents the current date and time? What about
using MySQL?
A1:
In PHP, use time(). In MySQL, use
UNIX_TIMESTAMP().
2:
Which PHP function accepts a timestamp and returns
an associative array that represents the given date?
A2:
The getdate() function returns an associative array
whose elements contain aspects of the given date.
3:
Which PHP function do you use to format date
information? What about using MySQL?
A3:
In PHP, use date(). In MySQL, use
DATE_FORMAT().
4:
Which PHP function could you use to check the
validity of a date?
A4:
You can check a date with the checkdate() function.
Activity
Create a birthday countdown script. Given form input of month, day, and year, output a message that tells the user
how many days, hours, minutes, and seconds until the big day. Use whatever combination of PHP and MySQL
This document is created with the unregistered version of CHM2PDF Pilot
[ Team LiB ]
This document is created with the unregistered version of CHM2PDF Pilot
[ Team LiB ]
Hour 12. Creating a Simple Calendar
This hour continues the date and time lesson from the previous hour, this time in the context of creating a small
calendar.
In this hour, you will learn
•
How to build a simple calendar script
•
How to build a class library to generate date pull-downs in HTML forms
[ Team LiB ]