Prev | Current Page 394 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

date("l");
// Today is Wednesday
Let??™s try a more verbose presentation of the present date:
$weekday = date("l");
$daynumber = date("dS");
$monthyear = date("F Y");
printf("Today is %s the %s day of %s", $weekday, $daynumber, $monthyear);
This returns the following:
Today is Wednesday the 22nd day of August 2007
You might be tempted to insert the nonparameter-related strings directly into the
date() function, like this:
echo date("Today is l the ds day of F Y");
Indeed, this does work in some cases; however, the results can be quite unpredictable.
For instance, executing the preceding code produces the following:
EST200724pm07 3842 Saturday 2803America/New_York 2442 24pm07 2007f February 2007
However, because punctuation doesn??™t conflict with any of the parameters, feel
free to insert it as necessary. For example, to format a date as mm-dd-yyyy, use the
following:
echo date("m-d-Y");
// 04-26-2007
CHAPTER 1 2 ?–  D ATE AND T IME 329
Working with Time
The date() function can also produce time-related values. Let??™s run through a few
examples, starting with simply outputting the present time:
echo "The time is ".date("h:i:s");
// The time is 07:44:53
But is it morning or evening? Just add the a parameter:
echo "The time is ".date("h:i:sa");
// The time is 07:44:53pm
Learning More About the Current Time
The gettimeofday() function returns an associative array consisting of elements
regarding the current time.


Pages:
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406