DateDiff

The DateDiff function has been another useful function in report building. It can be useful when trying to determine the average time for each stage of development in a project, or the amount of time that it took to complete.  I have used it enough in both php and ColdFusion applications.  I have found that using a consistent method of calculating dates is worth its weight in gold in the long run!
Parameter Description
datePart Optional. String. Precision of the comparison.
  • s Precise to the second (default)
  • n Precise to the minute
  • h Precise to the hour
  • d Precise to the day
  • m Precise to the month
  • yyyy Precise to the year
  • date1 Date/time object, in the range 100 AD–9999 AD.
    date2 Date/time object, in the range 100 AD–9999 AD.

    The Function

     <?php

     function DateDiff($datepart, $date1, $date2)
     {
      $a = explode(" " ,$date1);
      $b = explode("-",$a[0]);
      if(count($a) > 1)
      {
       $c = explode(":",$a[1]);
      } 
      else
      {
       $c = array();$c[ 0] = 0;$c[1] = 0;$c[2] = 0;
      }
      $a1 = explode (" ",$date2);
      $b1 = explode("-",$a1[0]);
      if(count($a1) > 1)
      {
       $c1 = explode(":",$a1[1]);
      } 
      else
      {
       $c1 = array();$c1[ 0] = 0;$c1[1] = 0;$c1[2] = 0;
      }
       switch($datepart)
      {
       case "n":
    // Minute
        $db= 60;break;
       case "h":
    // Hour
        $db= 60*60;break;
       case "d":
    // Day
        $db=(60*60)*24;break;
       case "w":
    // Weeks
        $db=((60*60)*24)* 7;break;
       case "m":
    // Month
        $db=ceil(((( 60*60)*24)*365)/12);break;
        case "yyyy": // Year
        $db=(((60*60)*24)* 365);break;
       default:
        $db=1;
      }
      $nDate1 = mktime($c[0],$c[ 1],$c[2],$b[1],$b[2],$b[0]);
      $nDate2 = mktime($c1[ 0],$c1[1],$c1[2],$b1[1],$b1[2],$b1[0]);
      
      $rDate = $nDate1 - $nDate2;
      
      $num = number_format(($rDate/$db),4);
      
      if($num < 0)
      {
        return $num * -1;
      }
      else
      {
       return $num;
      }
     }

    ?>

    Usage:
    This is a example of how to use the DateDiff Function:

    <?php
     
     $datepart = "s";
     $date1 = "2007-12-01 19:20:12" ;
     $date2 = "2007-11-15 11:45:39";
     echo DateDiff($datepart, $date1, $date2);

    ?>

    The result should be: 1,409,673!

    <?php
     
     $datepart = "yyyy";
     $date1 = "2007-12-01 19:20:12" ;
     $date2 = "2007-11-15 11:45:39";
     echo DateDiff($datepart, $date1, $date2);

    ?>

    The result should be: 0.0447!

     

    All About PHP array

    We know what is PHP variable. In a variable we can store information. But PHP variable has some problem such as in a variable we cannot store same types of information. Variable takes same types of information as a one information. But we need to store our information separately.  
    For an example, we want to store different name in a variable and the code is 
    <?php
    $variable="Sharif","Rahim", "Zohn"; 
    echo $variable;
    ?>
    This code show "Sharif","Rahim", "Zohn" but we want to show only Rahim or Zohn. That is the problem. To solve this problem we introduce PHP array. Array store same type of information and we can retrieve information separately from an array variable. To understand array we write a PHP code here.
    Suppose that we want to store some customers name in a variable. Take a look at the code 
    <?php
    $customers=array(“Sharif”,”Rahim”,”Karim”,”Shuvo”);
    echo $array[1];
    ?> 

    What is PHP

    Previous Tutorial                                                Next Tutorial

    The meaning of PHP is Hypertext Preprocessor. PHP is a server side scripting language. You may  ask me that why PHP is not programming language. I will discuss it but now you need to concentrate on PHP. Server side scripting language means that the language can contact with server and run in the server. PHP language is designed only for this kind of purpose related with web design and development. In our whole tutorial we use the term server that is actually means database and we use PHP-MySQL enable Vertrigo Server.

    We use server to store website information. We can show server information via PHP in the HTML page. Where the website information is stored in the database and the database information is displayed in the HTML page we can call this kind of website is a dynamic website. Otherwise if a website information shows only using HTML then we call this type of website isstatic website. So the thing is when we use database then we call it dynamic website and when we do not use database then we call it static website. So we can say that PHP is created for dynamic website development.

    PHP was created for web development

    PHP was developed for web development and we can embed PHP with HTML. PHP cannot do anything alone. See the following example that describe how we embed PHP with HTML. PHP Embeded with
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>PHP Embeded with HTML</title>

    </head>

    <body>

    <?php

    echo "This Programm Show You How PHP embed with HTML";

    ?>

    </body>

    </html>

    We use <?php and ?> tag to differentiate PHP code from HTML.The PHP code is run on the server. So you cannot see the PHP source code of a site, just you can see the HTML and CSS code. You can consider PHP as a website input and the output is HTML page.
    The syntax of PHP is similar to programming languages such as C, Java Programming language. So if you have any idea about programming language, you can easily understand  PHP. But if you have not, no problem. We have good tutorial on PHP, just you need to practice it. See the video tutorial that give you some clear understanding about PHP programming language. 

    Video Tutorial: What is PHP Or  PDF Tutorial: What is PHP.pdf 

    multi dimensional arrays in php

    <?php


    $birds= array('Parrots' =>
    array('Kakapo','Kea ','Vernal Hanging Parrot'),

    'Cuckoos and Turacos'=>
    array('Purple-crested Turaco','Grey Go-away-bird','Great Blue Turaco'));

    print_r($birds);

    echo '<br><br><br>';

    echo $birds['Parrots'][0].'<br>';
    echo $birds['Parrots'][1].'<br>';
    echo $birds['Parrots'][2].'<br>';


    echo '<br><br><br>';

    echo $birds['Cuckoos and Turacos'][0].'<br>';
    echo $birds['Cuckoos and Turacos'][1].'<br>';
    echo $birds['Cuckoos and Turacos'][2].'<br>';


    ?>


    output :
    Array ( [Parrots] => Array ( [0] => Kakapo [1] => Kea [2] => Vernal Hanging Parrot ) [Cuckoos and Turacos] => Array ( [0] => Purple-crested Turaco [1] => Grey Go-away-bird [2] => Great Blue Turaco ) )


    Kakapo
    Kea
    Vernal Hanging Parrot



    Purple-crested Turaco
    Grey Go-away-bird
    Great Blue Turaco