How to convert one time zone to another time zone

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this blog i will show you how to convert one time zone to another time zone in php laravel application. We know some of application has required one time zone to convert to another time zone. In PHP provide us some ready made timezone conversion classes but it a very complected to uses. so now we have try to define this function on very simple way. We can create a simple class and need to write one function into the class. then we are able to use this function .

Example For Use

$c = new convertTimezone();

$resultTime = $c->converToTime(5.30, “2021-05-09 11:00:00”, -13);

$resultTime = $c->converToTime(-4, “2021-05-09 11:00:00”, 10);

The first one converts 5th May, 2021, 11 am from the timezone GMT+0530hrs to GMT-1300hrs

The second one coverts 5th May,2021 11 am from the timezone GMT-0400hrs to GMT1000 hrs everywhere

Code:
class convertTimezone
	{
	    function convert_timezone() // Constructor of the class
	    {
	    }
	 
	    function converToTime($conv_fr_zon=0,$conv_fr_time="",$conv_to_zon=0)
	    {
	        //echo $conv_fr_zon;
	        $cd = strtotime($conv_fr_time);
	         
	        $gmdate = date('Y-m-d H:i:s', mktime(date('H',$cd)-$conv_fr_zon,date('i',$cd),date('s',$cd),date('m',$cd),date('d',$cd),date('Y',$cd)));
	        //echo $gmdate";
	                 
	        $gm_timestamp = strtotime($gmdate);
	        $finaldate = date('Y-m-d H:i:s', mktime(date('H',$gm_timestamp )+$conv_to_zon,date('i',$gm_timestamp ),date('s',$gm_timestamp ),date('m',$gm_timestamp ),date('d',$gm_timestamp ),date('Y',$gm_timestamp )));
	         
	        return $finaldate;
	    }
	}

Read Also : How to get hours difference between two dates in Laravel?

Thanks for read. I hope it help you. For more you can follow us on facebook.

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →