Place holder using javascript

By using javascript we can create placeholder which is compatible with the older browsers which didn’t support the new placeholder

and here is the code on how we can do this

<input name="location" type="text" value="Location" 
onblur="this.value=!this.value?'Location':this.value;" onclick="this.value='';" />

Get subcategories of a categories with images in magento

Here is the code to show all the subcategories inside an category in magento

<?php
$children = Mage::getModel('catalog/category')->getCategories(10);
foreach ($children as $category) {
    echo $category->getName();
}
?>

Here 10 is the id of the parent category

and to get the url for that category use

<?php
$url=Mage::getModel('catalog/category')->load($category->getId())->getURL();
 ?>

You can also get the image of an category by adding this inside the for loop

<?php
$imageurl=Mage::getModel('catalog/category')->load($category->getId())->getImageUrl();
?>

Inorder to get the child categories of a category when include in navigation set to no
we have to use the below code to get the children categories

$_categories = Mage::getModel('catalog/category')->load(10)->getChildrenCategories();
foreach ($_categories as $category) {
    echo $category->getName();
}

How to add static blocks inside the phtml file

We can insert the static blocks inside an phtml file by using the identifier of the static block we created in the admin panel of magento

Here is the code to do so,

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('identifier')->toHtml();?>

In this case identifier must be replaced by the one which you created on the admin

How to find the difference between two dates using php

Here’s the code snippet to find the difference between two dates

<?php
 
$date1 = "2009-01-01 00:00:00";
$date2 = "2010-03-21 14:34:09"; 
$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)); 
echo $years." years, ".$months." months, ".$days." days, ".$hours." hours, ".$minutes." minutes, ".$seconds." seconds";
?>