Prev | Current Page 411 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

1+ Users
Enhanced support for PHP??™s date and time support was added in PHP 5.1. Not only
was an object-oriented interface added, but so was the ability to manage your dates
and times in respect to various time zones. This section touches solely upon the
object-oriented interface.
Introducing the DateTime Constructor
Before you can use the Date features, you need to instantiate a date object via its class
constructor. This constructor??™s prototype follows:
object DateTime([string $time [, DateTimeZone $timezone]])
The Date() method is the class constructor. You can set the date either at the time
of instantiation or later by using a variety of mutators (setters). To create an empty
date object (which will set the object to the current date), just call DateTime() like so:
$date = new DateTime();
To create an object and set the date to February 27, 2007, execute the following:
346 CHAPTER 12 ?–  D ATE AND TIME
$date = new Date("27 February 2007");
You can set the time as well, for instance to 9:55 p.m., like so:
$date = new Date("27 February 2007 21:55");
Or you can just set the time like so:
$date = new Date("21:55");
In fact, you can use any of the formats supported by PHP??™s strtotime() function,
introduced earlier in this chapter. Refer to the PHP manual for additional examples of
supported formats.
The optional $timezone parameter refers to one of PHP??™s supported time zone
settings.


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