Prev | Current Page 405 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

", date("t"), date("F"));
If this is executed in April, the following result will be output:
There are 30 days in April.
Determining the Number of Days in Any Given Month
Sometimes you might want to determine the number of days in some month other
than the present month. The date() function alone won??™t work because it requires
a timestamp, and you might only have a month and year available. However, the
mktime() function can be used in conjunction with date() to produce the desired
result. Suppose you want to determine the number of days found in February 2007:
$lastday = mktime(0, 0, 0, 3, 0, 2007);
printf("There are %d days in February 2007.", date("t",$lastday));
Executing this snippet produces the following output:
There are 28 days in February 2007.
340 CHAPTER 12 ?–  D ATE AND TIME
Calculating the Date X Days from the Present Date
It??™s often useful to determine the precise date of some specific number of days into the
future or past. Using the strtotime() function and GNU date syntax, such requests
are trivial. Suppose you want to know what the date will be 45 days into the future,
based on today??™s date of February 25, 2007:
$futuredate = strtotime("45 days");
echo date("F d, Y", $futuredate);
This returns the following:
April 12, 2007
By prepending a negative sign, you can determine the date 45 days into the past
(today being February 25, 2007):
$pastdate = strtotime("-45 days");
echo date("F d, Y", $pastdate);
This returns the following:
January 11, 2007
What about ten weeks and two days from today (February 25, 2007)?
$futuredate = strtotime("10 weeks 2 days");
echo date("F d, Y", $futuredate);
This returns the following:
May 9, 2007
CHAPTER 1 2 ?–  D ATE AND T IME 341
Taking Advantage of PEAR: Creating a Calendar
The Calendar PEAR package consists of a number of classes capable of automating
numerous chronological tasks such as the following:
??? Rendering a calendar of any scope in a format of your choice (hourly, daily,
weekly, monthly, and yearly being the most common).


Pages:
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417