Skip to main content

Posts

Find relative duration of an event using PHP for given date time (seconds ago, minutes ago, hours ago, days ago, weeks ago, months ago, years ago)

You might have seen that many of the live applications shows the time relative to when it was posted as seconds ago, minutes ago, hours ago, days ago, weeks ago, month ago, year ago and so on. Instead of showing the whole date and time of any possible action on the application it is shown with smaller units, more simpler ones like a minute ago, 15 minutes and 10 seconds ago etc. Here is how we achieve it using PHP, below are two methods that can be used, it requires a Date Time string in YYYY-MM-DD HH:II:SS format. Method One: /** * returns the time ago in string * @param string $date_time * * @return string */ function timeAgo($date_time) { $time_ago = strtotime($date_time); $cur_time = time(); $time_elapsed = $cur_time - $time_ago; $seconds = $time_elapsed; $minutes = round($time_elapsed / 60); $hours = round($time_elapsed / 3600); $days = round($time_elapsed / 86400); $weeks = r...

Recovering an InnoDB table from only an .ibd file

Sometime you may need to recover a table when all you have is the .ibd file. In this case, if you try to load it into a new instance, your likely to encounter some errors about the table id not matching. And there is not really a way around this. However, I’ve found two work-arounds for this: Note: You will need the .ibd file and the CREATE TABLE statement for each table you want to recover using these methods. Simulate the internal InnoDB table counter. That is, create work tables (with innodb_file_per_table enabled) until you have the internal pointer of table id equal to (1 – id_of_ibd_table_you_need_to_restore). (See Method #1) Manually hex edit the .ibd file, changing the table id. (See Method #2) *Note: There are some internal structures with this meta information, so you’ll need to dump/import that data after you get it loaded, so you avoid unpleasantries that will inevitably show their face. Method #1 – Create work tables Start up clean/fresh instance of...

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); }