Normally we can get local date and time by using php date function but today we will learn to get present time of different time zone. Here is the code
$date = new DateTime('2014-06-1', new DateTimeZone('America/Mexico_City'));
$zone = $date->format('P');
$query = "SELECT CONVERT_TZ(NOW(), @@session.time_zone, '$zone') as time;";
$data = $this->db->query($query)->row;
$time = date("Y-m-d H:i:s", strtotime($data['time']));
If we print the $date then it gives DateTime object
DateTime Object
(
[date] => 2014-06-01 00:00:00.000000
[timezone_type] => 3
[timezone] => America/Mexico_City
)
If we print the $zone then it gives
-05:00 If we print the $data then it gives
Array
(
[time] => 2016-02-17 13:44:42
)
If we print the $time then it gives
2016-02-17 13:52:59

Be the first to comment.