Monthly Archives: February 2012

How to generate unique ID in php

Many developers use md5 or rand() function to generate the unique id in php, Which is actually not a good practice under some senario. The best technique is to use the pre given php function whic is uniqid()

Example:

 

    // generate unique string
    echo uniqid();
    /* prints
    4bd67c947233e
    */  

    // generate another unique string
    echo uniqid();
    /* prints
    4bd67c9472340
    */
    // with prefix
    echo uniqid('ilovephp_');
    /* prints
    ilovephp_4bd67d6cd8b8f
    */  

    // with more entropy
    echo uniqid('',true);
    /* prints
    4bd67d6cd8b926.12135106
    */  

    // both
    echo uniqid('phpfresher_',true);
    /* prints
    phpfresher_4bd67da367b650.43684647
    */

Related Links for PHP

Link one

Link Two

Link Three

height:auto not working

I have been facing with css problem from long time, that i have three divs

  • container
  • leftbar
  • rightbar
    .container{
          height: auto;
         ....
         ...
 }
.leftbar{
          float:left;
         ....
         ...
 }
.rightbar{
        float:right;
.....
}

The problem i am facing is, when the rightbar div or the leftbar div height increases , the container div height remains the constant.
Finally i solved the problem by adding the property overflow hidden in the container div.

Here is the solution i solve the problem of Css height auto not working

    .container{
          height: auto;
         overflow: hidden;
         ....
         ...
 }
.leftbar{
          float:left;
         ....
         ...
 }
.rightbar{
        float:right;
.....
}

Difference between two dates in php

Suppose i have two dates in any format and i need to find the difference between the two dates , then the solution to find the difference between the two dates in php is given below:

You can use strtotime() to convert two dates to unix time and then calculate the number of seconds between them. From this it’s rather easy to calculate different time periods.

This following code will show you how to calculate difference between two dates by using PHP

Feel free to share

function datedifference($date1,$date2){

$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));

$minuts  = 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 - $minuts*60));

if($years>0){
echo $years.'y-'.$months.'m-'.$days.'d-'.$hours.'h-'.$minuts.'m-'.$seconds.'s';
}

if($months>0&&$years==0){
echo $months.'m-'.$days.'d-'.$hours.'h-'.$minuts.'m-'.$seconds.'s';
}
if($days>0&&$years==0){
echo $days.'d-'.$hours.'h-'.$minuts.'m-'.$seconds.'s';
}
if($hours>0&&$months==0&&$years==0){
echo $hours.'h-'.$minuts.'m-'.$seconds.'s';
}
if($minuts>0&&$hours==0&&$months==0&&$years==0){
echo $minuts.'m-'.$seconds.'s';
}
if($seconds>0&&$minuts==0&&$hours==0&&$months==0&&$years==0){
echo $seconds.'s';
}
}

How to disable the Tinymce Browser in Selected Textarea

  • Please First read the Documentation of TInymce Editor Carefully Because The documentation of tinmce editor is the best one.
  • If you haven’t read about the general configuration , Click here
  • If you haven’t read about setting height and width , Click here
  • Ok, Lets come to the point i.e Displaying the tinymce editor in the desired textareas.
  • Here is the Setting
  • tinyMCE.init({
    // General options
    mode : “specific_textareas”,
    editor_selector : “texteditor”,
    mode:”textareas”,
    theme : “advanced”,
    editor_deselector : “noeditor”,
    width : 700,
    height : 400,
  • How to Use, Here is the Code if you want to Exclude the Tinymce Browser in the Textarea,
  • <textarea class=”noeditor” name=”some_name”></textarea>
  • If you want to Include the Tinymce Editor  Here is the Sample,
  • <textarea name=”some_name” class=”texteditor”></textarea>
  • Hope you enjoyed …..
  • Related Website   Click Here or Click Here

Setting height and width in Tinymce Editor

    • If you havent read the basic Setting please Read , and
    • <html>
    <head>
    <script language="javascript" type="text/javascript" src="../jscripts/tiny_mce/tiny_mce.js"></script>
    <script language="javascript" type="text/javascript">
    tinyMCE.init({
            theme : "advanced",
            mode : "textareas",
           width : 700,
            height : 400
    });
    </script>
    </head>
  • The height and width of the Tiny Browser as specified above. Enjoy….