Translate timestamp into words using PHP

Posted: May 28th, 2009 | Author: admin | Filed under: PHP | Tags: , | No Comments »

Here’s a nifty class I use for getting the date difference and translating them into words.

 class Date  {

    	public static function date_diff($dateTimeBegin,$dateTimeEnd) {

    		$dateTimeBegin = @strtotime($dateTimeBegin);

    		if($dateTimeBegin === -1) throw new InvalidDate("$dateTimeBegin: invalid date");

    		$dateTimeEnd=@strtotime($dateTimeEnd);

    		if($dateTimeEnd === -1) throw new InvalidDate("$dateTimeEnd: invalid date");

    		$dif=$dateTimeEnd - $dateTimeBegin;

    		if ($dif == 0) return "< 1 second";

    		if ($dif == 1) return "1 second";

    		$return_val = null;
    		$weeks      = floor($dif / 604800);

    		if ($weeks > 0) {
    			$dif -= $weeks * 604800;
    			$return_val .= "$weeks week".(($weeks>1)?'s':'');
    		}

    		$days = floor($dif / 86400);

    		if ($days > 0) {
    			$dif -= $days * 86400;
    			$return_val .= !is_null($return_val)?' and ':'';
    			$return_val .= "$days day".(($days>1)?'s':'');
    		}

    		$hours = floor($dif/3600);

    		if ($hours > 0) {
    			$dif -= $hours * 3600;
    			$return_val .= !is_null($return_val)?' and ':'';
    			$return_val .= "$hours hour".(($hours>1)?'s':'');
    		}

    		$minutes = floor($dif/60);

    		if ($minutes > 0) {
    			$dif -= $minutes * 60;
    			$return_val .= !is_null($return_val)?' and ':'';
    			$return_val .= "$minutes minute".(($minutes>1)?'s':'');
    		}

    		$seconds = $dif;

    		if ($seconds > 0) {
    			$return_val .= !is_null($return_val)?' and ':'';
    			$return_val .= "$seconds second".(($seconds>1)?'s':'');
    		}

    		return $return_val;
    	}

    	public static function format_date($date) {
    	    $return_val = null;
    		$weeks      = floor($date / 604800);

    		if ($weeks > 0) {
    			$dif -= $weeks * 604800;
    			$return_val .= "$weeks week".(($weeks>1)?'s':'');
    		}

    		$days = floor($date / 86400);

    		if ($days > 0) {
    			$date -= $days * 86400;
    			$return_val .= !is_null($return_val)?' and ':'';
    			$return_val .= "$days day".(($days>1)?'s':'');
    		}

    		$hours = floor($date/3600);

    		if ($hours > 0) {
    			$date -= $hours * 3600;
    			$return_val .= !is_null($return_val)?' and ':'';
    			$return_val .= "$hours hour".(($hours>1)?'s':'');
    		}

    		$minutes = floor($date/60);

    		if ($minutes > 0) {
    			$date -= $minutes * 60;
    			$return_val .= !is_null($return_val)?' and ':'';
    			$return_val .= "$minutes minute".(($minutes>1)?'s':'');
    		}

    		$seconds = $date;

    		if ($seconds > 0) {
    			$return_val .= !is_null($return_val)?' and ':'';
    			$return_val .= "$seconds second".(($seconds>1)?'s':'');
    		}

    		return $return_val;
    	}
    }

Adding security to your SVN directory using .htaccess

Posted: May 25th, 2009 | Author: admin | Filed under: Server Related Stuff | No Comments »

If you are familiar with SVN, your directory tree of your project or website would be populated by .svn folders. These folders are a security vulnerability since this is where you keep your file revisions and other information regarding the structure and layout of your website. Here’s a simple solution made by Adam Gotterer that protect and secure those directories by using .htaccess:

RewriteRule (\.svn)/(.*?) - [F,L]

Another good tip is to use SVN export instead of a checkout or rsync.


Connect to a Unix system using PHP SSH2

Posted: May 22nd, 2009 | Author: admin | Filed under: PHP | No Comments »

In order to use this class, SSH2 lib must be installed in your PHP configuration. Go here for detailed instructions on how to install and compile SSH2 lib in your web server.

class Ssh {

        private $host = '';
        private $user = '';
        private $port = '22';
        private $password = '';
        private $con = null;
        private $shell_type = 'xterm';
        private $shell = null;
        private $log = '';

        public function __construct($params) {
            if($params['host'] != '') $this->host  = $params['host'];
            if($params['port'] != '') $this->port  = $params['port'];
        }

        public function connectShell() {
            $this->con = @ssh2_connect($this->host, $this->port);

            if(!$this->con) {
                $this->log .= "Connection failed!\n";

                return FALSE;
            } else {
                return TRUE;
            }
        }

        public function authPassword($user = '', $password = '' ) {
            if($user != '') $this->user = $user;

            if($password != '') $this->password = $password;

            if(!@ssh2_auth_password($this->con, $this->user, $this->password) ) {
                $this->log .= "Authorization failed!\n";

                return FALSE;
            } else {
                return TRUE;
            }
        }

        public function execShell($command) {
            if(!($stream = @ssh2_exec($this->con, $command)) ){
                $this->log .= "Fail: Unable to execute command!\n";

                return FALSE;
            } else {
                // collect returning data from command
                stream_set_blocking($stream, true );
                $this->log .= stream_get_contents($stream);
                fclose($stream);                

                return TRUE;
            }
        }

        public function sendFile($local_file, $remote_file, $mode = '0644') {
            if(!ssh2_scp_send($this->con, $local_file, $remote_file)) {
                $this->log .= "Cannot send file to server.\n";

                return FALSE;
            } else {
                return TRUE;
            }
        }

        public function getLog() {
            return $this->log;
        }

    }

Ajax preloading/loading animation using jQuery BlockUI

Posted: May 20th, 2009 | Author: admin | Filed under: Javascript | Tags: , , | No Comments »

This tutorial shows you how to block/disable your web page whenever an application is processing an ajax request. First, you would have to download the latest jQuery library and the jQuery BlockUI plugin. Usage is very simple; Here’s a sample to block user activity for the page whenever an ajax request is called:

<script type="text/javascript" src="jquery-latest.js" language="javascript"></script>
<script type="text/javascript" src="jquery.blockui.js" language="javascript"></script>
<script type="text/javascript" language="javascript">
<!--
	$(document).ready( function () {
		<!-- Page block animation during ajax requests -->
		$().ajaxStart($.blockUI).ajaxStop($.unblockUI);		

		$('#form_button').click( function () {
			$.ajax({
			<!-- Ajax actions here -->
				....
			});
		});
	});
-->
</script>

For more options on using jQuery BlockUI plugin go here


Cool Ajax Preload Images

Posted: May 20th, 2009 | Author: admin | Filed under: Javascript | Tags: , | No Comments »

If you’re looking for cool ajax preloading animated images, go to http://www.ajaxload.info. You get to customize your preload images and download them for free. Try it out!