multiple timezone php

0

Well, I have a system where I need to use a timestamp in UTC format, and I use a system to show when the profile was visited on the site, it updates with a timestamp, however I have a problem, on the site when updating it, he says that the profile was updated to 3h more, I need to use UTC because it takes this timezone to receive information from an external api, how do I make this timestamp normal, and work correctly on the site showing that it was updated on time? >     

asked by anonymous 13.10.2017 / 14:42

2 answers

2

Carlos if I understood your question is this answer that will meet your need, first of all it would be ideal if you read the documentation and understand the operation of timezone and datetime of php .

In your system you should receive the variable and arrow-there with the desired timezone, in your case São Paulo.

new DateTimeZone('America/Sao_Paulo');

Receive the variable by parameter and set it with this timezone

$date = new DateTime('2017-10-14 10:04:59 GMT'); //entre os ( ) irá sua variável
$date->setTimezone($tz);

ready your room has been set with the desired timezone , and at that time you can use it to save in the bank, but it is important to read the documentation because at this time you implement it in different ways, I made two form just to exemplify your case, but in the documentation you will understand the various ways to do it.

  

echo $date->format('Y-m-d g:i:s A');

Result: 2017-10-14 7:04:59 AM

  

echo $date->format('l F j Y g:i:s A');

Result: Saturday October 14 2017 7:04:59 AM

  

The complete code looks like this:

$tz = new DateTimeZone('America/Sao_Paulo');
$date = new DateTime('2017-10-14 10:04:59 GMT'); // aqui dentro dos parenteses você deve coloca a sua váriavel DateTime($variavel);
$date->setTimezone($tz); //seta o timezone da váriavel
echo $date->format('Y-m-d g:i:s A'); //imprime o horário no padrão do timezone
echo '<br>';
echo $date->format('l F j Y g:i:s A');

And you can test on this LINK

    
14.10.2017 / 15:30
0

You can change on the server, or this way in the php itself:

<?php
date_default_timezone_set('America/Sao_Paulo');
echo date('d/m/Y H:i:s');
?>

If you need other regions: link

If you have access to the files on the server:

link

SetEnv TZ America / Sao_Paulo

php.ini

date.timezone="America / Sao_Paulo"

my.ini

default-time-zone="America / Sao_Paulo"

    
13.10.2017 / 14:45