Prev | Current Page 412 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

Remember that by default PHP is going to use the time as specified by your
server, which could conceivably be located anywhere on the planet. If you want the
dates and times to correspond to a set time zone, you can use this parameter. Consult
the PHP manual for more information about its time zone support.
Formatting Dates
To format the date and time for output, or easily retrieve a single component, you can
use the format() method. This method accepts the same parameters as the date()
function. For example, to output the date and time using the format 2007-02-27
09:55:00pm you would call format() like so:
echo $date->format("Y-m-d h:i:sa");
Setting the Date After Instantiation
Once the DateTime object is instantiated, you can set its date with the setDate() method.
The setDate() method sets the date object??™s day, month, and year, returning TRUE on
success, and FALSE otherwise. Its prototype follows:
Boolean setDate(integer year, integer month, integer day)
Let??™s set the date to February 27, 2007:
$date = new DateTime();
$date->setDate(2007,2,27);
echo $date->format("F j, Y");
CHAPTER 1 2 ?–  D ATE AND T IME 347
This returns the following:
February 27, 2007
Setting the Time After Instantiation
Just as you can set the date after DateTime instantiation, you can set the time using the
setTime() method. The setTime() method sets the object??™s hour, minute, and optionally
the second, returning TRUE on success and FALSE otherwise.


Pages:
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424