Search This Blog

Thursday, December 9, 2010

PHP :: Date & Time Options

Hey Cakes..!
Lets see how we can modify the Date/Time as we want..


ADD & DIVIDE TIME:

$time = date('H:i:s');
$data = explode(":",$time);
$min += $data['1'];
$min += $data['0'] * 60;
$endresult = $min/$count;
$avg = date("H:i",mktime(0,$endresult,0,0,0,0));

TIME DURATION BETWEEN TWO TIME:

$before = strtotime('2010-08-26 08:00:00');
$after = strtotime('2010-08-26 18:50:00');
$diff = $after - $before;
$hours = floor($diff / 3600);
$minutes = floor(($diff - $hours * 3600) / 60);
$seconds = $diff - $hours * 3600 - $minutes * 60;
IF(STRLEN($hours)<2)$hours="0".$hours;
IF(STRLEN($minutes)<2)$minutes="0".$minutes;
IF(STRLEN($seconds)<2)$seconds="0".$seconds;
debug($hours." : ".$minutes." : ".$seconds);

FIND TOMORROW'S DATE

$start = date('Y-m-d');
$s = strtotime($start);
$start = date('Y-m-d', strtotime('+1 day', $s));

FIND NEXT TIME:

$start = date('Y-m-d H:i:s');
$s = strtotime($start);
$start = date('Y-m-d H:i:s', strtotime('+20 min', $s)); // Finds the date and time for after 20 minutes

ADD TWO TIME:

function add_time($start, $end){
       $str = explode(':', $start);
       $en = explode(':', $end);
       $hours = $str[0] + $en[0];
       $minutes = $str[1] + $en[1];
       $sec = $str[2] + $en[2];
       while($sec > 60){
               $minutes++;
               $sec = $sec - 60;
       }
       while($minutes > 60){
               $hours++;
               $minutes = $minutes - 60;
       }
       return($hours.":".$minutes.":".$sec);
}

CONVERT TIME TO SECONDS:

$totaltime = '15:45:00';
$sat=explode(':',$totaltime);

$totalsec =  ( (int)$sat[0]*3600 ) + ( (int)$sat[1]*60 ) + (int)$sat[2];

FIND AVERAGE TIME:

function avg_time($time_in_seconds=null,$number=null){
       if($number){
          $totaltime =$time_in_seconds/$number;
       }else{
         $totaltime=0;
      }
       $hours = floor( $totaltime/ (60*60));
       $mins = floor(($totaltime - $hours * 60*60) / (60));
       $secs = floor(($totaltime - $hours * 60*60 - $mins*60));
       IF(STRLEN($hours)<2){$hours="0".$hours;}
       IF(STRLEN($mins)<2){$mins="0".$mins;}
       IF(STRLEN($secs)<2){$secs="0".$secs;}

       $tic_time = $hours.":".$mins.":".$secs;
    
 }

FIND LAST DAY OF ANY MONTH AND YEAR:

date("t", mktime(1, 1, 1, $month, 1, $year))


TIME DIFFERENCE BETWEEN THE TWO DATES:

function time_diff($date1=null,$date2=null){
    // Gets the time difference between the dates passed and returns it
in the format HH:MM:SS
         if(!$date1){
               $date1=$this->params[0];
         }
         if(!$date2){
               $date2=$this->params[1];
         }
           $before = strtotime($date1);
       $after = strtotime($date2);
       $diff = $after - $before;
       $hours = floor($diff / 3600);
       $minutes = floor(($diff - $hours * 3600) / 60);
       $seconds = $diff - $hours * 3600 - $minutes * 60;
               IF(STRLEN($hours)<2)$hours="0".$hours;
               IF(STRLEN($minutes)<2)$minutes="0".$minutes;
               IF(STRLEN($seconds)<2)$seconds="0".$seconds;
        $difference=$hours.":".$minutes.":".$seconds;
}


DAYS DIFFERENCE BETWEEN THE TWO DATES:

function dateDiff($dformat, $endDate, $beginDate) {
 // Function returns the number of days differing b/w two dates.
 // Here the $dformat is the delimiter say '-', '/', etc..
          $date_parts1=explode($dformat, $beginDate);
          $date_parts2=explode($dformat, $endDate);
          $start_date=gregoriantojd($date_parts1[1], $date_parts1[2],
$date_parts1[0]);
          $end_date=gregoriantojd($date_parts2[1], $date_parts2[2],
$date_parts2[0]);
          return $end_date - $start_date;
}

--
ಅರ್ಜುನ್ ಅರಸ್

Sorting Fields in custom array :: CakePHP

Hey Cakes...!

This blog is abt sorting the fields of a custom array..

My array looks like this:


Array
(
    [0] => Array
        (
            [Task] => Array
                (
                    [id] => 2
                    [comment_id] => 2
                    [name] => Work Load
                    [feedback] => Half Done
                    [percentage_completed] => 50
                    [task_date] => 2009-05-17
                )


            [comment] => Array
                (
                    [id] => 2
                    [name] => comment2
                )


        )


    [1] => Array
        (
            [Task] => Array
                (
                    [id] => 3
                    [comment_id] => 5
                    [name] => Server Maintenance
                    [feedback] => Task in progress
                    [percentage_completed] => 60
                    [task_date] => 2010-07-31
                )
            [Comment] => Array
                (
                    [id] => 5
                    [name] => server maintenance in progress
                )
       )
   [2] => Array
      (
           [Task] => Array
               (
                  [id] => 3
                  [comment_id] => 6
                  [name] => Process Complianced
                  [feedback] => Almost Done
                  [percentage_completed] => 90
                  [task_date] => 2010-07-05
               )
          [Comment] => Array
               (
                  [id] => 6
                  [name] => comment6
               )
      )
)


So here, i wanna sort [name] field of Task model.

To sort this array, i wil use Set::sort()


$somearray = Set::sort($somearray, '{n}.Task.name', 'desc');

This takes 3 arguments: The array to sort, Tthe array value to sort on, and the sorting order.

Here goes my Controller:



class TaskController extends AppController {


    var $name = 'Task';


    function index() {
        $tasks = $this->Task->find('all');
        $somearray = $tasks;
        $somearray = Set::sort($somearray, '{n}.Task.name', 'desc');
        $this->set('Task', $somearray);
    }


}
?>


After Sorting, here is my sorted array..



Array
(
    [0] => Array
        (
            [Task] => Array
                (
                    [id] => 3
                    [comment_id] => 6
                    [name] => Process Compliance
                    [feedback] => Almost Done
                    [percentage_completed] => 90
                    [task_date] => 2010-07-05
                )


            [comment] => Array
                (
                    [id] => 6
                    [name] => comment6
                )


        )


    [1] => Array
        (
            [Task] => Array
              (
                    [id] => 3
                    [comment_id] => 5
                    [name] => Server Maintenance
                    [feedback] => Task in progress
                    [percentage_completed] => 60
                    [task_date] => 2010-07-31
                )


            [Comment] => Array
               (
                  [id] => 5
                  [name] => server maintenance in progress
               )
        )


    [2] => Array
       (
             [Task] => Array
               (
                    [id] => 2
                    [comment_id] => 2
                    [name] => Work Load
                    [feedback] => Half Done
                    [percentage_completed] => 50
                    [task_date] => 2009-05-17
               )


            [Comment] => Array
              (
                   [id] => 2
                   [name] => comment2
              )
      )
)


Now, to sort an array like this:



Array
(
    [0] => Array
        (
            [001] => Array
                (
                    [id] => 2
                    [comment_id] => 2
                    [name] => Work Load
                    [feedback] => Half Done
                    [percentage_completed] => 50
                    [task_date] => 2009-05-17
                )


            [comment] => Array
                (
                    [id] => 2
                    [alias] => comment2
                )


        )


    [1] => Array
        (
            [002] => Array
                (
                    [id] => 3
                    [comment_id] => 5
                    [name] => Server Maintenance
                    [feedback] => Task in progress
                    [percentage_completed] => 60
                    [task_date] => 2010-07-31
                )
            [Comment] => Array
                (
                    [id] => 5
                    [alias] => server maintenance in progress
                )
       )
   [2] => Array
      (
           [003] => Array
               (
                  [id] => 3
                  [comment_id] => 6
                  [name] => Process Complianced
                  [feedback] => Almost Done
                  [percentage_completed] => 90
                  [task_date] => 2010-07-05
               )
          [Comment] => Array
               (
                  [id] => 6
                  [alias] => comment6
               )
      )
)


Here is the technique:


class TaskController extends AppController {
    var $name = 'Task';


    function index() {
        $tasks = $this->Task->find('all');
        $somearray = $tasks;
        $somearray = Set::sort($somearray, '{n}.{s}.name', 'desc');
        $this->set('Task', $somearray);
    }


}
?>


Sorting can also be done in find method or with paginate helper of Cakephp..
--
ಅರ್ಜುನ್ ಅರಸ್

CakePHP Installation in Ubuntu

Hey Cakes..!

Lets get started with CakePHP installation :

1. Install the required packages : sudo apt-get install apache2 mysql-server php5 php5-mysql

2. In case u encounter any Lock Error, make sure no other installation is happening and close Synaptic Package Manager if open. Even if the problem exists, execute: sudo killall apt-get

3. Enable Mod-rewrite: sudo a2enmod rewrite

4. Download the latest stable version of cake from http://cakephp.org/ and save it into your Ubutu machine.

5. Extract the downloaded file with this command: tar xvf downloaded_file.tar.gz

6. Rename the extracted folder with your project name(in my case, project name is cakephp) and move it to the document root (most of the times it will be /var/www/)

7. Then give writable permission to tmp folder:  sudo chmod -R 777 cakephp/app/tmp

8. Open file /etc/apache2/sites-enabled/000-default and change AllowOverride None to AllowOverride All

Command to Open: sudo vim.tiny /etc/apache2/sites-enabled/000-default


Change
    
       Options Indexes FollowSymLinks MultiViews
       AllowOverride None
       Order allow,deny
       allow from all

to

       Options Indexes FollowSymLinks MultiViews
       AllowOverride All
       Order allow,deny
       allow from all

9. Create .htaccess file inside the project directory(sudo vim.tiny /var/www/cakephp/.htaccess) and add these contents:


  RewriteEngine on
  RewriteRule    ^$ app/webroot/    [L]
  RewriteRule    (.*) app/webroot/$1 [L]


10. Restart Apache: sudo /etc/init.d/apache2 restart

CakePHP installation is complete. Open http://localhost/cakephp from you favorite web browser and Enjoy the Cake:-)


ಅರ್ಜುನ್ ಅರಸ್.

Tuesday, December 7, 2010

Eclipse+PHPEclipse+SVN Installation on Ubuntu

Hi Cakes..

Here I go with the steps to Install Eclipse Galilio + PHPEclipse + SVN onto your Ubuntu machine..

————————————————————————————————————

Lets start with Eclipse Installation:

Login as Root user and update the package repository info by this command: root@root:~# apt-get update (or) root@root:~#aptitude update
After updating, root@root:~# aptitude show eclipse will give you the Eclipse package which you are gonna install..

Then use this command to Install Eclipse Galileo: 
root@root:~# aptitude install eclipse

This finishes the Eclipse Installation.

————————————————————————————————————

Lets begin to Install PHPEclipse plugin on Eclipse..

Go to Applications->programming->eclipse
Inside Eclipse, select from the menu Help->Install New Softwares
Click on Add, and insert these Info:

Name: PHPEclipse

Now click on Next, select the items to be installed and Finish.
After Installing PHPEclipse, restart your eclipse to view the changes.

————————————————————————————————————

Now coming to SVN plugin:

Inside Eclipse, select from the menu Help->Install New Softwares
Click on Add, and insert these Info:

Name: Subversion

Now click on Next, select the items to be installed and Finish.
After Installing Subversion, restart your eclipse to view the changes.

————————————————————————————————————

While apt-get update, If you encounter any Lock Error, make sure you are logged in as root and no other apt-get process are running.
To kill other apt-get process, Use this command: 
root@root:~# sudo killall apt-get

————————————

Hope this helps..:)

ಅರ್ಜುನ್ ಅರಸ್