How to return the Timezone timestamp set?

Asked

Viewed 132 times

1

I’m trying to return the timestamp of my city.

When I use the instruction ('Y-m-d H:i:s') it returns the right time of my region but I need to return the timestamp of that hour and it seems that with the instruction ('U') I only get the server time and not the timestamp of the hour of my region.

function agora (){
    date_default_timezone_set("America/Campo_Grande");
    $dftz011 = date_default_timezone_get();
    $dtms011 = new DateTime();
    return $dtms011->format('U'); 
}

I’m using this tool to convert the timestamp returned and when I do the conversion always return me the time of the server and not the Timezone of my region.

How to return the timestamp of my region?

  • Even echo $dtms011->format('H:i:s'); does not return the right time?

  • This returns the right time. With ('U') does not return.

  • 1

    you want a timestamp with Timezone this?

  • Exactly @rray

  • What is the purpose, I found interesting.

  • Variable names are funny

Show 1 more comment

1 answer

5


To get the timestamp converted with Timezone, add timestamp with offset (difference in greenwitch realation in seconds). Use methods getTimestamp() and getOffset() of the Datetime class.

<?php
$data = new DateTime('now', new DateTimeZone("America/Sao_Paulo"));
echo 'TimeStamp: '. ($data->getTimestamp() + $data->getOffset());

Output: 1444269422, corresponds to: Thu, 08 Oct 2015 01:57:02 GMT

Example - 3v4l

Example with funny message - ideone

References

epochconverter

How to get Unix timestamp in php based on Timezone

Adjusting Timestamps for Time Zones

Browser other questions tagged

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