How to Get Unix Timestamp in PHP Based on Timezone

How to get Unix timestamp in php based on timezone

The answer provided by Volkerk (that says timestamps are meant to be always UTC based) is correct, but if you really need a workaround (to make timezone based timestamps) look at my example.

<?php

//default timezone
$date = new DateTime(null);
echo 'Default timezone: '.$date->getTimestamp().'<br />'."\r\n";

//America/New_York
$date = new DateTime(null, new DateTimeZone('America/New_York'));
echo 'America/New_York: '.$date->getTimestamp().'<br />'."\r\n";

//Europe/Amsterdam
$date = new DateTime(null, new DateTimeZone('Europe/Amsterdam'));
echo 'Europe/Amsterdam: '.$date->getTimestamp().'<br />'."\r\n";

echo 'WORK AROUND<br />'."\r\n";
// WORK AROUND
//default timezone
$date = new DateTime(null);
echo 'Default timezone: '.($date->getTimestamp() + $date->getOffset()).'<br />'."\r\n";

//America/New_York
$date = new DateTime(null, new DateTimeZone('America/New_York'));
echo 'America/New_York: '.($date->getTimestamp() + $date->getOffset()).'<br />'."\r\n";

//Europe/Amsterdam
$date = new DateTime(null, new DateTimeZone('Europe/Amsterdam'));
echo 'Europe/Amsterdam: '.($date->getTimestamp() + $date->getOffset()).'<br />'."\r\n";
?>

Get the regular timestamp and add the UTC offset

PHP get unix timestamp from string without setting default timezone

As your time str has time zone, there is no need to set the default time zone. But when you want to print it, you need to set default timezone.

php > echo date_default_timezone_get();
UTC

php > echo date("Y-m-d H:i:s",strtotime("1/1/2020 00:00:00 America/Los_Angeles"));
2020-01-01 08:00:00

php > echo date("Y-m-d H:i:s",strtotime("1/1/2020 00:00:00"));
2020-01-01 00:00:00

PHP timestamps & timezone configuration

Using time() is the same as strtotime("now") and you do not need to worry about converting the timezone of the timestamp, as the timestamp has no timezone:

Does PHP time() return a GMT/UTC Timestamp?

time returns a UNIX timestamp, which is timezone independent. Since
a UNIX timestamp denotes the seconds since 1970 UTC you could say it's
UTC, but it really has no timezone.

You can then store that timestamp in your database. When you retrieve it you can convert it to the users timezone. With something like this:

date_default_timezone_set('UTC');

$timestamp = '1429066967';
//Supported Timezones: http://php.net/manual/en/timezones.php
$userTimezone = 'America/Los_Angeles';

$dt = new DateTime();
// Set the timestamp
$dt->setTimestamp($timestamp);
// Set the timezone
$dt->setTimezone(new DateTimeZone($userTimezone));
// Format the date
$date = $dt->format('Y-m-d H:i:s');

echo $date;

Outputs: 2015-04-14 20:02:47

But if you only have the UTC offset you could try this:

date_default_timezone_set('UTC');

$timestamp = '1429066967';
$offset = -8;
$userTimezone = timezone_name_from_abbr("", $offset*3600, false);

$dt = new DateTime();
// Set the timestamp
$dt->setTimestamp($timestamp);
// Set the timezone
$dt->setTimezone(new DateTimeZone($userTimezone));
// Format the date
$date = $dt->format('Y-m-d H:i:s');

echo $date;

Which also outputs: 2015-04-14 20:02:47

get timezone offset from UNIX timestamp in PHP

Apparently you can do it this way:

<?php
$dt = new DateTime(DateTime::createFromFormat('U', '1522680410')->format('Y-m-d'), new DateTimeZone(date_default_timezone_get()));
?>

Timestamp for different timezones in php

You can get time zone offset in seconds using date("Z"). And then calculate as you need.

date_default_timezone_set('Asia/Calcutta');
echo 'Local time : '.date("r").'<br>'; // local time
echo 'Offset : '.date("Z").'<br>'; // time zone offset from UTC in seconds
echo 'UTC Time : '.date('r', strtotime(date("r")) + (date("Z")*-1)); echo '<br><br>'; // this is UTC time converted from Local time

date_default_timezone_set('Europe/London');
echo 'Local time : '.date("r").'<br>'; // local time
echo 'Offset : '.date("Z").'<br>'; // time zone offset from UTC in seconds
echo 'UTC time : '.date('r', strtotime(date("r")) + (date("Z")*-1)); echo '<br><br>'; // this is utc time converted from Local time

Output:

Local time : Tue, 18 Dec 2012 10:53:07 +0530
Offset : 19800
UTC Time : Tue, 18 Dec 2012 05:23:07 +0530

Local time : Tue, 18 Dec 2012 05:23:07 +0000
Offset : 0
UTC time : Tue, 18 Dec 2012 05:23:07 +0000

Get timezone offset in PHP given a unix timestamp

I feel silly again answering my own question, but I just found the gmstrftime function. It seems to do exactly what I want. Thanks everyone though!

PHP - get current time in specific time zone

As @Karthik said, you can grab the timezone by using the DateTimezone object.

Here's a link to the docs.

Example:

$tz = 'America/New_York';
$tz_obj = new DateTimeZone($tz);
$today = new DateTime("now", $tz_obj);
$today_formatted = $today->format('Y-m-d');
$directory = "comics";

$query = "
SELECT *
FROM comics
WHERE story = ?
AND `date` <= ?
ORDER BY `date` DESC, id DESC
LIMIT 1"

$prepped = $conn->prepare($query);
$prepped->bind_param('ss', $directory, $today_formatted);


Related Topics



Leave a reply



Submit