Entries by admin

microtime()

April 11, 2011 | Filed Under Date and Time, PHP Tutorial | Leave a Comment

This function returns the current Unix timestamp with microseconds.

Syntax: microtime(get_as_float)

Description:

The argument “get_as_float” is Optional. When set to true it specifies that the function should return a float, instead of a string.

Note: This function returns the string “microsec sec”, where sec is the number of seconds since the Unix Epoch (0:00:00 January 1, 1970 GMT),and microsec is the microseconds part. Notice that both parts are returned in units of seconds.

Note: The get_as_float parameter was added to PHP 5!

Example:
Code:

echo(microtime());

Output:

0.03691400 1302507097

Article written by admin

localtime()

April 11, 2011 | Filed Under Date and Time, PHP Tutorial | Leave a Comment

The function returns an array that contains the time components of a Unix timestamp.

Syntax: localtime(timestamp,is_associative)

Description:

The first argument “timestamp” is Optional. It specifies the date or time to be formatted. If no timestamp is specified, it uses the current local time.

The second argument “is_associative” is Optional. It specifies whether to return an associative or indexed array. If set to false the array returned is an indexed array. If set to true then the array returned is an associative array.

The keys of the associative array are:

* [tm_sec] – seconds
* [tm_min] – minutes
* [tm_hour] – hour
* [tm_mday] – day of the month
* [tm_mon] – month of the year (January=0)
* [tm_year] – Years since 1900
* [tm_wday] – Day of the week (Sunday=0)
* [tm_yday] – Day of the year
* [tm_isdst] – Is daylight savings time in effect

Example:
Code:

$localtime  = localtime();
$localtime_assoc = localtime(time(), true);
print_r($localtime);
print_r($localtime_assoc);

Output:

Array (
[0] => 6
[1] => 27
[2] => 6
[3] => 11
[4] => 3
[5] => 111
[6] => 1
[7] => 100
[8] => 0
)

Array (
[tm_sec] => 6
[tm_min] => 27
[tm_hour] => 6
[tm_mday] => 11
[tm_mon] => 3
[tm_year] => 111
[tm_wday] => 1
[tm_yday] => 100
[tm_isdst] => 0
)

Article written by admin

gmmktime()

April 10, 2011 | Filed Under Date and Time, PHP Tutorial | Leave a Comment

This function returns a Unix timestamp for a GMT date.

Syntax:gmmktime(hour,minute,second,month,day,year,is_dst)

Description:

The first argument “hour” is Optional. It specifies the hour.

The second argument “minute” is Optional. It specifies the minute.

The third argument “second” is Optional. It specifies the second.

The fourth argument “month” is Optional. It specifies the numerical month.

The five argument “day” is Optional. It specifies the day.

The six argument “year” is Optional. It specifies the year. The valid range for year is on some systems between 1901 and 2038. However this limitation is overcome in PHP5

The sevent argument “is_dst” is Optional. It Set this parameter to 1 if the time is during daylight savings time (DST), 0 if it is not, or -1 (the default) if it is unknown. If it’s unknown, PHP tries to find out itself (which may cause unexpected results).

Note: This function is identical to mktime() except the passed parameters represents a GMT date.

Example:
Code:

$time = gmmktime(0,0,0,4,11,2011);
print($time . "");
print(date("M-d-Y",$time));

Output:

1302480000
Apr-11-2011

Article written by admin

gmdate()

April 10, 2011 | Filed Under Date and Time, PHP Tutorial | Leave a Comment

This function formats a GMT/UTC date/time.

Syntax: gmdate(format,timestamp)

Description:

The first argument “format” is Required. It specifies how to return the result:

* d – The day of the month (from 01 to 31)
* D – A textual representation of a day (three letters)
* j – The day of the month without leading zeros (1 to 31)
* l (lowercase ‘L’) – A full textual representation of a day
* N – The ISO-8601 numeric representation of a day (1 for Monday through 7 for Sunday)
* S – The English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works well with j)
* w – A numeric representation of the day (0 for Sunday through 6 for Saturday)
* z – The day of the year (from 0 through 365)
* W – The ISO-8601 week number of year (weeks starting on Monday)
* F – A full textual representation of a month (January through December)
* m – A numeric representation of a month (from 01 to 12)
* M – A short textual representation of a month (three letters)
* n – A numeric representation of a month, without leading zeros (1 to 12)
* t – The number of days in the given month
* L – Whether it’s a leap year (1 if it is a leap year, 0 otherwise)
* o – The ISO-8601 year number
* Y – A four digit representation of a year
* y – A two digit representation of a year
* a – Lowercase am or pm
* A – Uppercase AM or PM
* B – Swatch Internet time (000 to 999)
* g – 12-hour format of an hour (1 to 12)
* G – 24-hour format of an hour (0 to 23)
* h – 12-hour format of an hour (01 to 12)
* H – 24-hour format of an hour (00 to 23)
* i – Minutes with leading zeros (00 to 59)
* s – Seconds, with leading zeros (00 to 59)
* e – The timezone identifier (Examples: UTC, Atlantic/Azores)
* I (capital i) – Whether the date is in daylights savings time (1 if Daylight Savings Time, 0 otherwise)
* O – Difference to Greenwich time (GMT) in hours (Example: +0100)
* T – Timezone setting of the PHP machine (Examples: EST, MDT)
* Z – Returns 0
* c – The ISO-8601 date (e.g. 2004-02-12T15:19:21+00:00)
pills online pharmacy * r – The RFC 2822 formatted date (e.g. Thu, 21 Dec 2000 16:01:07 +0200)
* U – The seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)

The second argument “timestamp” is Optional.

Example:
Code:

echo date("M d Y H:i:s", mktime(0, 0, 0, 1, 1, 2011))."
"; echo gmdate("M d Y H:i:s", mktime(0, 0, 0, 1, 1, 2011));

Output:

Jan 01 2011 00:00:00
Jan 01 2011 00:00:00

Explanation:

This function is Identical to the date() function except that the time returned is Greenwich Mean Time (GMT) when this code is run in Finland (GMT +0200), the first line below prints “Jan 01 2011 00:00:00″, while the second prints “Dec 31 2011 22:00:00″.

Article written by admin

date_default_timezone_set()

April 10, 2011 | Filed Under Date and Time, PHP Tutorial | Leave a Comment

This function sets the default timezone used by all date/time functions.

Syntax: date_default_timezone_set(timezone)

Description:

The argument “timezone” is Required. The timezone identifier,like “UTC” or “Europe/Paris”.

Example:
Code:

echo(date_default_timezone_set("UTC"));

Output:

1

Article written by admin

date_default_timezone_get()

April 10, 2011 | Filed Under Date and Time, PHP Tutorial | Leave a Comment

This function returns the default timezone used by all date/time functions in a script.

Syntax:date_default_timezone_get(void)

Description:

The argument “void” is Optional.

Example:
Code:

echo(date_default_timezone_get());

Output:

UTC

Article written by admin

date()

April 10, 2011 | Filed Under Date and Time, PHP Tutorial | Leave a Comment

This function Formats a local time/date.

Syntax: date ( string format, timestamp)

Description:

The first argument “format” is Required. It specifies how to return the result:

* d – The day of the month (from 01 to 31)
* D – A textual representation of a day (three letters)
* j – The day of the month without leading zeros (1 to 31)
* l (lowercase ‘L’) – A full textual representation of a day
* N – The ISO-8601 numeric representation of a day (1 for Monday through 7 for Sunday)
* S – The English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works well with j)
* w – A numeric representation of the day (0 for Sunday through 6 for Saturday)
* z – The day of the year (from 0 through 365)
* W – The ISO-8601 week number of year (weeks starting on Monday)
* F – A full textual representation of a month (January through December)
* m – A numeric representation of a month (from 01 to 12)
* M – A short textual representation of a month (three letters)
* n – A numeric representation of a month, without leading zeros (1 to 12)
* t – The number of days in the given month
* L – Whether it’s a leap year (1 if it is a leap year, 0 otherwise)
* o – The ISO-8601 year number
* Y – A four digit representation of a year
* y – A two digit representation of a year
* a – Lowercase am or pm
* A – Uppercase AM or PM
* B – Swatch Internet time (000 to 999)
* g – 12-hour format of an hour (1 to 12)
* G – 24-hour format of an hour (0 to 23)
* h – 12-hour format of an hour (01 to 12)
* H – 24-hour format of an hour (00 to 23)
* i – Minutes with leading zeros (00 to 59)
* s – Seconds, with leading zeros (00 to 59)
* e – The timezone identifier (Examples: UTC, Atlantic/Azores)
* I (capital i) – Whether the date is in daylights savings time (1 if Daylight Savings Time, 0 otherwise)
* O – Difference to Greenwich time (GMT) in hours (Example: +0100)
* T – Timezone setting of the PHP machine (Examples: EST, MDT)
* Z – Timezone offset in seconds. The offset west of UTC is negative, and the offset east of UTC is positive (-43200 to 43200)
* c – The ISO-8601 date (e.g. 2004-02-12T15:19:21+00:00)
* r – The RFC 2822 formatted date (e.g. Thu, 21 Dec 2000 16:01:07 +0200)
* U – The seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)

timestamp Optional.

Example:
Code:


// Prints  like: Monday
echo date("l")."
"; // Prints like: Monday 10th of April 2011 03:12:46 PM echo date('l dS \of F Y h:i:s A')."
"; // Prints: April 8, 2011 is on a Friday echo "April 8, 2011 is on a " . date("l", mktime(0, 0, 0, 4, 1, 2011))."
";

Output:

Monday
Monday 11th of April 2011 03:46:03 AM
April 8, 2011 is on a Monday

Article written by admin

gettimeofday()

April 10, 2011 | Filed Under Date and Time, PHP Tutorial | Leave a Comment

This function returns an array that contains current time information.

The meaning of the returning array keys:

* [sec] – seconds since the Unix Epoch
* [usec] – microseconds
* [minuteswest] – minutes west of Greenwich
* [dsttime] – type of dst correction

Syntax: gettimeofday(return_float)

Description:

The argument “return_float” is Optional. It Makes gettimeofday() return a float when it is set to true.

Example:
Code:

Array
(
[sec] => 1328530803
[usec] => 166461
[minuteswest] => 0
[dsttime] => 0
)
1328530803.1665

Output:

Array (
[sec] => 1302433638
[usec] => 351183
[minuteswest] => 0
[dsttime] => 0
)
1302433638.3516

Article written by admin

getdate()

April 10, 2011 | Filed Under Date and Time, PHP Tutorial | Leave a Comment

This function returns an array that contains date and time information for a Unix timestamp.

The returning array contains ten elements with relevant information needed when formatting a date string:

* [seconds] – seconds
* [minutes] – minutes
* [hours] – hours
* [mday] – day of the month
* [wday] – day of the week
* [year] – year
* [yday] – day of the year
* [weekday] – name of the weekday
* [month] – name of the month

Syntax: getdate(timestamp)

Description:

The argument “timestamp” is Optional. It specifies the time in Unix time format.

Example:
Code:

$today = getdate();
print_r($today);

Output:

Array (
[seconds] => 49
[minutes] => 57
[hours] => 10
[mday] => 10
[wday] => 0
[mon] => 4
[year] => 2011
[yday] => 99
[weekday] => Sunday
[month] => April
[0] => 1302433069
)

Article written by admin

strftime()

April 10, 2011 | Filed Under Date and Time, PHP Tutorial | Leave a Comment

This function formats a local time or date according to locale settings.

Syntax: strftime(format,timestamp)

Description:

The first argument “format” is Required. It specifies how to return the result:

* %a – abbreviated weekday name
* %A – full weekday name
* %b – abbreviated month name
* %B – full month name
* %c – preferred date and time representation
* %C – century number (the year divided by 100, range 00 to 99)
* %d – day of the month (01 to 31)
* %D – same as %m/%d/%y
* %e – day of the month (1 to 31)
* %g – like %G, but without the century
* %G – 4-digit year corresponding to the ISO week number (see %V).
* %h – same as %b
* %H – hour, using a 24-hour clock (00 to 23)
* %I – hour, using a 12-hour clock (01 to 12)
* %j – day of the year (001 to 366)
* %m – month (01 to 12)
* %M – minute
* %n – newline character
* %p – either am or pm according to the given time value
* %r – time in a.m. and p.m. notation
* %R – time in 24 hour notation
* %S – second
* %t – tab character
* %T – current time, equal to %H:%M:%S
* %u – weekday as a number (1 to 7), Monday=1. Warning: In Sun Solaris Sunday=1
* %U – week number of the current year, starting with the first Sunday as the first day of the first week
* %V – The ISO 8601 week number of the current year (01 to 53), where week 1 is the first week that has at least 4 days in the current year, and with Monday as the first day of the week
* %W – week number of the current year, starting with the first Monday as the first day of the first week
* %w – day of the week as a decimal, Sunday=0
* %x – preferred date representation without the time
* %X – preferred time representation without the date
* %y – year without a century (range 00 to 99)
* %Y – year including the century
* %Z or %z – time zone or name or abbreviation
* %% – a literal % character

The second argument “timestamp” is Optional. It specifies the date or time to be formatted. If no timestamp is specified, it uses the current local time.

Note: This function is identical to gmstrftime() except that the time returned is local time.

Example:
Code:

echo(strftime("%b %d %Y %X", mktime(20,0,0,12,30,11))."");
echo(gmstrftime("%b %d %Y %X", mktime(20,0,0,12,30,11))."");
//Print the current date, time, and time zone.
echo(gmstrftime("It is %a on %b %d, %Y, %X time   zone: %Z",time()));

Output:

Dec 30 2011 20:00:00
Dec 30 2011 20:00:00
It is Sun on Apr 10, 2011, 10:50:01 time zone: India Standard Time

Article written by admin

strptime()

April 10, 2011 | Filed Under Date and Time, PHP Tutorial | Leave a Comment

This function parses a time/date generated with strftime().

This function returns an array with the date parsed. The meaning of the returning array keys are:

* [tm_sec] – seconds (0-61)
* [tm_min] – minutes (0-59)
* [tm_hour] – hour (0-23)
* [tm_mday] – day of the month (1-31)
* [tm_mon] – months since January (0-11)
* [tm_year] – years since 1900
* [tm_wday] – days since Sunday (0-6)
* [tm_yday] – days since January 1 (0-365)
* [unparsed] – the date part which was not recognized using the specified format, if any

Syntax: strptime(date,format)

Description:

The first argument “date” is Required. The string to parse.
The second argument “format” is Required. It specifies the format used in the date:

* %a – abbreviated weekday name
* %A – full weekday name
* %b – abbreviated month name
* %B – full month name
* %c – preferred date and time representation
* %C – century number (the year divided by 100, range 00 to 99)
* %d – day of the month (01 to 31)
* %D – same as %m/%d/%y
* %e – day of the month (1 to 31)
* %g – like %G, but without the century
* %G – 4-digit year corresponding to the ISO week number (see %V).
* %h – same as %b
* %H – hour, using a 24-hour clock (00 to 23)
* %I – hour, using a 12-hour clock (01 to 12)
* %j – day of the year (001 to 366)
* %m – month (01 to 12)
* %M – minute
* %n – newline character
* %p – either am or pm according to the given time value
* %r – time in a.m. and p.m. notation
* %R – time in 24 hour notation
* %S – second
* %t – tab character
* %T – current time, equal to %H:%M:%S
* %u – weekday as a number (1 to 7), Monday=1. Warning: In Sun Solaris Sunday=1
* %U – week number of the current year, starting with the first Sunday as the first day of the first week
* %V – The ISO 8601 week number of the current year (01 to 53), where week 1 is the first week that has at least 4 days in the current year, and with Monday as the first day of the week
* %W – week number of the current year, starting with the first Monday as the first day of the first week
* %w – day of the week as a decimal, Sunday=0
* %x – preferred date representation without the time
* %X – preferred time representation without the date
* %y – year without a century (range 00 to 99)
* %Y – year including the century
* %Z or %z – time zone or name or abbreviation
* %% – a literal % character

Note: This function is not implemented on Windows platforms.

Example:
Code:

$format = '%d/%m/%Y %H:%M:%S';
$strf = strftime($format);

echo "$strf\n";

print_r(strptime($strf, $format));

Output:

10/04/2011 10:37:40

Array
(
[tm_sec] => 19
[tm_min] => 54
[tm_hour] => 15
[tm_mday] => 3
[tm_mon] => 9
[tm_year] => 104
[tm_wday] => 0
[tm_yday] => 276
[unparsed] =>
)

Article written by admin

strtotime()

April 10, 2011 | Filed Under Date and Time, PHP Tutorial | Leave a Comment

This function parses an English textual date or time into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).

Syntax: strtotime(time,now)

Description:

The first argument “time” is Required. It specifies the time string to parse.

The second argument “now” is Optional. It specifies the timestamp used to calculate the returned value. If this parameter is omitted, current time is used.

Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.

This function returns false on failure.

Example:
Code:

echo strtotime("now")."
"; echo strtotime("9 September 2011")."
"; echo strtotime("+1 day")."
"; echo strtotime("+1 week")."
"; echo strtotime("+1 week 2 days 4 hours 2 seconds")."
"; echo strtotime("next Thursday")."
"; echo strtotime("last Monday");

Output:

1302430839
1315526400
1302517240
1303035640
1303222842
1302739200
1301875200

Article written by admin

date_sunrise()

April 10, 2011 | Filed Under Date and Time | Leave a Comment

This function returns the time of sunrise for a particular day and location.

Syntax: date_sunrise(timestamp,format,latitude,longitude,zenith,gmt_offset)

Description:

The first argument “timestamp” is Required.

The second argument “format” is Optional. It specifies how to return the result:

* SUNFUNCS_RET_STRING (returns the result as string. e.g. 16:46)
* SUNFUNCS_RET_DOUBLE (returns the result as float. e.g. 16.78243132)
* SUNFUNCS_RET_TIMESTAMP (returns the result as integer (timestamp). e.g. 1095034606)

The third argument “latitude” is Optional. It specifies the latitude of the location. The latitude defaults to North. If you want to specify a South value, you must pass a negative value.

The fourth argument “longitude” is Optional. It specifies the longitude of the location. The longitude defaults to East. If you want to specify a West value, you must pass a negative value.

The fifth argument “zenith” is Optional.

The sixth argument gmt_offset is Optional. It specifies the difference between GMT and local time in hours.

Example:
Code:


/* calculate the sunrise time for Lisbon, Portugal
Latitude: 38.4 North
Longitude: 9 West
Zenith ~= 90
offset: +1 GMT
*/

echo  date("D M d Y"). ', sunrise time : ' .date_sunrise(time(), SUNFUNCS_RET_STRING, 38.4, -9, 90, 1);

Output:

Sun Apr 10 2011, sunrise time : 07:09

Article written by admin

date_sunset()

April 10, 2011 | Filed Under Date and Time, PHP Tutorial | Leave a Comment

This function returns the time of sunset for a particular day and location.

Syntax: date_sunset(timestamp,format,latitude,longitude,zenith,gmt_offset)

Description:

The first argument “timestamp” is Required.

The second argument “format” is Optional. It specifies how to return the result:

* SUNFUNCS_RET_STRING (returns the result as string. e.g. 16:46)
* SUNFUNCS_RET_DOUBLE (returns the result as float. e.g. 16.78243132)
* SUNFUNCS_RET_TIMESTAMP (returns the result as integer (timestamp). e.g. 1095034606)

The third argument “latitude” is Optional. It specifies the latitude of the location. The latitude defaults to North. If you want to specify a South value, you must pass a negative value.

The fourth argument “longitude” is Optional. It specifies the longitude of the location. The longitude defaults to East. If you want to specify a West value, you must pass a negative value.

The fifth argument “zenith” is Optional.

The sixth argument “gmt_offset” is Optional. It specifies the difference between GMT and local time in hours.

Example:
Code:

//Calculate the sunset time for Lisbon, Portugal
//Latitude: 38.4 North
//Longitude: 9 West
//Zenith ~= 90
//offset: +1 GMT
echo("Date: " . date("D M d Y") . "");
echo("Sunset time: ");
echo(date_sunset(time(),SUNFUNCS_RET_STRING,38.4,-9,90,1));

Output:

Date: Sun Apr 10 2011
Sunset time: 20:04

Article written by admin

time()

April 10, 2011 | Filed Under Date and Time, PHP Tutorial | Leave a Comment

This function returns the current time as a (the number of seconds since January 1 1970 00:00:00 GMT).
Syntax: time(void)

Description:

The parameter “void” is Optional.

Example:
Code:

cheap diet pill  name="code" class="php">
$nextWeek Buy celebrex online  = time()  + (7 * 24 * 60 * 60);

echo  'Now:       '. date('Y-m-d') ."\n";
echo 'Next Week: '. date('Y-m-d', $nextWeek) ."\n";

Output:

Now: 2011-04-10

Next Buy VPXL Online Week: 2011-04-17

Article written by admin

checkdate()

April 10, 2011 | Filed Under Date and Time | Leave a Comment

This function returns true if the specified date is valid, and false otherwise.

A date is valid if:

* month is between 1 and 12 inclusive
* day is within the allowed number of days for the particular month
* year is between 1 and 32767 inclusive

Syntax: checkdate(month,day,year)

Description:

The first parameter “month” is Required. It Specifies the month.

The second parameter “day” is Required. It Specifies the day.
The first parameteryear Specifies the year

Example1:
Code:

if(checkdate(12, 30, 2010))
{
echo "The date is valid"."
"; } else { echo "The date is not valid"; }

Output:

The date is valid

Example2:
Code:

if(checkdate(2, 29, 2003))
{
echo   "The date is valid";

}
else
{
echo "The date is not valid";
}

Output:

The date is not valid

Article written by admin

include()

April 6, 2011 | Filed Under Control Structures, PHP Tutorial | Leave a Comment

include() generates a warning, but the script will continue execution.

Syntax: include(path)

Description:

The argument “path” is Required. It specifies the path to the file that is to be called.

Example:
Code:

include("abc.php");
echo  "Hello";

Output:

Warning: include(abc.php) [function.include]: failed to open stream: No such file or directory in C:\wamp\www\control structure\include.php on line 2

Warning: include() [function.include]: Failed opening ‘abc.php’ for inclusion (include_path=’.;C:\php5\pear’) in C:\wamp\www\control structure\include.php on line 2
Hello

Article written by admin

switch

April 6, 2011 | Filed Under Control Structures, PHP Tutorial | Leave a Comment

You can Use the switch statement to choose one of many blocks of code to be executed.

Syntax:

switch (n)
{
case Label1:
code to be executed if n=Label1;
break;
case Label2:
code to be executed if n=Label2;
break;
default:
code to be executed if n is different from both Label1 and Label2;
}

First we have a single expression “n”. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. You can Use break to prevent the code from running into the next case automatically. The default statement is used if no match is found.

Example:
Code:

$i=4;
switch ($i) {
case 0:
    echo "Tom";
    break;
case 1:
    echo "Lara";
    break;
case 2:
    echo "Lyod";
    break;
default:
    echo "Harper";
}

Output:

Harper

Article written by admin

continue

April 6, 2011 | Filed Under Control Structures, PHP Tutorial | Leave a Comment

This statement specifies to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.

Syntax: continue [n]

Description:

The argument “n” is Optional.It specifies the Number of levels to skip.
Example:
Code:


for  ($i = 0; $i < 10; $i++) {
   if ($i== 5)
      continue;
   echo "$i ";
}

Output:

0 1 2 3 4 6 7 8 9

Article written by admin

break

April 6, 2011 | Filed Under Control Structures, PHP Tutorial | Leave a Comment

break stops execution of the current for, foreach, while, do-while or switch structure.

Syntax: break;

Example:
Code:

$name = array('Tom', 'Ross',   'Smith',  'Taylor','Mahanama', 'Harsha');
while (list(, $val) = each($name)) {
    if ($val == 'Taylor') {
        break;
    }
    echo "$val\n";
}

Output:

Tom
Ross
Smith

Article written by admin

© PHPInterviewQuestion.com 2009 - 2012

eXTReMe Tracker