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

Asked

Viewed 68 times

0

In PHP we can manipulate dates using functions such as date, strtotime, mktime etc. and from version 5.3 of it we can manipulate dates object-oriented with the classes Datetime, Dateinterval, Dateperiod and etc.

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 recommendable and performative way to manipulate dates in PHP, using them in an object-oriented or structured way?

1 answer

3

The best way, depends on what your application requires, because both the functions data, strtime as the class DateTime 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 that case would not imagine creating a class for hours and the date, keep typing new times and times, when the website itself would be something simple, and would probably only make use of it, to print the year in the footer (unique throughout the site) and when to enter bank dates.

??: In the php auxiliary classes are normally static because they are convenient, not ?.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.