PHP returning wrong current time

Asked

Viewed 4,894 times

5

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 verify, I have the following result:

14:45:21

But now it’s 9:45.

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

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

  • 1

    This is probably the time zone configuration: https://answall.com/q/245778/112052

4 answers

7


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

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

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

date.timezone = America/Sao_Paulo

4

You’re taking the server time.

To fix just use the date_default_timezone_set php.

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');

3

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

0

Common error: change php.ini code from 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 must change in line 1969:

[Date]
date.timezone=America/Sao_Paulo //linha 1969
  • 1

    That one php.ini is with the directive date.timezone duplicated and the last assignment is that it takes precedence.

Browser other questions tagged

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