Skip to main content

Posts

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"; //$timezon...

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                   ...

Random Numbers or Password or Pin in MySQL

To obtain a random integer R in the range i <= R < j, use the expression FLOOR(i + RAND() * (j – i)). For example, to obtain a random integer in the range the range 7 <= R < 12, you could use the following statement: SELECT FLOOR(7 + (RAND() * 5)); To generate between 1000 to 9999, i.e., 4 digits then you can do the following using update query    UPDATE users SET password = FLOOR(1000 + RAND() * 8999);

Force HTTPS & WWW using htaccess

RewriteEngine on RewriteCond %{HTTPS} off # First rewrite to HTTPS: # Don't put www. here. If it is already there it will be included, if not # the subsequent rule will catch it. RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] # Now, rewrite any request to the wrong domain to use www. RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Get Latitude & Longitude from address

function getLatLong($address){     $prep_addr = str_replace(' ','+',$address);     $geocode = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prep_addr.'&sensor=false');     $output= json_decode($geocode);     $lat = $output->results[0]->geometry->location->lat;     $long = $output->results[0]->geometry->location->lng;     return array("latitude" => $lat, "longitude" => $long); }

Read .docx file as string

I found it after googling for a day, finally a function that reads the .docx file and return you a string. Hats off to the original author of this function. function readDocx($file_name){     $striped_content = '';     $content = '';     if(!$file_name || !file_exists($file_name))         return false;     $zip = zip_open($file_name);     if (!$zip || is_numeric($zip))         return false;     while ($zip_entry = zip_read($zip)) {         if (zip_entry_open($zip, $zip_entry) == FALSE)             continue;         if (zip_entry_name($zip_entry) != "word/document.xml")             continue;         $content .= zip_entry_read($zip_entry, zip_entry_filesize($zip...

Convert object to multi dimensional array

   /**     *     * Convert an object to an array     *     * @param    object  $object The object to convert     * @reeturn      array     *     */      function  objectToArray (  $object  )     {         if( ! is_object (  $object  ) && ! is_array (  $object  ) )         {             return  $object ;         }         if(  is_object (  $object  ) )     ...