Archive for September, 2009

Meld : File Comaprison UI Tool [ Ubuntu ]

Posted in coldfusion, Linux, PHP, Technical, work with tags , , , , , , , , on September 21, 2009 by saffrongeek

I was in need of a file comparison tool , which is UI based and very handy application. Luckily, even though I heard about Kdiff and Emacs to do the purpose, I decided to try for Meld.

My comment : Wow!! Easy to get, set , Run. Very Handy to use.

Available @ CLICK_LINK Or Open a terminal and type the below command:

saffrongeek@saffrongeek-laptop:~$ sudo apt-get install meld

Note: You could also use Synaptic package manager for installation of this package.
meldBlog

Advertisements

Posting code, formulas, equations in blog posts

Posted in @home, Apache, Linux, mysql, PHP, Technical, work with tags , , , , , , , , , , on September 5, 2009 by saffrongeek

To post code snippets or formulas or equations I was finding the format loose when I save the post. I checked out any way to highlight the code or preserve the program structure to make it readable neat but I found it tough. Then, after a few trial and errors , I got a way for this. You are lucky and would not find it tough my way , if you have your own webspace. I still now had not. 😦

Steps to post the code , program, formulas, equations without losing format structure or readability on free blog spaces is to :-

Step #1: Switch to HTML mode

Step #2: Type the < pre > and < /pre > tags.
Step #3: Copy the code or equation or whatever you would like to preserve format.
Step #4: Open http://www.htmlconvert.net/, select filter tags option and paste the copied. click convert.

Step #5: Copy the converted and paste it between < pre>  and </ pre> tags and switch back to visual mode in editor.
Step #6: Now , publish and Hope you have the format preserved.

Let me know your thoughts.

Other good reads at :
http://www.thunderguy.com/semicolon/wordpress/code-markup-wordpress-plugin/
http://en.forums.wordpress.com/topic/problem-embedding-html-code-into-blog
http://wordpress.jdwebdev.com/blog/code-in-posts/
http://codex.wordpress.org/Writing_Code_in_Your_Posts

cwarn2’s classic function handles dates pre-1970

Posted in Apache, Linux, mysql, PHP, Technical, work with tags , , , , , , , , , on September 5, 2009 by saffrongeek

I would like to share a neat decent function to handle the dates pre-1970 which normally would find getting reset to 1970, January 01. I had a need where I need to handle birthdates pre-1970 and PHP’s in built functions like strtotime (), date(), mktime() , including safestrotime() of no help.

Thanks to the patience and effort of cwarn2, whom I met at Daniweb, I now have a function that address my need. This post is an acknowledgment to cwarn2 friendly mind. Thanks cwarn2.

To know more about cwarn2 and other contributions , please visit http://syntax.cwarn23.info/PHP:_Contents . There are other great scenarios where cwarn2 has contributed with solutions.

Below is the cwarn2’s function to handle dates pre-1970:

==================================================================================
=================== cwarn2 function to handle dates pre-1970 ================================

function get_date($date,$timestamp=false) {
    if ($timestamp==false) {
        $timestamp=time();
        } else {
        $timestamp=round($timestamp);
        }
    if ($timestamp>=0) {
        return date($date,$timestamp);
        } else {
        if ($timestamp$timestamp;) { //year loop
            $year--;
            for ($k=31; $k>0;$k--) { //month loop (December)
                $month=12;
                $day=$k;
                if (($i-86400)>$timestamp) {
                    $i-=86400;
                    $l=24; $hour=24;
                    $m=59; $minute=59;
                    $n=59; $second=59;
                    } else {
                    for ($l=24; $l>0;$l--) {
                        $hour=$l;
                        for ($m=59; $m>-1;$m--) {
                            $minute=$m;
                            for ($n=59; $n>-1;$n--, $i--) {
                                $second=$n;
                                if ($i==$timestamp) { break;break;break;break;break; }
                                }
                            if ($i==$timestamp) { break;break;break;break; }
                            }
                        if ($i==$timestamp) { break;break;break; }
                        }
                    }
                if ($i==$timestamp) { break;break; }
                }
            if ($i==$timestamp) { break; }
            for ($k=30; $k>0;$k--) { //month loop (November)
                $month=11;
                $day=$k;
                if (($i-86400)>$timestamp) {
                    $i-=86400;
                    $l=24; $hour=24;
                    $m=59; $minute=59;
                    $n=59; $second=59;
                    } else {
                    for ($l=24; $l>0;$l--) {
                        $hour=$l;
                        for ($m=59; $m>-1;$m--) {
                            $minute=$m;
                            for ($n=59; $n>-1;$n--, $i--) {
                                $second=$n;
                                if ($i==$timestamp) { break;break;break;break;break; }
                                }
                            if ($i==$timestamp) { break;break;break;break; }
                            }
                        if ($i==$timestamp) { break;break;break; }
                        }
                    }
                if ($i==$timestamp) { break;break; }
                }
            if ($i==$timestamp) { break; }
            for ($k=31; $k>0;$k--) { //month loop (October)
                $month=10;
                $day=$k;
                if (($i-86400)>$timestamp) {
                    $i-=86400;
                    $l=24; $hour=24;
                    $m=59; $minute=59;
                    $n=59; $second=59;
                    } else {
                    for ($l=24; $l>0;$l--) {
                        $hour=$l;
                        for ($m=59; $m>-1;$m--) {
                            $minute=$m;
                            for ($n=59; $n>-1;$n--, $i--) {
                                $second=$n;
                                if ($i==$timestamp) { break;break;break;break;break; }
                                }
                            if ($i==$timestamp) { break;break;break;break; }
                            }
                        if ($i==$timestamp) { break;break;break; }
                        }
                    }
                if ($i==$timestamp) { break;break; }
                }
            if ($i==$timestamp) { break; }
            for ($k=30; $k>0;$k--) { //month loop (September)
                $month=9;
                $day=$k;
                if (($i-86400)>$timestamp) {
                    $i-=86400;
                    $l=24; $hour=24;
                    $m=59; $minute=59;
                    $n=59; $second=59;
                    } else {
                    for ($l=24; $l>0;$l--) {
                        $hour=$l;
                        for ($m=59; $m>-1;$m--) {
                            $minute=$m;
                            for ($n=59; $n>-1;$n--, $i--) {
                                $second=$n;
                                if ($i==$timestamp) { break;break;break;break;break; }
                                }
                            if ($i==$timestamp) { break;break;break;break; }
                            }
                        if ($i==$timestamp) { break;break;break; }
                        }
                    }
                if ($i==$timestamp) { break;break; }
                }
            if ($i==$timestamp) { break; }
            for ($k=31; $k>0;$k--) { //month loop (August)
                $month=8;
                $day=$k;
                if (($i-86400)>$timestamp) {
                    $i-=86400;
                    $l=24; $hour=24;
                    $m=59; $minute=59;
                    $n=59; $second=59;
                    } else {
                    for ($l=24; $l>0;$l--) {
                        $hour=$l;
                        for ($m=59; $m>-1;$m--) {
                            $minute=$m;
                            for ($n=59; $n>-1;$n--, $i--) {
                                $second=$n;
                                if ($i==$timestamp) { break;break;break;break;break; }
                                }
                            if ($i==$timestamp) { break;break;break;break; }
                            }
                        if ($i==$timestamp) { break;break;break; }
                        }
                    }
                if ($i==$timestamp) { break;break; }
                }
            if ($i==$timestamp) { break; }
            for ($k=31; $k>0;$k--) { //month loop (July)
                $month=7;
                $day=$k;
                if (($i-86400)>$timestamp) {
                    $i-=86400;
                    $l=24; $hour=24;
                    $m=59; $minute=59;
                    $n=59; $second=59;
                    } else {
                    for ($l=24; $l>0;$l--) {
                        $hour=$l;
                        for ($m=59; $m>-1;$m--) {
                            $minute=$m;
                            for ($n=59; $n>-1;$n--, $i--) {
                                $second=$n;
                                if ($i==$timestamp) { break;break;break;break;break; }
                                }
                            if ($i==$timestamp) { break;break;break;break; }
                            }
                        if ($i==$timestamp) { break;break;break; }
                        }
                    }
                if ($i==$timestamp) { break;break; }
                }
            if ($i==$timestamp) { break; }
            for ($k=30; $k>0;$k--) { //month loop (June)
                $month=6;
                $day=$k;
                if (($i-86400)>$timestamp) {
                    $i-=86400;
                    $l=24; $hour=24;
                    $m=59; $minute=59;
                    $n=59; $second=59;
                    } else {
                    for ($l=24; $l>0;$l--) {
                        $hour=$l;
                        for ($m=59; $m>-1;$m--) {
                            $minute=$m;
                            for ($n=59; $n>-1;$n--, $i--) {
                                $second=$n;
                                if ($i==$timestamp) { break;break;break;break;break; }
                                }
                            if ($i==$timestamp) { break;break;break;break; }
                            }
                        if ($i==$timestamp) { break;break;break; }
                        }
                    }
                if ($i==$timestamp) { break;break; }
                }
            if ($i==$timestamp) { break; }
            for ($k=31; $k>0;$k--) { //month loop (May)
                $month=5;
                $day=$k;
                if (($i-86400)>$timestamp) {
                    $i-=86400;
                    $l=24; $hour=24;
                    $m=59; $minute=59;
                    $n=59; $second=59;
                    } else {
                    for ($l=24; $l>0;$l--) {
                        $hour=$l;
                        for ($m=59; $m>-1;$m--) {
                            $minute=$m;
                            for ($n=59; $n>-1;$n--, $i--) {
                                $second=$n;
                                if ($i==$timestamp) { break;break;break;break;break; }
                                }
                            if ($i==$timestamp) { break;break;break;break; }
                            }
                        if ($i==$timestamp) { break;break;break; }
                        }
                    }
                if ($i==$timestamp) { break;break; }
                }
            if ($i==$timestamp) { break; }
            for ($k=30; $k>0;$k--) { //month loop (April)
                $month=4;
                $day=$k;
                if (($i-86400)>$timestamp) {
                    $i-=86400;
                    $l=24; $hour=24;
                    $m=59; $minute=59;
                    $n=59; $second=59;
                    } else {
                    for ($l=24; $l>0;$l--) {
                        $hour=$l;
                        for ($m=59; $m>-1;$m--) {
                            $minute=$m;
                            for ($n=59; $n>-1;$n--, $i--) {
                                $second=$n;
                                if ($i==$timestamp) { break;break;break;break;break; }
                                }
                            if ($i==$timestamp) { break;break;break;break; }
                            }
                        if ($i==$timestamp) { break;break;break; }
                        }
                    }
                if ($i==$timestamp) { break;break; }
                }
            if ($i==$timestamp) { break; }
            for ($k=31; $k>0;$k--) { //month loop (March)
                $month=3;
                $day=$k;
                if (($i-86400)>$timestamp) {
                    $i-=86400;
                    $l=24; $hour=24;
                    $m=59; $minute=59;
                    $n=59; $second=59;
                    } else {
                    for ($l=24; $l>0;$l--) {
                        $hour=$l;
                        for ($m=59; $m>-1;$m--) {
                            $minute=$m;
                            for ($n=59; $n>-1;$n--, $i--) {
                                $second=$n;
                                if ($i==$timestamp) { break;break;break;break;break; }
                                }
                            if ($i==$timestamp) { break;break;break;break; }
                            }
                        if ($i==$timestamp) { break;break;break; }
                        }
                    }
                if ($i==$timestamp) { break;break; }
                }
            if ($i==$timestamp) { break; }
            if (round((1972-$year)/4)==((1972-$year)/4)) {
                $j=29;
                } else {
                $j=28;
                }
            for ($k=$j; $k>0;$k--) { //month loop (February)
                $month=2;
                $day=$k;
                if (($i-86400)>$timestamp) {
                    $i-=86400;
                    $l=24; $hour=24;
                    $m=59; $minute=59;
                    $n=59; $second=59;
                    } else {
                    for ($l=24; $l>0;$l--) {
                        $hour=$l;
                        for ($m=59; $m>-1;$m--) {
                            $minute=$m;
                            for ($n=59; $n>-1;$n--, $i--) {
                                $second=$n;
                                if ($i==$timestamp) { break;break;break;break;break; }
                                }
                            if ($i==$timestamp) { break;break;break;break; }
                            }
                        if ($i==$timestamp) { break;break;break; }
                        }
                    }
                if ($i==$timestamp) { break;break; }
                }
            if ($i==$timestamp) { break; }
            for ($k=31; $k>0;$k--) { //month loop (January)
                $month=1;
                $day=$k;
                if (($i-86400)>$timestamp) {
                    $i-=86400;
                    $l=24; $hour=24;
                    $m=59; $minute=59;
                    $n=59; $second=59;
                    } else {
                    for ($l=24; $l>0;$l--) {
                        $hour=$l;
                        for ($m=59; $m>-1;$m--) {
                            $minute=$m;
                            for ($n=59; $n>-1;$n--, $i--) {
                                $second=$n;
                                if ($i==$timestamp) { break;break;break;break;break; }
                                }
                            if ($i==$timestamp) { break;break;break;break; }
                            }
                        if ($i==$timestamp) { break;break;break; }
                        }
                    }
                if ($i==$timestamp) { break;break; }
                }
            if ($i==$timestamp) { break; }
 
            }
        }
        if ($hour>12) {
            $ampm='PM';
            $pmam='pm';
            } else {
            $ampm='AM';
            $pmam='am';
            }
        if ($hour>12) {
            $hr=$hour-12;
            } else {
            $hr=$hour;
            }
        if ($second<10) {
            $second='0'.$second;
            }
        if ($minute<10) {
            $minute='0'.$minute;
            }
        return str_replace(array('Y','n','j','H','h','i','s','a','A'),array($year,$month,$day,$hour,$hr,$minute,$second,$pmam,$ampm),$date);
    }

PHP:: Pre-1970 Dates:: Solution Simple Weird One

Posted in Apache, Linux, mysql, PHP, Technical, work with tags , , , , , , , , , , , on September 5, 2009 by saffrongeek

=================================================================

SOLUTION : [ Weird one but worked…]

==================================================================

#1. Solution WORKED:-

Input date: 1/1/1900
Output desired as inserted in database : 1900-01-01

Step #1. Exploded the input date to a array.
Step #2. Formed a needed format variable($timestamp) from the array
Step #3. Just to make sure cast as ‘String’ : ( String ) $timestamp

Inserted to the datebase , and the date type field holds the date correctly and the way expected.

==========================================================================

PHP:: Pre-1970 Dates :: Functions Explored for a task

Posted in Apache, Linux, mysql, PHP, Technical, work with tags , , , , , , , , , , , , , , on September 4, 2009 by saffrongeek

The purpose of this post is to collect different possible solutions or custom functions available on net or shared for my query in Web Development forum to solve a issue with dates before 1970.

Scenario: Today at work, I had a scenario where I have to fetch the dates in 1/1/1900 format and insert it to a new database as 1900-01-01. The issue i face is my date before 1970 gets reset to 01-01-1970.

Input date: 1/1/1900
Output desired as inserted in database : 1900-01-01

First Efforts to resolve , but didnt worked out is :-
=> Tried strtotime()  and date()+mktime() but all gave date reset to 1970, Jan, 01.

  • strftime(“%Y-%m-%d”, strtotime($timestamp));
  • date(‘Y-M-d’, mktime(0,0,0,1,1,1900))

=> safestrtotime() :: There is safestrtotime in the strtotime() page in php.net which seems to work but the dates is not precisely the same anymore . For eg:- strftime(“%Y-%m-%d”, safestrtotime($timestamp)); gave 1901-12-14.

=> safestrtotime function was as below:

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Below Function is available in strtotime function documentation page in http://www.php.net

	function safestrtotime($strInput) {
        $iVal = -1;
        for ($i=1900; $i<=1969; $i++) {
            # Check for this year string in date
            $strYear = (string)$i;
            if (!(strpos($strInput, $strYear)===false)) {
                $replYear = $strYear;
                $yearSkew = 1970 - $i;
                $strInput = str_replace($strYear, "1970", $strInput);
            };
        };
        $iVal = strtotime($strInput);
        if ($yearSkew > 0) {
            $numSecs = (60 * 60 * 24 * 365 * $yearSkew);
            $iVal = $iVal - $numSecs;
            $numLeapYears = 0;        # Work out number of leap years in period
            for ($j=$replYear; $j<=1969; $j++) {
                $thisYear = $j;
                $isLeapYear = false;
                # Is div by 4?
                if (($thisYear % 4) == 0) {
                    $isLeapYear = true;
                };
                # Is div by 100?
                if (($thisYear % 100) == 0) {
                    $isLeapYear = false;
                };
                # Is div by 1000?
                if (($thisYear % 1000) == 0) {
                    $isLeapYear = true;
                };
                if ($isLeapYear == true) {
                    $numLeapYears++;
                };
            };
            $iVal = $iVal - (60 * 60 * 24 * $numLeapYears);
        };
        return($iVal);
    }

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

=>  Another function using basetime explored is as below:-

	function Hsafestrtotime ($s) {
	  $basetime = 0;
	  if (preg_match ("/19(\d\d)/", $s, $m) && ($m[1] < 70)) {
	    $s = preg_replace ("/19\d\d/", 1900 + $m[1]+68, $s);
	    $basetime = 0x80000000 + 1570448;
	  }
	  return $basetime + strtotime ($s);
	}

/////////////////////////////////////////////////////////////////////////////////////////////////////////

	function convertTime($dformat,$sformat,$ts) {
		    extract(strptime($ts,$sformat));
		    return strftime($dformat,mktime(
		                                  intval($tm_hour),
		                                  intval($tm_min),
		                                  intval($tm_sec),
		                                  intval($tm_mon)+1,
		                                  intval($tm_mday),
		                                  intval($tm_year)+1900
		                                ));
   }

%d bloggers like this: