I need to format several thousand Y-m-d H:i:s
dates from an API response (sample). I used Xdebug to profile these two approaches.
What I have
foreach ($dates as $date) {
$dt = new \DateTimeImmutable($date);
$value = $dt->format('r');
}
Calls Count Total Call Cost
php::DateTimeImmutable->__construct 55800 183 ms 1,79%
php::DateTimeImmutable->format 55800 14 ms 0,14%
What I tried
$dt = new \DateTime;
foreach ($dates as $date) {
$dt->modify($date);
$value = $dt->format('r');
}
Calls Count Total Call Cost
php::DateTime->modify 55800 130 ms 1,10%
php::DateTime->format 55800 17 ms 0,14%
php::DateTime->__construct 1 0 ms 0,00%
The latter approach looks slightly more efficient. Is it safe to rely on just modifying the datetime instead of completely redeclaring it?