DEV Community

spO0q
spO0q

Posted on

PHP 8.4: It's a small change, but `$date = DateTimeImmutable::createFromTimestamp(1732420815)` instead of `$date = new DateTimeImmutable(); $date->setTimestamp(1732420815);` looks pretty cool! 🀩

Top comments (8)

Collapse
 
xwero profile image
david duymelinck

I couldn't find documentation about the feature, but the method is descriptive enough.

You could do $date = (new DateTimeImmutable())->setTimestamp(1732420815); before 8.4. And now you can do $date = new DateTimeImmutable()->setTimestamp(1732420815);. Which is shorter than using the createFromTimestamp method by one character. So they didn't need to do this for the lazy typists.

Collapse
 
spo0q profile image
spO0q • Edited

The new method createFromTimestamp brings more readability and removes the hassle of creating instances just for conversions.

It also supports floats for microseconds.

Collapse
 
xwero profile image
david duymelinck • Edited

If the static method is only syntactic sugar, I wouldn't add it if I was writing the class.
The only argument I can think of to add the createFromTimestamp method is that they want to deprecate and later remove the setTimestamp method. But every method of the DateTimeImmutable class creates a new instance, so are they going to add create suffix static functions for all the methods?
For the DateTime object it makes more sense because it is mutable.

I also prefer to be explicit instead of implicit. So if the code needs to create an instance I prefer to use new instead of hiding it in a static method.
If creating an instance irks you, the date_create_immutable function is in php since 5.5. They forgot to add the date_create_immutable_from_timestamp function.

I don't see the added value of the method.

Thread Thread
 
spo0q profile image
spO0q

maybe look at what the Carbon package already does with a similar method. It's not only a syntactic sugar and simplifies the process. For example, you don't have to convert from one format to another (see U format).

Thread Thread
 
xwero profile image
david duymelinck

Why would you compare native code with a package? I just compare the new method with what already is available in php.

$timestamp = 1733251555;
$format = 'd-m-Y';

echo DateTimeImmutable::createFromTimestamp($timestamp)->format($format);
echo new DateTimeImmutable()->setTimestamp($timestamp)->format($format);
echo date_create_immutable()->setTimestamp($timestamp)->format($format);
Enter fullscreen mode Exit fullscreen mode

The three echo outputs are the same. So I don't see how the new method makes it simpler?

Thread Thread
 
spo0q profile image
spO0q

Hi, it takes microseconds into account and does not need a new instance.

Sometimes, you also need some conversion or to stringify the timestamp.

see github.com/php/php-src/pull/12413 for more details ;)

Thread Thread
 
xwero profile image
david duymelinck

Thank you for that link.

I was too focused on the syntax to see the benefit.

The question I have after this information is why didn't they fix setTimestamp to accept float while they added the other microsecond aware methods?

Thread Thread
 
spo0q profile image
spO0q

dunno, but I guess it would be risky to modify this implementation that way (e.g., backward compatibility). That could be a breaking change. The new helper makes sense.