Nov 30, 2014

Convert Date Time to GMT/UTC

If you want to convert any date time to GMT/UTC for given timezone (observing DST/not).

/**
     * coverts the given date time to GMT/UTC based on timezone provided
     * @param string $date
     * @param string $timezone
     * @return string
     */
    function getGMT($date, $timezone){
        date_default_timezone_set("UTC");
        $daylight_savings_offset_in_seconds = timezone_offset_get( timezone_open( $timezone ), new DateTime() );
        return $new_date = date('Y-m-d H:i:s', strtotime('-'.$daylight_savings_offset_in_seconds.' seconds', strtotime($date)));
    }



$date = "2014-11-30 23:50:00"; //yyyy-mm-dd
//$date = "11/30/2014 23:50:00"; //mm/dd/yyyy
//$date = "30-11-2014 23:50:00"; //dd-mm-yyyy
$timezone = "Asia/Kolkata";
//$timezone = "Australia/Adelaide";
//$timezone = "America/New_York";

echo getGMT($date, $timezone);

Find Random Coordinates in the proximity

If you are asked to find the random coordinates (latitude & longitude) considering that your latitude & longitude as the center and with in the given proximity (radius), then this service will help to get one such coordinate. An example, you are at X location (17.414472, 78.449024) and radius 1 mile.



 /**
     * picks the random latitude & longitude with respect to given within the provided radius
     *
     * @param array $centre    (lat, lng)
     * @param number $radius           
     * @return array:
     */
    function getCoordinates($centre, $radius)
    {
        $radius_earth = 3959; // miles
                             
        // Pick random distance within $distance;
        $distance = lcg_value() * $radius;
       
        // Convert degrees to radians.
        $centre_rads = array_map('deg2rad', $centre);
       
        // First suppose our point is the north pole.
        // Find a random point $distance miles away
        $lat_rads = (pi() / 2) - $distance / $radius_earth;
        $lng_rads = lcg_value() * 2 * pi();
       
        // ($lat_rads,$lng_rads) is a point on the circle which is
        // $distance miles from the north pole. Convert to Cartesian
        $x1 = cos($lat_rads) * sin($lng_rads);
        $y1 = cos($lat_rads) * cos($lng_rads);
        $z1 = sin($lat_rads);
       
        // Rotate that sphere so that the north pole is now at $centre.
       
        // Rotate in x axis by $rot = (pi()/2) - $centre_rads[0];
        $rot = (pi() / 2) - $centre_rads[0];
        $x2 = $x1;
        $y2 = $y1 * cos($rot) + $z1 * sin($rot);
        $z2 = - $y1 * sin($rot) + $z1 * cos($rot);
       
        // Rotate in z axis by $rot = $centre_rads[1]
        $rot = $centre_rads[1];
        $x3 = $x2 * cos($rot) + $y2 * sin($rot);
        $y3 = - $x2 * sin($rot) + $y2 * cos($rot);
        $z3 = $z2;
       
        // Finally convert this point to polar co-ords
        $lng_rads = atan2($x3, $y3);
        $lat_rads = asin($z3);
       
        return array_map('rad2deg', array(
            $lat_rads,
            $lng_rads
        ));
    }


    $my_location = array(17.414472, 78.449024);
    $radius = 1;
    print_r(getCoordinates($my_location, $radius));