<?php
class Utilities
{

    public function __construct()
    {
    }

    public static function convertUtcToLocal($datetime, $timezone, $format = 'Y-m-d H:i:s')
    {
        if (!empty($datetime) && !empty($timezone)) {
            $datetime = date($format, strtotime($datetime));
            $utc_date = DateTime::createFromFormat(
                $format,
                $datetime,
                new DateTimeZone('UTC')
            );
            if ($utc_date) {
                $utc_date->setTimeZone(new DateTimeZone($timezone));
                return $utc_date->format($format);
            }
        }
        return $datetime;
    }

    public static function getClientIpAddress()
    {
        $ip="";
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
            //ip from share internet
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            //ip pass from proxy
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }

    public static function startBenchmark(&$benchmark=[]){
        $benchmark['start'] = microtime(true) * 1000;
    }

    public static function finishBenchmark(&$benchmark){
        $benchmark['finish']  = microtime(true) * 1000;
        $benchmark['duration']  = $benchmark['finish'] - $benchmark['start'];
        error_log('benchmark'.json_encode($benchmark));
    }
}
