For further actions, you may consider blocking this person and/or reporting abuse
Read next
WebAssembly + JavaScript: Building a Real-Time Image Processing Tool
Vladislav V. -
Finally got some time to play with the new JSONata and Variables support for Step Functions, and I have to say, it is massive improvement. Check out my latest blog post, where I walk through a simple example of how easy it is to handle pagination now
Ian -
Getting Started with Golang: A Beginnerβs Guide
Mukul Saini -
React 19: New hook useActionState
Jaime -
Top comments (8)
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.The new method
createFromTimestamp
brings more readability and removes the hassle of creating instances just for conversions.It also supports floats for microseconds.
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 thesetTimestamp
method. But every method of theDateTimeImmutable
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 thedate_create_immutable_from_timestamp
function.I don't see the added value of the method.
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).Why would you compare native code with a package? I just compare the new method with what already is available in php.
The three echo outputs are the same. So I don't see how the new method makes it simpler?
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 ;)
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?
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.