PHP Directory Introduction

Posted by admin | Posted in PHP Tutorial | Posted on 10-08-2010-05-2008

0

The directory functions allow you to retrieve information about directories and their contents.

PHP Date / Time Constants

Posted by admin | Posted in PHP Tutorial | Posted on 10-08-2010-05-2008

0

PHP: indicates the earliest version of PHP that supports the constant.

Constant Description PHP Version
DATE_ATOM Atom (example: 2005-08-15T16:13:03+0000)
DATE_COOKIE HTTP Cookies (example: Sun, 14 Aug 2005 16:13:03 UTC)
DATE_ISO8601 ISO-8601 (example: 2005-08-14T16:13:03+0000)
DATE_RFC822 RFC 822 (example: Sun, 14 Aug 2005 16:13:03 UTC)
DATE_RFC850 RFC 850 (example: Sunday, 14-Aug-05 16:13:03 UTC)
DATE_RFC1036 RFC 1036 (example: Sunday, 14-Aug-05 16:13:03 UTC)
DATE_RFC1123 RFC 1123 (example: Sun, 14 Aug 2005 16:13:03 UTC)
DATE_RFC2822 RFC 2822 (Sun, 14 Aug 2005 16:13:03 +0000)
DATE_RSS RSS (Sun, 14 Aug 2005 16:13:03 UTC)
DATE_W3C World Wide Web Consortium (example: 2005-08-14T16:13:03+0000)

PHP Date / Time Functions

Posted by admin | Posted in PHP Tutorial | Posted on 10-08-2010-05-2008

0

Function Description PHP Version
checkdate() Validates a Gregorian date 3
date_default_timezone_get() Returns the default time zone 5
date_default_timezone_set() Sets the default time zone 5
date_sunrise() Returns the time of sunrise for a given day / location 5
date_sunset() Returns the time of sunset for a given day / location 5
date() Formats a local time/date 3
getdate() Returns an array that contains date and time information for a Unix timestamp 3
gettimeofday() Returns an array that contains current time information 3
gmdate() Formats a GMT/UTC date/time 3
gmrmktime() Returns the Unix timestamp for a GMT date 3
gmstrftime() Formats a GMT/UTC time/date according to locale settings 3
idate() Formats a local time/date as integer 5
localtime() Returns an array that contains the time components of a Unix timestamp 4
microtime() Returns the microseconds for the current time 3
mktime() Returns the Unix timestamp for a date 3
strftime() Formats a local time/date according to locale settings 3
strptime() Parses a time/date generated with strftime() 5
strtotime() Parses an English textual date or time into a Unix timestamp 3
time() Returns the current time as a Unix timestamp 3

Runtime Configuration

Posted by admin | Posted in PHP Tutorial | Posted on 10-08-2010-05-2008

0

The behavior of the date/time functions is affected by settings in php.ini.

Date/Time configuration options:

Name Default Description Changeable
date.default_latitude “31.7667″ Specifies the default latitude (available since PHP 5). This option is used by date_sunrise() and date_sunset() PHP_INI_ALL
date.default_longitude “35.2333″ Specifies the default longitude (available since PHP 5). This option is used by date_sunrise() and date_sunset() PHP_INI_ALL
date.sunrise_zenith “90.83″ Specifies the default sunrise zenith (available since PHP 5). This option is used by date_sunrise() and date_sunset() PHP_INI_ALL
date.sunset_zenith “90.83″ Specifies the default sunset zenith (available since PHP 5). This option is used by date_sunrise() and date_sunset() PHP_INI_ALL
date.timezone “” Specifies the default timezone (available since PHP 5.1) PHP_INI_ALL

Installation

Posted by admin | Posted in PHP Tutorial | Posted on 10-08-2010-05-2008

0

The date/time functions are part of the PHP core. There is no installation needed to use these functions.

PHP Date / Time Introduction

Posted by admin | Posted in PHP Tutorial | Posted on 10-08-2010-05-2008

0

The date/time functions allow you to extract and format the date and time on the server.

Note: These functions depend on the locale settings of the server!

PHP Calendar Constants

Posted by admin | Posted in PHP Tutorial | Posted on 10-08-2010-05-2008

0

Constant Description PHP Version
CAL_GREGORIAN Gregorian calendar 3
CAL_JULIAN Julian calendar 3
CAL_JEWISH Jewish calendar 3
CAL_FRENCH French Republican calendar 3
CAL_NUM_CALS 3
CAL_DOW_DAYNO 3
CAL_DOW_SHORT 3
CAL_DOW_LONG 3
CAL_MONTH_GREGORIAN_SHORT 3
CAL_MONTH_GREGORIAN_LONG 3
CAL_MONTH_JULIAN_SHORT 3
CAL_MONTH_JULIAN_LONG 3
CAL_MONTH_JEWISH 3
CAL_MONTH_FRENCH 3
CAL_EASTER_DEFAULT 4
CAL_EASTER_DEFAULT 4
CAL_EASTER_ROMAN 4
CAL_EASTER_ALWAYS_GREGORIAN 4
CAL_EASTER_ALWAYS_JULIAN 4
CAL_JEWISH_ADD_ALAFIM_GERESH 5
CAL_JEWISH_ADD_ALAFIM 5
CAL_JEWISH_ADD_GERESHAYIM 5

PHP Calendar Functions

Posted by admin | Posted in PHP Tutorial | Posted on 10-08-2010-05-2008

0

Function Description PHP Version
cal_days_in_month() Returns the number of days in a month for a specified year and calendar 4
cal_from_jd() Converts a Julian day count into a date of a specified calendar 4
cal_info() Returns information about a given calendar 4
cal_to_jd() Converts a date to Julian day count 4
easter_date() Returns the Unix timestamp for midnight on Easter of a specified year 3
easterr_days() Returns the number of days after March 21, on which Easter falls for a specified year 3
FrenchToJD() Converts a French Republican date to a Julian day count 3
GregorianToJD() Converts a Gregorian date to a Julian day count 3
JDDayOfWeek() Returns the day of a week 3
JDMonthName() Returns a month name 3
JDToFrench() Converts a Julian day count to a French Republican date 3
JDToGregorian() Converts a Julian day count to a Gregorian date 3
jdtojewish() Converts a Julian day count to a Jewish date 3
JDToJulian() Converts a Julian day count to a Julian date 3
jdtounix() Converts a Julian day count to a Unix timestamp 4
JewishToJD() Converts a Jewish date to a Julian day count 3
JulianToJD() Converts a Julian date to a Julian day count 3
unixtojd() Converts a Unix timestamp to a Julian day count 4

Installation

Posted by admin | Posted in PHP Tutorial | Posted on 10-08-2010-05-2008

0

The windows version of PHP has built-in support for the calendar extension. So, the calendar functions will work automatically.

However, if you are running the Linux version of PHP, you will have to compile PHP with –enable-calendar to get the calendar functions to work.

PHP Calendar Functions

Posted by admin | Posted in PHP Tutorial | Posted on 10-08-2010-05-2008

0

PHP Calendar Introduction

The calendar functions are useful when working with different calendar formats. The standard it is based on is the Julian day count (Julian day count is a count of days starting from January 1, 4713 B.C.). Note that the Julian day count is not the same as the Julian calendar!

Note: To convert between calendar formats, you must first convert to Julian day count, then to the calendar format.

PHP Array Constants

Posted by admin | Posted in PHP Tutorial | Posted on 10-08-2010-05-2008

0

Constant Description PHP Version
CASE_LOWER Used with array_change_key_case() to convert array keys to lower case
CASE_UPPER Used with array_change_key_case() to convert array keys to upper case
SORT_ASC Used with array_multisort() to sort in ascending order
SORT_DESC Used with array_multisort() to sort in descending order
SORT_REGULAR Used to compare items normally
SORT_NUMERIC Used to compare items numerically
SORT_STRING Used to compare items as strings
SORT_LOCALE_STRING Used to compare items as strings, based on the current locale 4
COUNT_NORMAL
COUNT_RECURSIVE
EXTR_OVERWRITE
EXTR_SKIP
EXTR_PREFIX_SAME
EXTR_PREFIX_ALL
EXTR_PREFIX_INVALID
EXTR_PREFIX_IF_EXISTS
EXTR_IF_EXISTS
EXTR_REFS

Installation

Posted by admin | Posted in PHP Tutorial | Posted on 10-08-2010-05-2008

0

The array functions are part of the PHP core. There is no installation needed to use these functions.

PHP Array Introduction

Posted by admin | Posted in PHP Tutorial | Posted on 10-08-2010-05-2008

0

The array functions allow you to manipulate arrays.

PHP supports both simple and multi-dimensional arrays. There are also specific functions for populating arrays from database queries.

The Text File

Posted by admin | Posted in PHP Tutorial | Posted on 10-08-2010-05-2008

0

The text file (poll_result.txt) is where we store the data from the poll.

It is stored like this:

  • 0||0

The first number represents the “Yes” votes, the second number represents the “No” votes.

Note: Remember to allow your web server to edit the text file. Do NOT give everyone access, just the web server (PHP).

The PHP Page

Posted by admin | Posted in PHP Tutorial | Posted on 10-08-2010-05-2008

0

The server page called by the JavaScript code is a simple PHP file called “poll_vote.php”.

  • <?php
    $vote = $_REQUEST['vote'];
    //get content of textfile
    $filename = “poll_result.txt”;
    $content = file($filename);

    //put content in array
    $array = explode(“||”, $content[0]);
    $yes = $array[0];
    $no = $array[1];

    if ($vote == 0)
    {
    $yes = $yes + 1;
    }
    if ($vote == 1)
    {
    $no = $no + 1;
    }

    //insert votes to txt file
    $insertvote = $yes.”||”.$no;
    $fp = fopen($filename,”w”);
    fputs($fp,$insertvote);
    fclose($fp);
    ?>

    <h2>Result:</h2>
    <table>
    <tr>
    <td>Yes:</td>
    <td>
    <img src=”poll.gif”
    width=’<?php echo(100*round($yes/($no+$yes),2)); ?>’
    height=’20′>
    <?php echo(100*round($yes/($no+$yes),2)); ?>%
    </td>
    </tr>
    <tr>
    <td>No:</td>
    <td>
    <img src=”poll.gif”
    width=’<?php echo(100*round($no/($no+$yes),2)); ?>’
    height=’20′>
    <?php echo(100*round($no/($no+$yes),2)); ?>%
    </td>
    </tr>
    </table>

The selected value is sent from the JavaScript and the following happens:

  1. Get the content of the “poll_result.txt” file
  2. Put the content of the file in variables and add one to the selected variable
  3. Write the result to the “poll_result.txt” file
  4. Output a graphical representation of the poll result

Example Explained – The JavaScript code

Posted by admin | Posted in PHP Tutorial | Posted on 10-08-2010-05-2008

0

This is the JavaScript code stored in the file “poll.js”:

  • var xmlhttp;

    function getVote(int)
    {
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
    {
    alert (“Browser does not support HTTP Request”);
    return;
    }
    var url=”poll_vote.php”;
    url=url+”?vote=”+int;
    url=url+”&sid=”+Math.random();
    xmlhttp.onreadystatechange=stateChanged;
    xmlhttp.open(“GET”,url,true);
    xmlhttp.send(null);
    }

    function stateChanged()
    {
    if (xmlhttp.readyState==4)
    {
    document.getElementById(“poll”).innerHTML=xmlhttp.responseText;
    }
    }

    function GetXmlHttpObject()
    {
    var objXMLHttp=null;
    if (window.XMLHttpRequest)
    {
    objXMLHttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
    objXMLHttp=new ActiveXObject(“Microsoft.XMLHTTP”);
    }
    return objXMLHttp;

The stateChanged() and GetXmlHttpObject functions are the same as in the PHP AJAX Suggest chapter.

The getVote() Function

This function executes when “yes” or “no” is selected in the HTML form.

  1. Calls the GetXmlHttpObject() function to create an XMLHTTP object
  2. Defines the URL (filename) to send to the server
  3. Adds a parameter (vote) to the URL with the content of the input field
  4. Adds a random number to prevent the server from using a cached file
  5. Each time the readyState property changes, the stateChanged() function will be executed
  6. Opens the XMLHTTP object with the given url.
  7. Sends an HTTP request to the server

Example Explained – The HTML page

Posted by admin | Posted in PHP Tutorial | Posted on 10-08-2010-05-2008

0

The HTML page contains a link to an external JavaScript, an HTML form, and a div element:

  • <html>
    <head>
    <script src=”poll.js”></script>
    </head>
    <body>

    <div>
    <h3>Do you like PHP and AJAX so far?</h3>
    <form>
    Yes:
    <input value=”0″ onclick=”getVote(this.value)” />
    <br />No:
    <input value=”1″ onclick=”getVote(this.value)” />
    </form>
    </div>

    </body>
    </html>

The HTML form works like this:

  1. An event is triggered when the user selects the “yes” or “no” option
  2. When the event is triggered, the function getVote() is executed
  3. The data returned from the getVote() function will replace the form, in the <div> tag

AJAX Poll

Posted by admin | Posted in PHP Tutorial | Posted on 10-08-2010-05-2008

0

This example will demonstrate a poll where a web page can get results without reloading.

Do you like PHP and AJAX so far?

Yes:

No:

Example Explained – The PHP page

Posted by admin | Posted in PHP Tutorial | Posted on 10-08-2010-05-2008

0

The PHP page called by the JavaScript code is called “getrss.php”:

  • <?php
    //get the q parameter from URL
    $q=$_GET["q"];

    //find out which feed was selected
    if($q==”Google”)
    {
    $xml=(“http://news.google.com/news?ned=us&topic=h&output=rss”);
    }
    elseif($q==”MSNBC”)
    {
    $xml=(“http://rss.msnbc.msn.com/id/3032091/device/rss/rss.xml”);
    }

    $xmlDoc = new DOMDocument();
    $xmlDoc->load($xml);

    //get elements from “<channel>”
    $channel=$xmlDoc->getElementsByTagName(‘channel’)->item(0);
    $channel_title = $channel->getElementsByTagName(‘title’)
    ->item(0)->childNodes->item(0)->nodeValue;
    $channel_link = $channel->getElementsByTagName(‘link’)
    ->item(0)->childNodes->item(0)->nodeValue;
    $channel_desc = $channel->getElementsByTagName(‘description’)
    ->item(0)->childNodes->item(0)->nodeValue;

    //output elements from “<channel>”
    echo(“<p><a href=’” . $channel_link
    . “‘>” . $channel_title . “</a>”);
    echo(“<br />”);
    echo($channel_desc . “</p>”);

    //get and output “<item>” elements
    $x=$xmlDoc->getElementsByTagName(‘item’);
    for ($i=0; $i<=2; $i++)
    {
    $item_title=$x->item($i)->getElementsByTagName(‘title’)
    ->item(0)->childNodes->item(0)->nodeValue;
    $item_link=$x->item($i)->getElementsByTagName(‘link’)
    ->item(0)->childNodes->item(0)->nodeValue;
    $item_desc=$x->item($i)->getElementsByTagName(‘description’)
    ->item(0)->childNodes->item(0)->nodeValue;

    echo (“<p><a href=’” . $item_link
    . “‘>” . $item_title . “</a>”);
    echo (“<br />”);
    echo ($item_desc . “</p>”);
    }
    ?>

When an option is sent from the JavaScript, the following happens:

  1. PHP finds out which RSS feed was selected
  2. An XML DOM object is created for the selected RSS feed
  3. The elements from the RSS channel are found and outputted
  4. Loops through the first three elements and output result

Example Explained – The JavaScript code

Posted by admin | Posted in PHP Tutorial | Posted on 10-08-2010-05-2008

0

This is the JavaScript code stored in the file “getrss.js”:

  • var xmlhttp;

    function showRSS(str)
    {
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
    {
    alert (“Your browser does not support XML HTTP Request”);
    return;
    }
    var url=”getrss.php”;
    url=url+”?q=”+str;
    url=url+”&sid=”+Math.random();
    xmlhttp.onreadystatechange=stateChanged;
    xmlhttp.open(“GET”,url,true);
    xmlhttp.send(null);
    }

    function stateChanged()
    {
    if (xmlhttp.readyState==4)
    {
    document.getElementById(“rssOutput”).innerHTML=xmlhttp.responseText;
    }
    }

    function GetXmlHttpObject()
    {
    if (window.XMLHttpRequest)
    {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    return new XMLHttpRequest();
    }
    if (window.ActiveXObject)
    {
    // code for IE6, IE5
    return new ActiveXObject(“Microsoft.XMLHTTP”);
    }
    return null;
    }

The stateChanged() and GetXmlHttpObject functions are the same as in the PHP AJAX Suggest chapter.

The showRSS() Function

Every time an option is selected in the input field, this function executes the following:

  1. Calls the GetXmlHttpObject() function to create an XMLHTTP object
  2. Defines the URL (filename) to send to the server
  3. Adds a parameter (q) to the URL with the selected option from the drop-down list
  4. Adds a random number to prevent the server from using a cached file
  5. Each time the readyState property changes, the stateChanged() function will be executed
  6. Opens the XMLHTTP object with the given URL
  7. Sends an HTTP request to the server