Convert time and date from one time zone to another in PHP

Sometimes we have required current time according to the different country base. But the server provides the only US and another country location time zone according to hosting.

We provide you small function for your related time converter, this function provides you current time according to the different country base. If you get value database and pass date in this function, it will automatically convert country base.

   function get_current_time(){
        $CI = get_instance();
        $ip_address = getenv('HTTP_CLIENT_IP') ?:
      getenv('HTTP_X_FORWARDED_FOR') ?: getenv('HTTP_X_FORWARDED') ?:
      getenv('HTTP_FORWARDED_FOR') ?: getenv('HTTP_FORWARDED') ?:
      getenv('REMOTE_ADDR');
      // Get JSON object
      $jsondata = file_get_contents("http://timezoneapi.io/api/ip/?" .
      $ip_address);
      // Decode
      $data = json_decode($jsondata, true);
      // Request OK?
      if($data['meta']['code'] == '200'){          
        $currenttime=date('H:i:s',strtotime($data['data']['datetime']['date_time']));
      }else
      {
        $currenttime=date('H:i:s');        
      }        
        
        return $currenttime;
    } 

Please follow and like us:
error

Leave a Reply

Your email address will not be published. Required fields are marked *