PHP & MySQL OLD

From Script | Spoken-Tutorial
Jump to: navigation, search

PHP or "PHP: Hypertext Preprocessor" is a widely-used Open Source general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. Its syntax draws upon C, Java, and Perl, and is easy to learn. The main goal of the language is to allow web developers to write dynamically generated web pages quickly, but you can do much more with PHP. MySQL is a RDBMS(Relational database management system). We use such a system in order to store, retrieve and manage data related to our applications. Apache is an Open Source web server in wide usage. A web-server is required to host a web-application(webpage) on a remote server and is responsible for processing the request from the client, who wants to access the application(or webpage).

Contents

PHP

Basic Level

These tutorials would help you get started with PHP programming. In this section we would go through the basics of installing and getting PHP ready for development, and the basic syntax and features of the language.


/Introduction to PHP, XAMPP and MySQL/

  • What is each of the above
  • Why each of the above

/Setting up PHP,MySQL and Apache in Windows/

  • Minimum requirements of the system.
  • Installation and configuring PHP, Apache and MySQL (native installation)in Windows.
  • Using XAMPP and MySQL GUI Tools on Windows.

/Setting up PHP,MySQL and Apache in Linux/

  • Minimum requirements of the system.
  • Installation and configuring PHP, Apache and MySQL(native installation)in Linux.
  • Using XAMPP and MySQL GUI Tools on Linux.

/Hello World/

Procedure to embed into a HTML source. Introduction to echo command.

/Datatypes, variables and constants/

Introduction to the various datatypes Syntax for declaration and usage of variables and constants.

/String Operations /

String Function:-

2. PHP chr() Function

Definition and Usage

The chr() function returns a character from the specified ASCII value.

Syntax

chr(ascii)
Parameter Description
ascii Required. An ASCII value

Tips and Notes

Note: The x parameter can be specified in decimal, octal, or hex values. Octal values are defined by a leading 0, while hex values are defined by a leading 0x.

Example

<?phpecho chr(52)."<br />";echo chr(052)."<br />";echo chr(0x52)."<br />";?>

The output of the code above will be:

4. PHP print() Function

Definition and Usage

The print() function outputs one or more strings.

Syntax

print(strings)
Parameter Description
strings Required. One or more strings to be sent to the output

Tips and Notes

Note: The print() function is not actually a function, so you are not required to use parentheses with it.

Tip: The print() function is slightly slower than echo().

Example 1

<?php$str = "Who's Kai Jim?";print $str;print "<br />";print $str."<br />I don't know!";?>

The output of the code above will be:


Who's Kai Jim?Who's Kai Jim?I don't know!

Example 3

Difference of single and double quotes. Single quotes will print the variable name, not the value:


<?php$color = "red";print "Roses are $color";print "<br />";print 'Roses are $color';?>

The output of the code above will be:


Roses are redRoses are $color


5. PHP str_ireplace() Function

Definition and Usage

The str_ireplace() function replaces some characters with some other characters in a string.

Syntax

str_ireplace(find,replace,string,count)
Parameter Description
find Required. Specifies the value to find
replace Required. Specifies the value to replace the value in find
string Required. Specifies the string to be searched
count Optional. A variable that counts the number of replacements

Tips and Notes

Note: This function is case-insensitive. Use str_replace() to perform a case-sensitive search.

Note: This function is binary-safe.

Example 1

<?phpecho str_ireplace("WORLD","aaditya","Hello world!");?>

The output of the code above will be:


Hello aaditya!

Example 2

In this example we will demonstrate str_ireplace() with an array and a count variable:


<?php$arr = array("blue","red","green","yellow");print_r(str_ireplace("RED","pink",$arr,$i));echo "Replacements: $i";?>

The output of the code above will be:


Array([0] => blue[1] => pink[2] => green[3] => yellow)Replacements: 1


6. PHP str_pad() Function

Definition and Usage

The str_pad() function pads a string to a new length.

Syntax

str_pad(string,length,pad_string,pad_type)



Parameter Description
string Required. Specifies the string to pad
length Required. Specifies the new string length. If this value is less than the original length of the string, nothing will be done
pad_string Optional. Specifies the string to use for padding. Default is whitespace
pad_type Optional. Specifies what side to pad the string.

Possible values:

  • STR_PAD_BOTH - Pad to both sides of the string. If not an even number, the right side gets the extra padding
  • STR_PAD_LEFT - Pad to the left side of the string
  • STR_PAD_RIGHT - Pad to the right side of the string. This is default

 

Example 1

<?php$str = "Hello World";echo str_pad($str,20,".");?>

The output of the code above will be:


Hello World.........

Example 2

<?php$str = "Hello World";echo str_pad($str,20,".",STR_PAD_LEFT);?>

The output of the code above will be:


.........Hello World

Example 3

<?php$str = "Hello World";echo str_pad($str,20,".:",STR_PAD_BOTH);?>

The output of the code above will be:


.:.:Hello World.:.:.


7. PHP str_repeat() Function

Definition and Usage

The str_repeat() function repeats a string a specified number of times.

Syntax

str_repeat(string,repeat)
Parameter Description
string Required. Specifies the string to repeat
repeat Required. Specifies the number of times the string will be repeated. Must be greater or equal to 0

Example

<?phpecho str_repeat(".",13);?>

The output of the code above will be:


.............


8. PHP str_replace() Function

Definition and Usage

Syntax


str_replace(find,replace,string,count)
Parameter Description
find Required. Specifies the value to find
replace Required. Specifies the value to replace the value in find
string Required. Specifies the string to be searched
count Optional. A variable that counts the number of replacements

Tips and Notes

Note: This function is case-sensitive. Use str_ireplace() to perform a case-insensitive search.

Note: This function is binary-safe.

Example 1

<?phpecho str_replace("world","aaditya","Hello world!");?>

The output of the code above will be:


Hello aaditya!

Example 2

In this example we will demonstrate str_replace() with an array and a count variable:


<?php$arr = array("blue","red","green","yellow");print_r(str_replace("red","pink",$arr,$i));echo "Replacements: $i";?>

The output of the code above will be:


Array([0] => blue[1] => pink[2] => green[3] => yellow)Replacements: 1

9. PHP str_shuffle() Function

Definition and Usage

The str_shuffle() function randomly shuffles all the characters of a string.


Syntax : - str_shuffle(string)
Parameter Description
string Required. Specifies the string to shuffle

Example

<?phpecho str_shuffle("Hello World");?>

The output of the code above could be:


H leooWlrld

10. PHP str_word_count() Function

Definition and Usage

The str_word_count() function counts the number of words in a string.

Syntax

str_word_count(string,return)
Parameter Description
string Required. Specifies the string to check
return Optional. Specifies the return value of the str_word_count() function.

Possible values:

  • 0 - Default. Returns the number of words found
  • 1 - Returns an array with the words from the string
  • 2 - Returns an array where the key is the position of the word in the string, and value is the actual word


Example 1

<?phpecho str_word_count("Hello world!");?>

The output of the code above will be:


2

Example 2

<?phpprint_r(str_word_count("Hello world!",1));?>

The output of the code above will be:


Array([0] => Hello[1] => world)

Example 3

<?phpprint_r(str_word_count("Hello world!",2));?>

The output of the code above will be:


Array([0] => Hello[6] => world)

11. PHP strcasecmp() Function

Definition and Usage

The strcasecmp() function compares two strings.

This function returns:

  • 0 - if the two strings are equal
  • <0 - if string1 is less than string2
  • >0 - if string1 is greater than string2


Syntax ; -strcasecmp(string1,string2)
Parameter Description
string1 Required. Specifies the first string to compare
string2 Required. Specifies the second string to compare

Tips and Notes

Tip: The strcasecmp() function is binary safe and case-insensitive. USE strcmp for case-sensitive

Example

<?phpecho strcasecmp("Hello world!","HELLO WORLD!");?>

The output of the code above will be:


0

12. PHP strtolower() Function

Definition and Usage

The strtolower() function converts a string to lowercase.

Syntax

strtolower(string)
Parameter Description
String Required. Specifies the string to convert

Example

<?phpecho strtolower("Hello WORLD.");?>

The output of the code above will be:


hello world.

13. PHP strtoupper() Function

Definition and Usage

The strtoupper() function converts a string to uppercase.

Syntax

strtoupper(string)
Parameter Description
string Required. Specifies the string to convert

Example

<?phpecho strtoupper("Hello WORLD!");?>

The output of the code above will be:


HELLO WORLD!


14. PHP substr() Function

Definition and Usage

The substr() function returns a part of a string.

Syntax

substr(string,start,length)
Parameter Description
string Required. Specifies the string to return a part of
start Required. Specifies where to start in the string
  • A positive number - Start at a specified position in the string
  • A negative number - Start at a specified position from the end of the string
  • 0 - Start at the first character in string


length Optional. Specifies the length of the returned string. Default is to the end of the string.
  • A positive number - The length to be returned from the start parameter
  • Negative number - The length to be returned from the end of the string


Tips and Notes

Note: If start is a negative number and length is less than or equal to start, length becomes 0.

Example 1

<?phpecho substr("Hello world!",6);?>

The output of the code above will be:


world!

Example 2

<?phpecho substr("Hello world!",6,5);?>

The output of the code above will be:


world



15. PHP ucwords() Function

Definition and Usage

The ucwords() function converts the first character of each word in a string to uppercase.

Syntax

ucwords(string)
Parameter Description
string Required. Specifies the string to convert

Example

<?phpecho ucwords("hello world");?>

The output of the code above will be:


Hello World

16. PHP trim() Function

Definition and Usage

The trim() function removes whitespaces and other predefined characters from both sides of a string.

Syntax

trim(string,charlist)
Parameter Description
string Required. Specifies the string to check
charlist Optional. Specifies which characters to remove from the string. If omitted, all of the following characters are removed:
  • "\0" - NULL
  • "\t" - tab
  • "\n" - new line
  • "\x0B" - vertical tab
  • "\r" - carriage return
  • " " - ordinary white space


Example 1

<html><body><?php$str = " Hello World! ";echo "Without trim: " . $str;echo "<br />";echo "With trim: " . trim($str);?><body><html>

The browser output of the code above will be:


Without trim: Hello World!With trim: Hello World!

If you select "View source" in the browser window, you will see the following HTML:


<html><body>Without trim:     Hello World!     <br />With trim: Hello World!</body></html>


Date Function: -

1. PHP Date() Function

The PHP date() function is used to format a time and/or date.

The PHP Date() Function

The PHP date() function formats a timestamp to a more readable date and time.

A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred.

Syntax

date(format,timestamp)
Parameter Description
format Required. Specifies the format of the timestamp
timestamp Optional. Specifies a timestamp. Default is the current date and time

PHP Date() - Format the Date

The required format parameter in the date() function specifies how to format the date/time.

Here are some characters that can be used:

  • d - Represents the day of the month (01 to 31)
  • m - Represents a month (01 to 12)
  • Y - Represents a year (in four digits)


<?phpecho date("Y/m/d") . "<br />";echo date("Y.m.d") . "<br />";echo date("Y-m-d")?>

The output of the code above could be something like this:


2009/05/112009.05.112009-05-11

PHP Date() - Adding a Timestamp

The optional timestamp parameter in the date() function specifies a timestamp. If you do not specify a timestamp, the current date and time will be used.

The mktime() function returns the Unix timestamp for a date.

The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

Syntax for mktime()

mktime(hour,minute,second,month,day,year,is_dst)

To go one day in the future we simply add one to the day argument of mktime():


<?php$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));echo "Tomorrow is ".date("Y/m/d", $tomorrow);?>

The output of the code above could be something like this:


Tomorrow is 2009/05/12

2. PHP checkdate() Function

Definition and Usage

The checkdate() 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)
Parameter Description
month Required. Specifies the month
day Required. Specifies the day
year Required. Specifies the year

Example

<?php

if(checkdate(12,31,2000))

echo "True";

else

echo "False";

if (checkdate(2,29,2003))

echo "true";

else

echo "False";

if( checkdate(2,29,2004))

echo "True";

else

echo "False";

?>

The output of the code above will be:


True

False

True

3. PHP date_default_timezone_get() Function

Definition and Usage

The date_default_timezone_get() function returns the default timezone used by all date/time functions in a script.

Syntax

date_default_timezone_get(void)
Parameter Description
void Optional.

Example

<?phpecho(date_default_timezone_get());?>

The output of the code above will be:


UTC

4. PHP date_default_timezone_set() Function

Definition and Usage

The date_default_timezone_set() function sets the default timezone used by all date/time functions.

Syntax

date_default_timezone_set(timezone)
Parameter Description
timezone Required. The timezone identifier, like "UTC" or "Europe/Paris". List of valid timezones: http://www.php.net/manual/en/timezones.php

Tips and Notes

Note: This function always returns true (even if the timezone parameter isn't valid).

Example

<?phpecho(date_default_timezone_set("Europe/Paris"));?>

The output of the code above will be:


1

5. PHP getdate() Function

Definition and Usage

The getdate() 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)
Parameter Description
timestamp Optional. Specifies the time in Unix time format

Example 1

<?phpprint_r(getdate());?>

The output of the code above could be:


Array([seconds] => 45[minutes] => 52[hours] => 14[mday] => 24[wday] => 2[mon] => 1[year] => 2006[yday] => 23[weekday] => Tuesday[month] => January[0] => 1138110765)

Example 2

<?php$my_t=getdate(date("U"));print("$my_t[weekday], $my_t[month] $my_t[mday], $my_t[year]");?>

The output of the code above could be:


Wednesday, January 25, 2006


6. PHP date() Function

Definition and Usage

The date() function formats a local time/date.

Syntax

date(format,timestamp)
Parameter Description
format Required. 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

<?phpecho("Result with date():<br />");echo(date("l") . "<br />");echo(date("l dS \of F Y h:i:s A") . "<br />");echo("Oct 3,1975 was on a ".date("l", mktime(0,0,0,10,3,1975))."<br />");echo(date(DATE_RFC822) . "<br />");echo(date(DATE_ATOM,mktime(0,0,0,10,3,1975)) . "<br /><br />");echo("Result with gmdate():<br />");echo(gmdate("l") . "<br />");echo(gmdate("l dS \of F Y h:i:s A") . "<br />");echo("Oct 3,1975 was on a ".gmdate("l", mktime(0,0,0,10,3,1975))."<br />");echo(gmdate(DATE_RFC822) . "<br />");echo(gmdate(DATE_ATOM,mktime(0,0,0,10,3,1975)) . "<br />");?>

The output of the code above could be something like this:


Result with date():TuesdayTuesday 24th of January 2006 02:41:22 PMOct 3,1975 was on a FridayTue, 24 Jan 2006 14:41:22 CET1975-10-03T00:00:00+0100Result with gmdate():TuesdayTuesday 24th of January 2006 01:41:22 PMOct 3,1975 was on a ThursdayTue, 24 Jan 2006 13:41:22 GMT1975-10-02T23:00:00+0000


7. PHP localtime() Function

Definition and Usage

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

Syntax

localtime(timestamp,is_associative)
Parameter Description
timestamp Optional. Specifies the date or time to be formatted. If no timestamp is specified, it uses the current local time.
is_associative Optional. 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

Example without and with associative arrays:


<?phpprint_r(localtime(time(),true));?>

The output of the code above could be:


Array([tm_sec] => 28[tm_min] => 35[tm_hour] => 13[tm_mday] => 25[tm_mon] => 0[tm_year] => 106[tm_wday] => 3[tm_yday] => 24[tm_isdst] => 0)


8. PHP time() Function

Definition and Usage

The time() function returns the current time as a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).

Syntax

time(void)
Parameter Description
void Optional.

Tips and Notes

Note: Calling this function is identical to calling mktime() with no parameters, or calling date("U").


Example

<?php$t=time();echo($t . "<br />");echo(date("D F d Y",$t));?>

The output of the code above could be:


1138618081Mon January 30 2006


PR!1 : Script to find Indian Standard Time.

<html>

<body>

<?php

$t=getdate();

$Hours=$t[hours];

echo ($Hours+5);

echo "<br>";

$Minutes=$t[minutes];

echo $Minutes+30;

$Seconds=$t[seconds];

echo $Seconds;

?>

<body>

<html>







/Operators and Expressions/

Exploring operator concept: 9 types Precedence of operators Exploring expressions

/Conditional statements: If-Else, Switch-case/

/Loops: For, While, Do-while/

/Foreach, Break-continue /

/Break and continue statements/

/Arrays/

/Functions/

Declaring a function Passing a value Returning value

/Form Handling/

/A PHP form example 1/

/A PHP form example 2/

Intermediate Level

/Including Files/

/Sessions/

/Cookies/

/Email/

/Date and Time/

1. Date() Function

The PHP date() function is used to format a time and/or date.

The PHP Date() Function

The PHP date() function formats a timestamp to a more readable date and time.

A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred.

Syntax

date(format,timestamp)
Parameter Description
format Required. Specifies the format of the timestamp
timestamp Optional. Specifies a timestamp. Default is the current date and time

PHP Date() - Format the Date

The required format parameter in the date() function specifies how to format the date/time.

Here are some characters that can be used:

  • d - Represents the day of the month (01 to 31)
  • m - Represents a month (01 to 12)
  • Y - Represents a year (in four digits)


<?phpecho date("Y/m/d") . "<br />";echo date("Y.m.d") . "<br />";echo date("Y-m-d")?>

The output of the code above could be something like this:


2009/05/112009.05.112009-05-11

PHP Date() - Adding a Timestamp

The optional timestamp parameter in the date() function specifies a timestamp. If you do not specify a timestamp, the current date and time will be used.

The mktime() function returns the Unix timestamp for a date.

The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

Syntax for mktime()

mktime(hour,minute,second,month,day,year,is_dst)

To go one day in the future we simply add one to the day argument of mktime():


<?php$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));echo "Tomorrow is ".date("Y/m/d", $tomorrow);?>

The output of the code above could be something like this:


Tomorrow is 2009/05/12

2. checkdate() Function

Definition and Usage

The checkdate() 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)
Parameter Description
month Required. Specifies the month
day Required. Specifies the day
year Required. Specifies the year

Example

<?php

if(checkdate(12,31,2000))

echo "True";

else

echo "False";

if (checkdate(2,29,2003))

echo "true";

else

echo "False";

if( checkdate(2,29,2004))

echo "True";

else

echo "False";

?>

The output of the code above will be:


True

False

True

3. date_default_timezone_get() Function

Definition and Usage

The date_default_timezone_get() function returns the default timezone used by all date/time functions in a script.

Syntax

date_default_timezone_get(void)
Parameter Description
void Optional.

Example

<?phpecho(date_default_timezone_get());?>

The output of the code above will be:


UTC

4. date_default_timezone_set() Function

Definition and Usage

The date_default_timezone_set() function sets the default timezone used by all date/time functions.

Syntax

date_default_timezone_set(timezone)
Parameter Description
timezone Required. The timezone identifier, like "UTC" or "Europe/Paris". List of valid timezones: http://www.php.net/manual/en/timezones.php

Tips and Notes

Note: This function always returns true (even if the timezone parameter isn't valid).

Example

<?phpecho(date_default_timezone_set("Europe/Paris"));?>

The output of the code above will be:


1

5. getdate() Function

Definition and Usage

The getdate() 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)
Parameter Description
timestamp Optional. Specifies the time in Unix time format

Example 1

<?phpprint_r(getdate());?>

The output of the code above could be:


Array([seconds] => 45[minutes] => 52[hours] => 14[mday] => 24[wday] => 2[mon] => 1[year] => 2006[yday] => 23[weekday] => Tuesday[month] => January[0] => 1138110765)

Example 2

<?php$my_t=getdate(date("U"));print("$my_t[weekday], $my_t[month] $my_t[mday], $my_t[year]");?>

The output of the code above could be:


Wednesday, January 25, 2006


6. date() Function

Definition and Usage

The date() function formats a local time/date.

Syntax

date(format,timestamp)
Parameter Description
format Required. 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

<?phpecho("Result with date():<br />");echo(date("l") . "<br />");echo(date("l dS \of F Y h:i:s A") . "<br />");echo("Oct 3,1975 was on a ".date("l", mktime(0,0,0,10,3,1975))."<br />");echo(date(DATE_RFC822) . "<br />");echo(date(DATE_ATOM,mktime(0,0,0,10,3,1975)) . "<br /><br />");echo("Result with gmdate():<br />");echo(gmdate("l") . "<br />");echo(gmdate("l dS \of F Y h:i:s A") . "<br />");echo("Oct 3,1975 was on a ".gmdate("l", mktime(0,0,0,10,3,1975))."<br />");echo(gmdate(DATE_RFC822) . "<br />");echo(gmdate(DATE_ATOM,mktime(0,0,0,10,3,1975)) . "<br />");?>

The output of the code above could be something like this:


Result with date():TuesdayTuesday 24th of January 2006 02:41:22 PMOct 3,1975 was on a FridayTue, 24 Jan 2006 14:41:22 CET1975-10-03T00:00:00+0100Result with gmdate():TuesdayTuesday 24th of January 2006 01:41:22 PMOct 3,1975 was on a ThursdayTue, 24 Jan 2006 13:41:22 GMT1975-10-02T23:00:00+0000


7. localtime() Function

Definition and Usage

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

Syntax

localtime(timestamp,is_associative)
Parameter Description
timestamp Optional. Specifies the date or time to be formatted. If no timestamp is specified, it uses the current local time.
is_associative Optional. 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

Example without and with associative arrays:


<?phpprint_r(localtime(time(),true));?>

The output of the code above could be:


Array([tm_sec] => 28[tm_min] => 35[tm_hour] => 13[tm_mday] => 25[tm_mon] => 0[tm_year] => 106[tm_wday] => 3[tm_yday] => 24[tm_isdst] => 0)


8. time() Function

Definition and Usage

The time() function returns the current time as a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).

Syntax

time(void)
Parameter Description
void Optional.

Tips and Notes

Note: Calling this function is identical to calling mktime() with no parameters, or calling date("U").


Example

<?php$t=time();echo($t . "<br />");echo(date("D F d Y",$t));?>

The output of the code above could be:


1138618081Mon January 30 2006


PR!1 : Script to find Indian Standard Time.

<html>

<body>

<?php

$t=getdate();

$Hours=$t[hours];

echo ($Hours+5);

echo "<br>";

$Minutes=$t[minutes];

echo $Minutes+30;

$Seconds=$t[seconds];

echo $Seconds;

?>

<body>

<html>







/File Handling/

/Exception Handling/

/PHP Function Library/

/Object Oriented Programming(OOPS) in PHP/

/Classes, Constructors and Methods,/

/Creating Instances./

/Variable scope within classes/

/Inheritance: the extends operator/

/Static methods and variables /

Advanced Level

/CakePHP: A PHP Framework/

Zend programming conventions Hands on sessions Examples

MySQL

Database Concepts

  • Entity - Relationship Model (ERM)
  • Primary And Foreign Keys
  • 1 st Normal Form [1 NF]
  • 2 nd Normal Form [2 NF]
  • 3 rd Normal Form [3 NF]
  • Overview of Denormalization

MySQL as DB Platform

MySQL Data Types

Administrative MySQL Command

MySQL Database Commands

/Basics of SQL/

Brief introduction to SQL and RDBMS

  • About SQL and RDBMS
  • Relational Databases
  • Primary keys
  • Foreign keys
  • Relationships

Simple Queries

  • Create table
  • Insert table into table values
  • Delete
  • Update table
  • Drop table

/MySQL/

MYSQL: Working with GUI tools Administrative tools Protection of databases

Intermediate Level

Introduction to views and joints

  • Create view
  • Using multiple tables while executing query

Advanced Level

PHP & MySQL Integration

Connecting to MYSQL using PHP Executing queries Error handling

Contributors and Content Editors

Minal, Nirmala Venkat