Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timestamp for different timezones in php

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
like image 899
Pradeep shyam Avatar asked Oct 15 '25 16:10

Pradeep shyam


1 Answers

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  
like image 184
Sithu Avatar answered Oct 17 '25 06:10

Sithu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!