377 Next Next We’ll move on to Chapter 18, “Managing the Date and Time,” and look at PHP’s libraries of date and calendar functions.You’ll see how to convert from user-entered for-mats t
Trang 1377 Next
Next
We’ll move on to Chapter 18, “Managing the Date and Time,” and look at PHP’s libraries of date and calendar functions.You’ll see how to convert from user-entered for-mats to PHP forfor-mats to MySQL forfor-mats, and back again
Trang 318 Managing the Date and Time
IN THIS CHAPTER,WE’LL DISCUSS CHECKINGand formatting the date and time and con-verting between date formats.This is especially important when concon-verting between MySQL and PHP date formats, Unix and PHP date formats, and dates entered by the user in an HTML form
We’ll cover
n Getting the date and time in PHP
n Converting between PHP and MySQL date formats
n Calculating dates
n Using the calendar functions
Getting the Date and Time from PHP
Way back in Chapter 1, “PHP Crash Course,” we talked about using the date() func-tion to get and format the date and time from PHP.We’ll talk about it and some of PHP’s other date and time functions in a little more detail now
Using the date() Function
As you might recall, the date()function takes two parameters, one of them optional The first one is a format string, and the second, optional one is a UNIX time stamp If you don’t specify a time stamp, then date()will default to the current date and time It returns a formatted string representing the appropriate date
A typical call to the date function could be
echo date('jS F Y');
This will produce a date of the format “27thAugust 2000”
The format codes accepted by date are listed in Table 18.1
Trang 4a Morning or afternoon, represented as two lowercase characters, either “am” or “pm”.
A Morning or afternoon, represented as two uppercase characters, either “AM” or “PM”.
B Swatch Internet time, a universal time scheme More information is available at
http://www.swatch.com/
d Day of the month as a 2-digit number with a leading zero Range is from “01” to
“31”.
D Day of the week in 3-character abbreviated text format Range is from “Mon” to
“Sun”.
F Month of the year in full text format Range is from “January” to “December”.
g Hour of the day in 12-hour format without leading zeroes Range is from “1” to “12”.
G Hour of the day in 24-hour format without leading zeroes Range is from “0” to “23”.
h Hour of the day in 12-hour format with leading zeroes Range is from “01” to “12”.
H Hour of the day in 24-hour format with leading zeroes Range is from “00” to “23”.
i Minutes past the hour with leading zeroes Range is from “00” to “59”.
I Daylight savings time, represented as a Boolean value.This will return “1” if the date is
in daylight savings and “0” if it is not.
j Day of the month as a number without leading zeroes Range is from “1” to “31”.
l Day of the week in full text format Range is from “Monday” to “Sunday”.
L Leap year, represented as a Boolean value.This will return “1” if the date is in a leap year and “0” if it is not.
m Month of the year as a 2-digit number with leading zeroes Range is from “01” to
“12”.
M Month of the year in 3-character abbreviated text format Range is from “Jan” to
“Dec”.
n Month of the year as a number without leading zeroes Range is from “1” to “12”.
O Difference between the current timezone and Greenwich Mean Time in hours e.g +1600.
r RFC822 formatted date and time, for example Wed, 9 Oct 2002 18:45:30 +1600 (Added in PHP 4.0.4.)
s Seconds past the minute with leading zeroes Range is from “00” to “59”.
S Ordinal suffix for dates in 2-character format.This can be “st”, “nd”, “rd”, or “th”, depending on the number it is after.
t Total number of days in the date’s month Range is from “28” to “31”.
T Timezone setting of the server in 3-character format, for example, “EST”.
U Total number of seconds from 1 January 1970 to this time; a.k.a., a UNIX time stamp for this date.
w Day of the week as a single digit Range is from “0” (Sunday) to “6” (Saturday).
W Week number in the year, ISO-8601 compliant (Added at PHP 4.1.0.)
Trang 5381 Getting the Date and Time from PHP
Z Offset for the current timezone in seconds Range is “-43200” to “43200”.
Dealing with Unix Timestamps
The second parameter to the date()function is a Unix time stamp
In case you are wondering exactly what this means, most Unix systems store the cur-rent time and date as a 32-bit integer containing the number of seconds since midnight, January 1, 1970, GMT, also known as the Unix Epoch.This can seem a bit esoteric if you are not familiar with it, but it’s a standard
Unix timestamps are a compact way of storing a date and time, but it is worth noting that they do not suffer from the year 2000 (Y2K) problem that affects some other com-pact or abbreviated date formats If your software is still in use in 2038, there will be similar problems though As timestamps do not have a fixed size, but are tied to the size
of a C long, which is at least 32 bits, the most likely solution is that by 2038, your com-piler will use a larger type
Even if you are running PHP on a Windows server, this is still the format that is used
by date()and a number of other PHP functions
If you want to convert a date and time to a Unix time stamp, you can use the
mktime()function.This has the following prototype:
int mktime (int hour, int minute, int second, int month,
int day, int year [, int is_dst])
The parameters are fairly self-explanatory, with the exception of the last one,is_dst, which represents whether the date was in daylight savings time or not.You can set this to
1if it was,0if it wasn’t, or -1(the default value) if you don’t know.This is optional so you will rarely use it anyway
The main trap to avoid with this function is that the parameters are in a fairly unin-tuitive order.The ordering doesn’t lend itself to leaving out the time If you are not wor-ried about the time, you can pass in 0s to the hour,minute, and secondparameters.You can, however, leave out values from the right side of the parameter list If you leave the parameters blank, they will be set to the current values Hence a call such as
$timestamp = mktime();
will return the Unix timestamp for the current date and time.You could, of course, also get this by calling
Table 18.1 Continued Code Description