Archive for January 2011

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


    Google gadget Ads

    Google has come up with new marketing tool google gadget ads.one of the coolest feature among all is that To be sure that your gadget ad has the latest data, you can force a refresh at an interval you specify.

    Google also provide Google ad editor to quickly build, test, and validate your gadget ads prior to submitting it for review.

    want some more Information for creating a google gadget ad?Click Here

    get browser data of visitor in php

    <?php
    $browser = get_browser(null,true);
    print_r($browser);
    ?>

    output :

    Array ( [browser_name_regex] => §^.*$§ [browser_name_pattern] => * [browser] => Default Browser [version] => 0 [majorver] => 0 [minorver] => 0 [platform] => unknown [alpha] => [beta] => [win16] => [win32] => [win64] => [frames] => 1 [iframes] => [tables] => 1 [cookies] => [backgroundsounds] => [cdf] => [vbscript] => [javaapplets] => [javascript] => [activexcontrols] => [isbanned] => [ismobiledevice] => [issyndicationreader] => [crawler] => [cssversion] => 0 [supportscss] => [aol] => [aolversion] => 0 )

    Understanding Http protocol

    Http stands for hyper text transfer protocol. A browser is an HTTP client because it sends requests to an HTTP server (Web server), which then sends responses back to the client. The standard (and default) port for HTTP servers to listen on is 80, though they can use any port.

    Here is basic steps how this works.

    1) When client [most of the case web browser] get input URL it check for local DNS cache for respective IP for making connection.

    2) If found, it goes to step 8

    3) If not found It make a call to OS for DNS Lookup and get IP of the server mentioned for respective
    URL.

    4) Once IP found client stats making socket connection using TCP protocol.

    5) Once connection is done, it sends request along with a request line header containing information about the request and request body.
         Request line contains Method, request URI, and http version.

    6) Server receive the request packet process it and respond further to respective client.

    7) Response contains status line, response headers and response body.

    8) Browser tries to read the response and render it. If it can not render the request. It opens up a dialog box for a user to open response with suitable program.

    Interview Questions


    1.What is PHP?
    PHP is a server side scripting language commonly used for web applications

    2.How to include a file to a php page?
     we can include a file using "include() " or "require()" function with as its parameter..

    3.What’s the difference between include and require?
    If the file is not found by require(), it will cause a fatal error and halt the execution of the script. If the file is not found by include(), a warning will be issued, but execution will continue.

    4.require_once (), require (), include().What is difference between them?
    require () includes and evaluates a specific file, while require_once() does that only if it has not been included before (on the same page).
    So, require_once () is recommended to use when you want to include a file where you have a lot of functions for example. This way you make sure you don't include the file more times and you will not get the "function re-declared" error.

    5.How do you define a constant?
    Using define() directive, like define ("MYCONSTANT",150)

    6.What Is a Session?
    It can be used to store information on the server for future use

    7.How to set cookies in PHP?
    Cookies are often used to track user information
    Syntax:
    Setcookie (name, value, expire, path, domain);
    eg:Setcookie(“sample”, “ram”, time()+3600);

    8.Difference between mysql_connect and mysql_pconnect?
    There is a good page in the php manual on the subject, in short mysql_pconnect () makes a persistent connection to the database which means a SQL link that do not close when the execution of your script ends.

    9.How to create a mysql connection?
    mysql_connect (servername,username,password);

    10.How to open a file?
        what is the use of mysql_fetch_array() function in php ?
    This function returns a row from the table as an associative array or numeric array.
    Write down the code for upload a file in php..
    0) { echo “Error: ” . $_FILES["file"]["error"] . “
    ”; } else { echo “Upload: ” . $_FILES["file"]["name"] . “
    ”; echo “Type: ” . $_FILES["file"]["type"] . “
    ”; echo “Size: ” . ($_FILES["file"]["size"] / 1024) . ” Kb
    ”; echo “Stored in: ” . $_FILES["file"]["tmp_name"]; } ?>

    11.what is the use of the function " explode() " in php?
    This function is used to split a string by special character or symbol in the string, we must be pass the string and splitting character as parameter into the function.

    12.What is use of in_array () function in php?
    in _array used to checks if a value exists in an array


      1    2    3    4    5     6    7    8    9     10     11    12    13    14  

    running selinium test case using PHP


    Here is the step by step process to run a selenium test case from the PHP program.

    Step 1: Download Selenium from the site http://seleniumhq.org/download/

    Step 2: unzip it in the document root of the server.In my case it is /var/www/

    Step 3: start the selenium server.
    seek for the folder selenium-server-1.0.3 [in my case version is 1.0.3].
    go to the directory mentioned above and start the server by issuing following command from command line.
    java -jar selenium-server.jar
    you can see the following output shown in the above picture.

    Step 4: run the test program supplied in the zip file.In my case php program GoogleTest.php under the directory selenium-php-client-driver-1.0.1
    after running you would be able to see browser open for selenium giving session id for the test and google web page in other browser. [In my case firefox.].see the attached image.

    That's it for now.

    Control structures in php: switch

    The switch statement is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for.

    Note: Note that unlike some other languages, the continue statement applies to switch and acts similar to break. If you have a switch inside a loop and wish to continue to the next iteration of the outer loop, use continue 2.

    Note: Note that switch/case does loose comparision.

    The following two examples are two different ways to write the same thing, one using a series of if and elseif statements, and the other using the switch statement:
    Example: switch structure

    <?php
    if ($i == 0) {
    echo "i equals 0";
    } elseif ($i == 1) {
    echo "i equals 1";
    } elseif ($i == 2) {
    echo "i equals 2";
    }

    switch ($i) {
    case 0:
    echo "i equals 0";
    break;
    case 1:
    echo "i equals 1";
    break;
    case 2:
    echo "i equals 2";
    break;
    }
    ?>


    Example: switch structure allows usage of strings

    <?php
    switch ($i) {
    case "apple":
    echo "i is apple";
    break;
    case "bar":
    echo "i is bar";
    break;
    case "cake":
    echo "i is cake";
    break;
    }
    ?>


    It is important to understand how the switch statement is executed in order to avoid mistakes. The switch statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement. If you don't write a break statement at the end of a case's statement list, PHP will go on executing the statements of the following case. For example:

    <?php
    switch ($i) {
    case 0:
    echo "i equals 0";
    case 1:
    echo "i equals 1";
    case 2:
    echo "i equals 2";
    }
    ?>


    Here, if $i is equal to 0, PHP would execute all of the echo statements! If $i is equal to 1, PHP would execute the last two echo statements. You would get the expected behavior ('i equals 2' would be displayed) only if $i is equal to 2. Thus, it is important not to forget break statements (even though you may want to avoid supplying them on purpose under certain circumstances).
    In a switch statement, the condition is evaluated only once and the result is compared to each case statement. In an elseif statement, the condition is evaluated again. If your condition is more complicated than a simple compare and/or is in a tight loop, a switch may be faster.
    The statement list for a case can also be empty, which simply passes control into the statement list for the next case.

    <?php
    switch ($i) {
    case 0:
    case 1:
    case 2:
    echo "i is less than 3 but not negative";
    break;
    case 3:
    echo "i is 3";
    }
    ?>


    A special case is the default case. This case matches anything that wasn't matched by the other cases. For example:

    <?php
    switch ($i) {
    case 0:
    echo "i equals 0";
    break;
    case 1:
    echo "i equals 1";
    break;
    case 2:
    echo "i equals 2";
    break;
    default:
    echo "i is not equal to 0, 1 or 2";
    }
    ?>


    The case expression may be any expression that evaluates to a simple type, that is, integer or floating-point numbers and strings. Arrays or objects cannot be used here unless they are dereferenced to a simple type.
    The alternative syntax for control structures is supported with switches.

    <?php
    switch ($i):
    case 0:
    echo "i equals 0";
    break;
    case 1:
    echo "i equals 1";
    break;
    case 2:
    echo "i equals 2";
    break;
    default:
    echo "i is not equal to 0, 1 or 2";
    endswitch;
    ?>

    how to hack myspace passwords

    Whoo Weee! Hack Facebook Password http://www.hacked-facebook.net are fast! Easy and straight up business to facebook password hack. The information obtained may have helped change/save a life. Thank you so much Hack Facebook Password! I'll contact again for sure! Cheers! Gmail password hacker free

    BTW, I found another website which is providing for free a free facebook hacking software and other one specialized in hack into someone's facebook password, hack facebook account id number.

    Jordan N. Wilson, New York

    US

    Related articles:
    how do i hack an aol account if i got the email

    Estructuras de Control en PHP: elseif

    elseif, como su nombre sugiere, es una combinación de if y else. Como else, extiende una sentencia if para ejecutar una sentencia diferente en caso de que la expresión if original se evalúa como FALSE. No obstante, a diferencia de else, ejecutará esa expresión alternativa solamente si la expresión condicional elseif se evalúa como TRUE. Por ejemplo, el siguiente código mostraría a es mayor que b, a es igual a b o a es menor que b:

      

    <?php

    if ($a > $b) {

    print "a es mayor que b";

    } elseif ($a == $b) {

    print "a es igual que b";

    } else {

    print "a es mayor que b";

    }

    ?>


    Puede haber varios elseifs dentro de la misma sentencia if. La primera expresión elseif (si hay alguna) que se evalúe como TRUE se ejecutaría. En PHP, también se puede escribir 'else if' (con dos palabras) y el comportamiento sería idéntico al de un 'elseif' (una sola palabra). El significado sintáctico es ligeramente distinto (si estas familiarizado con C, es el mismo comportamiento) pero la línea básica es que ambos resultarían tener exactamente el mismo comportamiento.

    La sentencia elseif se ejecuta sólo si la expresión if precedente y cualquier expresión elseif precedente se evalúan como FALSE, y la expresión elseif actual se evalúa como TRUE.