Developers Archive for December, 2006

Building an Iterator with PHP

Building an Iterator with PHP Friday, December 29th, 2006

Interface

An Iterator has a very simple, and at many times, clean interface.

<?php
function Iterator($array) // Constructor. Takes array to be traversed as a parameter.
function reset() // Sets internal pointer to the first element
function end() // Sets internal pointer to the last element
function seek($position) // Sets internal pointer to desired position
function next() // Returns next element in the iteration
function previous() // Returns previous element in the iteration
?>

With an interface like this, you can easily perform all your daily tasks. An example would be traversing arrays in any way you want and from any position you want. One advantage of using this approach against native PHP array functions is that you have one interface for all of your array tasks. You will not use the foreach() construct for one case, list-each combination in the other and the next() and prev() functions in third any more. Another advantage is that now you can easily set the position on any particular element and start traversing from there in any way you want. Here are few code examples:

<?php
// $array = ?. // initialize the array
$iterator = new Iterator($array);
// traverse the array
while ($elem = $iterator->next()) {
echo $elem;
}
// traverse array in reverse order
$iterator->end();
while ($elem = $iterator->previous()) {
echo $elem;
}
// traverse array from fifth element on
$iterator->seek(5);
while ($elem = $iterator->next()) {
echo $elem;
}
?>

“OK” you say, “this is all nice but there is nothing I can’t do with a combination of native PHP functions”.

Besides the fact that you are accessing all your arrays through a unique interface. Another and the most important advantage is that the Iterator’s object structure allows you to easily expand its functionality.

Calculating page loading time in PHP

Calculating page loading time in PHP Friday, December 29th, 2006

Description

Outputs the time in seconds that it takes for a PHP page to load.

Sample code

<?php

// Insert this block of code at the very top of your page:

$time = microtime();
$time = explode(” “, $time);
$time = $time[1] + $time[0];
$start = $time;
// All main codes comes here……….
// Place this part at the very end of your page

$time = microtime();
$time = explode(” “, $time);
$time = $time[1] + $time[0];
$finish = $time;
$totaltime = ($finish - $start);
printf (”This page took %f seconds to load.”, $totaltime);

?>

 

How to make a change to dates by using functions and operators in Microsoft Access

How to make a change to dates by using functions and operators in Microsoft Access Thursday, December 28th, 2006

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).
Date() function

The Date() function returns the current date in the short-date format.

Eg: 12/28/2006

Now() function

The Now() function returns the current date with the time.

Eg: 12/28/2006 04:46:50 PM

Format() function

use the Format() function with date values to specify the date format that you want to use for the date.

Eg: Format(Now() , “mmmm dd yyyy”)

The above example returns the current date in the format

December 28 2006

Day() function, WeekDay() function, Month() function, and Year() function

Eg: select Day(BirthDate) , WeekDay(BirthDate) , Month(BirthDate) , Year(BirthDate) from tblUser

The above query returns the day of birth, week of birth, month of birth and year of birth for each user from the table tblUsers. Note that the WeekDay() function returns a numeric value that indicates the day of the week.

DatePart() function

The DatePart() function can be used to extract the part of the specified date, such as the day, the month, or the year.

Eg: select DatePart(”yyyy”, BirthDate) from tblUsers

The above query returns the year component of the specified date.

DateDiff() function

The DateDiff() function returns the difference or the time lag between the two specified date values (in terms of the day, the month, the year, or the time units, such as hours, minutes, and seconds).

Eg: select DateDiff(”yyyy”, BirthDate, Date()) as Age from tblUsers

The above query returns the difference of years between the birthdate of the user and the current date.

DateAdd() function

The DateAdd() function increments a date by a specified number of time units, such as a day, a month, or a year and then returns the resultant value.
Eg: DateAdd(”yyyy”, 10, BirthDate)

The above example will add 10 years to the birthdate
DateValue() function

The DateValue() function verifies whether the input string is a valid date. If the input string is recognized as a valid date, the date is returned in short-date format. If the input string is not recognized as a valid date, the statement “Data type mismatch in criteria expression” is returned. The DateValue() function recognizes a variety of date formats, such as mm dd yyyy, dd mm yyyy, dd mmm yyyy, and dd mmm yyyy hh:mm:ss long date format.

Eg: SELECT DateValue(”28 Dec 2006″) AS ValidDate; 

DateSerial() function

The DateSerial() function returns the date value for the specified input parameters of year, month, and day. The input parameters can be expressions that involve arithmetical operations. The DateSerial() function evaluates the expressions in the input parameters before it returns the resultant date value.
Eg: SELECT DateSerial( 2006,  05, 1-1);

This example query returns the last day in the month of April for the year 2006. The last input parameter for the day with the value of 1 is decremented by 1. The result is that the month parameter is evaluated to 4.

WeekdayName() function

Eg: SELECT WeekdayName(1, False, 1) AS FirstWeekDayName;

The WeekdayName() function returns a string that indicates the day of the week, as specified in the first parameter. The day of the week string that is returned depends on the third parameter. This parameter sets the first day of the week. The second parameter is set to False to specify that the weekday name must not be abbreviated.
MonthName() function

SELECT MonthName(1)

The MonthName() function returns a string that indicates the month name for the specified month number from 1 through 12. The input parameter can also be an expression, as in the following query:

Eg: SELECT MonthName( DatePart(”m”, Date()) );

This query returns the name of the current month.

Using Comparison Operators With Date Values

You can use the following comparison operators to compare date values in expressions and in queries:

<(less than)

>(greater than)

<=(less than or equal to)

>=(greater than or equal to)

<>(not equal)

SELECT * FROM tblUsers
WHERE BirthDate >= DateValue(” 10/01/1963″)
AND BirthDate <= DateValue(”12/31/1973″);

This query uses the >= comparison operator and the <= comparison operator to verify whether the birth date of the user falls in the range of the two specified dates.

 

 

 

 

 


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.