codefog / contao-haste

Haste is a collection of tools and classes to ease working with Contao
http://codefog.pl/extension/haste.html
MIT License
43 stars 24 forks source link

DateTime::createFromTimestamp does not work properly #80

Closed richardhj closed 8 years ago

richardhj commented 8 years ago

This is the function:

    public static function createFromTimestamp($tstamp, \DateTimeZone $timezone=null)
    {
        return static::createFromFormat('U', $tstamp, $timezone);
    }

If s.o. calls ::createFromTimestamp() without the second parameter $timezone, your function will call static::createFromFormat('U', $tstamp, null); This leads to a PHP Warning (DateTime::createFromFormat() expects parameter 3 to be DateTimeZone, null given). Even if it's just a warning, the method will return bool(false) instead of a DateTime object

aschempp commented 8 years ago

You're right, the correct solution would be to pass date_default_timezone_get() if the argument is null

richardhj commented 8 years ago

Don't passing $timezone is a solution too. Because of the @date_default_timezone_set(Config::get('timeZone')); in the initialize.php, the right timezone will be applied, wouldn't it?

aschempp commented 8 years ago

Correct. Then we would need to if/else the method call.

richardhj commented 8 years ago

I have another point belonging to this. The method returns a \DateTime instance, should it not be a Haste\DateTime\DateTime instance (to use getAge() right from this context)? This is what it could like:

$objDate = new self();
$objDate->setTimestamp($tstamp);
return $objDate;
aschempp commented 8 years ago

Sounds reasonable

qzminski commented 8 years ago

Should be fixed in 211bc90. Can you confirm?

richardhj commented 8 years ago

It works great. Thanks!

aschempp commented 8 years ago

The method has just been deprecated. You should simply use new DateTime('@' . $timestamp).