Skip to content

Instantly share code, notes, and snippets.

@Sivamanim
Created September 23, 2017 06:55
Show Gist options
  • Save Sivamanim/0ea7bd73ea5469688609d96ef0e83edd to your computer and use it in GitHub Desktop.
Save Sivamanim/0ea7bd73ea5469688609d96ef0e83edd to your computer and use it in GitHub Desktop.
Time different between two time zone
<?php
$db_time = '2017-09-21 12:00:00 PM';
$db_timezone = 'Asia/Kolkata';
$user_timezone = 'America/New_York';
date_default_timezone_set($db_timezone); //Change default timezone to old timezone within this function only.
$originalTime = new DateTime($db_time);
$originalTime->setTimeZone(new DateTimeZone($user_timezone)); //Convert to desired TimeZone.
date_default_timezone_set($user_timezone) ; //Reset default TimeZone according to your global settings.
$LATime = $originalTime->format('Y-m-d h:i:s A'); //Return converted TimeZone.
$datetime1 = new DateTime();
$datetime2 = new DateTime($LATime);
$interval = $datetime1->diff($datetime2);
$year = $interval->format('%y years');
$month = $interval->format('%m months');
$day = $interval->format('%a days');
$hour = $interval->format('%h hours');
$minute = $interval->format('%i minutes');
$seconds = $interval->format('%s seconds');
$time_different = '';
if($year > 0){
$time_different .= $year. ' ';
} if($month > 0){
$time_different .= $month. ' ';
} if($day > 0){
$time_different .= $day. ' ';
} if($hour > 0){
$time_different .= $hour. ' ';
} if($minute > 0){
$time_different .= $minute. ' ';
}else{
$time_different .= $seconds. ' ';
}
echo $time_different. ' ago';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment