Archive

Archive for the ‘php’ Category

Cons of Storing Files In Mysql

October 21, 2014 Leave a comment

Actually this is a terrible idea in real world scenario and the reason is:

if you need to recover your database and/or copy to another server from a backup sql file, you can not import the sql file into mysql.

Tested with different mysql and mariadb versions on different linux platforms.

Every time i try to import the sql file mysql crashes. Only way to recover your database is removing table data which includes files (blob column) from your sql file.

Of course you have another option to backup and recover your data. Copy actual database/tables (/var/lib/mysql/) to the new server and run:

[root@mk ~]# mysql_upgrade
Advertisements

PHP Calculate time difference between countries v2

May 2, 2013 1 comment

Previous code wasn’t bad but geoip_time_zone_by_country_and_region() has limitations.

geoip_time_zone_by_country_and_region() CONS:

  • PECL extension
  • Can not calculate timezone for every country
  • You must use country and area to calculate time zone*

* Some countries have more than 1 time zone such as Indonesia (4 time zones) and you must give area name together with country code to calculate each time zone

So we’ll change the previous code and use DateTimeZone::listIdentifiers() function

DateTimeZone::listIdentifiers() PROS:

  • Built in to php core
  • Retrieves every possible time zone for a country
  • Works for every country

So here is the code:

Origin country: New York/United States
Destination Country: Indonesia

First we will retrieve each timezone within Indonesia as an array:

$timezones = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, "ID");

Then we will find offset between New York and each time zone in Indonesia. We will use get_timezone_offset() custom function (that i mentioned in the previous post) to find offset:

function get_timezone_offset($remote_tz, $origin_tz = null) {
    if ($origin_tz === null) {
        if (!is_string($origin_tz = date_default_timezone_get()))
             return false;
    }
    $origin_dtz = new DateTimeZone($origin_tz);
    $remote_dtz = new DateTimeZone($remote_tz);
    $origin_dt = new DateTime("now", $origin_dtz);
    $remote_dt = new DateTime("now", $remote_dtz);
    $offset = $origin_dtz->getOffset($origin_dt) - $remote_dtz->getOffset($remote_dt);

    return $offset;
}

So lets put these 2 functions together and save time in destination country, timezone of destination country and hour difference in a array then print results.

Here is the complete code ready to copy and paste:

function get_timezones($country) {
$i = 0;
$timezones = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, $country);

foreach ($timezones as $timezone) {
    $offset = get_timezone_offset($timezone, 'America/New_York');
    $offset_time = time() - $offset;

    $dr[$i]['timeincountry'] = date('Y-m-d H:i', $offset_time);
    $dr[$i]['timezone'] = $timezone;

    $of = $offset / 3600;

    if ($timezone) {
        if ($of > 0) {
            $dr[$i]['hourdifference'] = abs($of) . " hours behind";
        } elseif ($of < 0) {
            $dr[$i]['hourdifference'] = abs($of) . " hours ahead";
        } else {
            $dr[$i]['hourdifference'] = "There is no time difference";
        }
    }
    $i++;
}
}

function get_timezone_offset($remote_tz, $origin_tz = null) {
    if ($origin_tz === null) {
        if (!is_string($origin_tz = date_default_timezone_get()))
             return false;
    }
    $origin_dtz = new DateTimeZone($origin_tz);
    $remote_dtz = new DateTimeZone($remote_tz);
    $origin_dt = new DateTime("now", $origin_dtz);
    $remote_dt = new DateTime("now", $remote_dtz);
    $offset = $origin_dtz->getOffset($origin_dt) - $remote_dtz->getOffset($remote_dt);

    return $offset;
}

// Calculate time differences between new york and Indonesia then print out the results

$timezones = get_timezones("ID");

foreach ($timezones as $tz) {
    $content.= '<div id="boxcorner">';
    $content.= $tz['timezone'] . '</br>';
    $content.= $tz['timeincountry'] . '</br>';
    $content.= $tz['hourdifference'];
    $content.= '</div>';
}

echo $content;

PHP find time difference between two countries by country code

April 28, 2013 1 comment

To calculate time difference between two countries following code can be used:

i will calculate time difference between United States (New York) and Jamaica

Origin: United States
Destination: Jamaica

Find Jamaica time zone

(note if you need accurate result and/or there are more than 1 timezone in calculated country you need to use city/province code with geoip_time_zone_by_country_and_region() function as second parameter to get timezone)

$timezone = geoip_time_zone_by_country_and_region("JM");

Find offset between destination time zone and origin timezone

$offset = get_timezone_offset($timezone, 'America/New_York');

Our custom get_timezone_offset() function that we use to calculate offset between 2 locations:

function get_timezone_offset($remote_tz, $origin_tz = null) {
    if ($origin_tz === null) {
        if (!is_string($origin_tz = date_default_timezone_get())) {
            return false;
        }
    }
    $origin_dtz = new DateTimeZone($origin_tz);
    $remote_dtz = new DateTimeZone($remote_tz);
    $origin_dt = new DateTime("now", $origin_dtz);
    $remote_dt = new DateTime("now", $remote_dtz);
    $offset = $origin_dtz->getOffset($origin_dt) - $remote_dtz->getOffset($remote_dt);
    return $offset;
}

Add offset to current time:

$offset_time = time() - $offset;

Print current time and timezone of destination country

echo "Time in JM: ".date('Y-m-d H:i', $offset_time) ." ($timezone)</br>";

Convert offset to hour format (default is seconds)

$of=$offset/3600;

Print time difference in hours:

if ($of > 0){
   echo "JM is ".abs($of)." hours behind";
}elseif($of < 0){
   echo "JM is ".abs($of)." hours ahead";
}else{
   echo "There is no time difference";
}

Note:Make sure you set your local time zone at the beginning of you code otherwise time() function results will be in accurate

date_default_timezone_set('America/New_York');

Check out the new version

**get_timezone_offset function is taken from php.net