PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » Date and Time » Date » Bug #3779

Instatiating under a timezone

Details

Request #3779Instatiating under a timezone
Submitted2005-03-11 17:55 UTC
Frommjs15451 at hotmail dot com
Assignedpajoye
StatusSuspended
PackageDate
PHP Version5.0.3
OSLinux
Roadmaps(Not assigned)

Comments

[2005-03-11 17:55 UTC] mjs15451 at hotmail dot com

Description:
------------
I found a problem where if my environment is set to a specific timezone like US/Pacific and I grabbed a datetime from the database which was set with the UTC timezone. If I were to instantiate a new date with this datetime from the db, it would instantiate automatically as US/Pacific and if I did a conversion of that date to US/Pacific, it would reduce it by 8 hours not giving me the original date. So to patch the problem, I felt it was best to have an additional optional parameter of setting the timezone when instantiating the date.

Reproduce code:
---------------
Current code does not allow setting the timezone when instantiating the date:

function Date($date = null)
{
$this->tz = Date_TimeZone::getDefault();
if (is_null($date)) {
$this->setDate(date("Y-m-d H:i:s"));
} elseif (is_a($date, 'Date')) {
$this->copy($date);
} else {
$this->setDate($date);
}
}

Expected result:
----------------
Should be able to set the timezone of the new date when creating a new date.

Here's the patch:

function Date($date = null, $tz = null)
{
if (is_null($tz)){
$this->tz = Date_TimeZone::getDefault();
}
else {
if (Date_TimeZone::isValidID($tz)) {
$this->tz = new Date_TimeZone($tz);
}
else {
$this->tz = Date_TimeZone::getDefault();
}
}
if (is_null($date)) {
$this->setDate(date("Y-m-d H:i:s"));
} elseif (is_a($date, 'Date')) {
$this->copy($date);
} else {
$this->setDate($date);
}
}

Actual result:
--------------
Always returns default environment timezone in date function.