multiples Timezone php

Asked

Viewed 1,081 times

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, and I update it with a timestamp, but I have a problem, on the site when I update it, it says that the profile has been updated to 3h more, i need to use UTC because it is necessary this Timezone to receive information from an external api, how do I make this timestamp normal, and function correctly on the site showing that it was updated on time?

  • Does this look like a common bug in hosting, you’re using localhost, or some paid server? the @Wictorchaves response is correct, but you need to know which php settings can be edited if you are a paid server and if this is the case they may already have a wiki to this end.

  • is not an error, I use a dedicated, I think I was unclear, I use an api to receive external information, profiles statistics, to access api I am obliged to use UTC which is the server schedule that provides access to api, my server and website on the other hand, are br, I would have to convert the result that the api sends me to the Brazilian time zone as well

  • The timestamp that you mention in the question refers to a bank table that saves the time for each new interaction using current timestamp? or is a variable php which takes the current time and saves in the database?

  • a variable that takes current value from another server.

2 answers

3

Carlos if I understood your question is this answer that will meet your need, before anything else would be ideal you give a read on documentation and understand the functioning of timezone and of datetime of php.

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

new DateTimeZone('America/Sao_Paulo');

Receive the variable per parameter and set the same with this Timezone

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

ready his variable was set with the timezone desired, and at that moment 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 several ways, I did two ways just to exemplify your case, but in the documentation you will understand the various ways of doing.

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

Upshot: 2017-10-14 7:04:59 am

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

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

The whole code goes 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

1

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

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

If you need other regions: http://br.php.net/manual/en/timezones.america.php

If you have access to the server files:

httpd.conf

Setenv TZ America/Sao_paulo

php.ini

date.Timezone = "America/Sao_paulo"

my ini.

default-time-zone = "America/Sao_paulo"

  • in fact, and I can use different Timezone and make them work synchronized, example use Sao Paulo as default, but in the api use UTC and convert to sao paulo the result when inserting in the database

Browser other questions tagged

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