PHP's DateTime object and unix times..
Just started checking out PHP 5.2's DateTime object. Its great to have a 'standard object' for dates.. Lots of the frameworks out there all define their own date class, perhaps this can help creating more generic interfaces.
Its quite hard to find the documentation though. I'm looking for a way to create a DateTime object based on a unix time, but this seems to be the only way:
<?php
// unix time
$unix = time();
$dateTime = new DateTime(date(DATE_ATOM,$unix));
?>
Which is a pretty nasty hack, because the strtotime()-like parsing is unnecessary because the actual date is likely stored as a number anyway..
Update: kore pointed out the proper way to do this in the comments.
Comments
kore •
Just use:$time = new DateTime( '@123456789' );
?>
mathieu Kooiman •
Hi Evert,I wrote a little (dutch) article on the DateTime extensions on my own blog:
http://www.scriptorama.nl/tips-tricks/php-52-de-date-extensie-uitbreidingen .
I missed this little feature, so I've added it :). Thanks