Reputation: 411
What are the advantages and disadvantages of each method? In which case should I create a new object and when should I use the static call? Is DateTime an exception?
In this case the method call for format()
seems to work for both.
Both the $dateObj
and the $dateStatic
work similarly in this case:
<?php
$raw = '22. 11. 1968';
$dateObj1 = new DateTime ();
$dateObj2= $dateObj1->createFromFormat('d. m. Y', $raw);
echo 'Start date: ' . $dateObj2->format('Y-m-d') . "\n";
$dateStatic = DateTime::createFromFormat('d. m. Y', $raw);
echo 'Start date: ' . $dateStatic->format('Y-m-d') . "\n";
?>
Upvotes: 0
Views: 762
Reputation: 1619
The static method is preferable. It will have slightly better performance. Your first method is creating a new DateTime object and then immediately replacing it with a new DateTime object. Serves no purpose.
Upvotes: 1