Does the DateTime class of PHP support date creation in the Brazilian format?

2

In the following code echo will be printing +273 days, but would not it be +9 days ?

$datetime1 = new DateTime('01/04/2018');
$datetime2 = new DateTime('10/04/2018');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

I'm not understanding the reason, could you help me? Thank you in advance

    
asked by anonymous 06.04.2018 / 20:57

2 answers

6

YES, it supports.

To format in DD MM AAAA you can use - , . and \t , but not / :

$datetime1 = new DateTime('01-04-2018'); // poderia ser 01.04.2018 também
$datetime2 = new DateTime('10-04-2018');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

See working at IDEONE

More details in the documentation (see "Localized Notations > Day, Month & Year"):

  

link

    
06.04.2018 / 21:14
2

You can format the time in several ways by manipulating this DateTime object. The documentation of this method of php brings numerous ways of doing it.

But as the question refers to the Brazilian format here it goes:

$datetime2 = new DateTime('2011-01-01');
var_dump($datetime2->format('d/m/y'));

Output: string(8) "01/01/11"

Another example:

$datetime1 = new DateTime('2011-01-02');
var_dump($datetime1->format('d*m*y'));

Output: string(8) "02*01*11"

Make sure the time entered is in the pattern: ('ano-mes-dia') . If not strange things will happen .

Now with the data in perfect order apply your methods:

$interval = $datetime1->diff($datetime2);

echo $interval->format('%R%a days');

Output: 1

    
06.04.2018 / 21:08