Tuesday, August 28, 2012

Format code snips for blog

To make keep the formatting of my code spinets that I just copied from my editor i use this tool 1. http://www.manoli.net/csharpformat/ It automatically convert your snippet into html friendly format

Calculate date and time diff in PHP

public function  calculateDateDiff($date1,$date2)
    {
            $string_diff = array();
            $diff = abs(strtotime($date2) - strtotime($date1));
            $years   = floor($diff / (365*60*60*24));
            $months  = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
            $days    = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
            $hours   = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60));
            $minutes  = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60);
            $seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minutes*60));
            if($years >=1 ){
                $string_diff[]= $years.' Year'.(($years>1)?'s':'');
            }
            if($months >=1 ){
                $string_diff[]= $months.' Month'.(($months>1)?'s':'');
            }
            if($days >=1 ){
                $string_diff[]= $days.' Day'.(($days>1)?'s':'');
            }
            if($hours >=1 ){
                $string_diff[]= $hours.' Hour'.(($hours>1)?'s':'');
            }
            if($minutes >=1 ){
                $string_diff[]= $minutes.' Minute'.(($minutes>1)?'s':'');
            }
            return implode (',', $string_diff);
    }

Monday, August 27, 2012

SVN - Display modified files only and revert

To display only the  modified files we need to use svn st with grep:
svn st | grep 'M '

now to revert only all those modified, we can do this
svn st | grep ' M' | awk '{print $2}' | xargs svn revert

you can also use this to delete or move those file by simply changing the last  arguments


Wednesday, August 22, 2012

Find string in all included files in PHP

Some times when debugging we find some variables or string, but when tons of php code is included its big problem. Here's how I search string or variable in all included files.
$file_included = get_included_files();

foreach($file_included as $val) {
    if(preg_match('/YOUR STRING HERE/',file_get_contents($val))){  
       echo $val.'

';
    }
}
You simply need to include that on the footer of your page

Saturday, August 18, 2012

how to clean array values

one of the fastest way is to use array map ex. $a = array('a',' b','c '); $b = array_map('trim',$a); or you can create your own function and use that as first parameter when calling array map

Ronaldo Bernal

Hi, I'm Ronaldo Bernal a web developer for some time :). I'll be posting here some interesting code I've done. So hope you enjoy it