PHP returning wrong current time

I'm having a problem using the function date using the parameters to return the current time.

When I use the function:

$hora = date('H:i:s');

When I give a echo in the variable $hora to check, I have the following result:

14:45:21

But now it's 09: 45.

Is there any PHP or Xampp configuration that might be changing the schedule?

The date is returning correctly, but the time is not.

Author: hkotsubo, 2019-04-30

4 answers

In the php file.ini you have to change the timezone.

If you use Xampp you find the file in xampp \ php\php.ini

Just change the line below or add it there, after that restart XAMPP apache and try again.

date.timezone = America/Sao_Paulo
 7
Author: Kevin Faccin, 2019-04-30 12:52:28

Is picking up the server time.

To fix just use PHP's date_default_timezone_set.

For Brasilia Time use: 'America/Sao_Paulo'. Other time zones you can find here .

The code will look like this:

date_default_timezone_set('America/Sao_Paulo');
$hora = date('H:i:s');
 4
Author: Artur Brasil, 2019-04-30 12:56:26

Use date_default_timezone_set('America/Sao_Paulo'); to configure your

 3
Author: Leonardo Barros, 2019-04-30 12:52:18

Common error: change php code.ini of line 975 (more or less):

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = America/Sao_Paulo // linha 975 

But in fact you should change in line 1969:

[Date]
date.timezone=America/Sao_Paulo //linha 1969
 0
Author: Reginaldo de Oliveira de Mello, 2021-01-02 17:23:39