Remove Form Decorators From view file in Zend Framework

If you want complete customization of the zend form, it can be done through the Decorators by using addDisplayGroup() method. But for new users it is little bit difficult and need to write lots of code. You can do this in view script.In view script I had remove all the decoration of the default zend form by using this code:

$form = $this->form;
foreach ($form->getElements() as $element)
{
    $element->removeDecorator('DtDdWrapper')
            ->removeDecorator('HtmlTag')
            ->removeDecorator('Label')
            ->removeDecorator('Errors');
}

Removed all the decoration of the form.
To show the errors before the forms:

foreach ($this->form->getMessages() as $elemName => $messages)
{
    foreach ($messages as $message)
    {
        $label = $form->getElement($elemName)->getLabel();
        echo $this->escape($label . ':' . $message);
    }
}

The generate the form in the following way and then organize your form elements as you want.

<form method="<?php echo $form->getMethod() ?>" action="<?php echo $form->getAction() ?>">
          <?php
          echo $form->categoryName;
          echo'<br />';
          echo $form->groupName;
          echo'<br />';
          echo $form->Submit;
          echo'<br />';
          echo $form->Clear;
          ?>          
</form>

How to SWAP values of two variables without using a third variable?

1. Arithmetic operation
2. Bitwise operation.

Arithmetic Operation:

Bitwise operation:
x = x XOR y
y = x XOR y
x = x XOR y
if we write them in computer language using caret (^) then it will look like

x = x ^ y
y = x ^ y
x = x ^ y

in shorter version:

x ^= y
y ^= x
x ^= y

in one line

x ^= y ^= x ^= y

so here is the command in php:

$x ^= $y ^= $x ^= $y

This is collected from http://www.mizanurrahman.com/2008/07/30/how-to-swap-values-of-two-variables-without-using-a-third-variable. You can get more details about these operation on his blog.

Date difference interms of year/month/week/day/hour/minute/seconds

Some times we need to calculate date difference between 2 dates  in the format such as how many years or months or weeks or days or hours or minutes or seconds left from a particular date. Here is nice and simple solutions for this type of problem.


function compare_dates($date1, $date2) {
     $blocks = array(
         array('name' => 'year', 'amount' => 60 * 60 * 24 * 365),
         array('name' => 'month', 'amount' => 60 * 60 * 24 * 31),
         array('name' => 'week', 'amount' => 60 * 60 * 24 * 7),
         array('name' => 'day', 'amount' => 60 * 60 * 24),
         array('name' => 'hour', 'amount' => 60 * 60),
         array('name' => 'minute', 'amount' => 60),
         array('name' => 'second', 'amount' => 1)
     );

$diff = abs($date1 - $date2);

$levels = 2;
     $current_level = 1;
     $result = array();
     foreach ($blocks as $block) {
         if ($current_level > $levels) {
             break;
         }
         if ($diff / $block['amount'] >= 1) {
             $amount = floor($diff / $block['amount']);
             if ($amount > 1) {
                 $plural = 's';
             } else {
                 $plural = '';
             }
             $result[] = $amount . ' ' . $block['name'] . $plural;
             $diff -= $amount * $block['amount'];
             $current_level++;
         }
     }
     return implode(' ', $result);
 }

[NB: $date1 & $date2 must be in time stamp]