Reputation: 1292
In the below code i need to get unixtimestamp for 2 Country timezone. The output of the code will give me a date with difference but the timestamp is not differ each other. It remains same. Can anyone possible to give a solution to get different timestamp for different timezones? Thanks in advance.
date_default_timezone_set('Asia/Calcutta');
echo date("Y-m-d H:i:s")."<br/>"; //2012-12-18 12:12:12
echo strtotime(date("Y-m-d H:i:s",time()))."<br/>"; //1355812934
date_default_timezone_set('Europe/London');
echo date("Y-m-d H:i:s")."<br/>"; //2012-12-18 06:12:12
echo strtotime(date("Y-m-d H:i:s",time()))."<br/>"; //1355812934
Upvotes: 4
Views: 4410
Reputation: 5050
This should work, I changed your original way to use php DataTimeZone
class. Give this a try, it should be easy to follow:
$dateTimeZoneCalcutta = new DateTimeZone("Asia/Calcutta");
$dateTimeCalcutta = new DateTime("now", $dateTimeZoneCalcutta);
$calcuttaOffset = $dateTimeZoneCalcutta->getOffset($dateTimeCalcutta);
$calcuttaDateTime = date("Y-m-d H:i:s", time() + $calcuttaOffset);
echo 'Local Server Time: ' . date("Y-m-d H:i:s", time()) . '<br />';
echo 'Calcutta Time: ' . $calcuttaDateTime . '<br />';
echo 'Calcutta Timestamp: ' . strtotime($calcuttaDateTime) . '<br />';
echo '<br /><br />';
$dateTimeZoneLondon = new DateTimeZone("Europe/London");
$dateTimeLondon = new DateTime("now", $dateTimeZoneLondon);
$londonOffset = $dateTimeZoneLondon->getOffset($dateTimeLondon);
$londonDateTime = date("Y-m-d H:i:s", time() + $londonOffset);
echo 'Local Server Time: ' . date("Y-m-d H:i:s", time()) . '<br />';
echo 'London Time: ' . $londonDateTime . '<br />';
echo 'London Timestamp: ' . strtotime($londonDateTime) . '<br />';
Upvotes: 3
Reputation: 4862
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
Upvotes: 4