What is the most recommended and performative way to manipulate dates in PHP?

0

In PHP we can manipulate dates using functions such as date , strtotime , mktime , and so on. and from version 5.3 of it we can manipulate dates in an object-oriented way with the DateTime , DateInterval , DatePeriod classes, and so on. >

Using functions:

<?php

date_default_timezone_set('America/Sao_Paulo');
echo date('d-m-Y H:i:s');

Using classes:

<?php

$timeZone = new DateTimeZone('America/Sao_Paulo');

$currentDateTime = new DateTime('now', $timeZone);
echo $currentDateTime->format('d-m-Y H:i:s');

What would be the most advisable and performative way to manipulate dates in PHP, using it in an object-oriented or structured way?

    
asked by anonymous 01.04.2017 / 16:52

1 answer

3

The best way, depends on what your application requires, because both the data , strtime and the DateTime functions are qualified. If it is oriented then it uses classes, if not, it uses the same structured form.

So much so that the people speak of DRY , and in this case would not imagine creating a class for hours and the date, typing new times and times, when the website itself would be something simple, and I would probably only use it to print the year in the footer (single on the whole site) and when to insert dates into the database.

  

?? : In the php helper classes, they are usually static because they are convenient,

    
01.04.2017 / 17:07