Top Banner
PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.
84

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

Mar 26, 2015

Download

Documents

Paige Jimenez
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

PHP: Date() Function

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

Page 2: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

PHP: Date() Function

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

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

Other characters, like"/", ".", or "-" can also be inserted between the letters to add additional formatting.

Page 3: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

PHP: Date() Function

Page 4: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

PHP: Date() Function

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.

Page 5: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

PHP: Date() Function

Page 6: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

PHP: Server Side Includes (SSI)

You can insert the content of one PHP file into another PHP file before the server executes it, with the include() or require() function.

include() generates a warning, but the script will continue execution require() generates a fatal error, and the script will stop

Page 7: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

PHP: Server Side Includes (SSI)

These two functions are used to create functions, headers, footers, or elements that will be reused on multiple pages.

SSI saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. When the header needs to be updated, you update the include file, or when you add a new page to your site, you can simply change the menu file (instead of updating the links on all your web pages).

Page 8: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

PHP: include() Function

The include() function takes all the content in a specified file and includes it in the current file.

If an error occurs, the include() function generates a warning, but the script will continue execution.

Page 9: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

PHP: include() Function

Assume that you have a standard header file, called "header.php". To include the header file in a page, use the include() function:

Page 10: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

PHP: include() Function

Assume we have a standard menu file, called "menu.php", that should be used on all pages:

Page 11: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

PHP: include() Function

Page 12: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

PHP: include() Function

Page 13: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

PHP: require() Function

The require() function is identical to include(), except that it handles errors differently.

If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.

Page 14: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

PHP: require() Function

Page 15: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

PHP: require() Function

Page 16: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

PHP: require() Function

Page 17: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

PHP: require() Function

Page 18: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

PHP: Cookies

A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.

Page 19: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

PHP: setcookie() Function

The setcookie() function is used to set a cookie.

Note: The setcookie() function must appear BEFORE the <html> tag.

Page 20: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

PHP: setcookie() Function

In the example below, we will create a cookie named "user" and assign the value "Alex Porter" to it. We also specify that the cookie should expire after one hour:

Page 21: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

PHP: setcookie() Function

You can also set the expiration time of the cookie in another way. It may be easier than using seconds.

Page 22: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

PHP: $_COOKIE

The PHP $_COOKIE variable is used to retrieve a cookie value.

In the example below, we retrieve the value of the cookie named "user" and display it on a page:

Page 23: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

PHP: $_COOKIE

Page 24: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Introduction

MySQL is a database.

The data in MySQL is stored in database objects called tables.

A table is a collection of related data entries and it consists of columns and rows.

Page 25: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Tables

A database most often contains one or more tables. Each table is identified by a name (e.g. "Customers" or "Orders"). Tables contain records (rows) with data.

Below is an example of a table called "Persons":

Page 26: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Queries

A query is a question or a request.

With MySQL, we can query a database for specific information and have a recordset returned.

Look at the following query:

Page 27: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Queries

The query above selects all the data in the "LastName" column from the "Persons" table, and will return a recordset like this:

Page 28: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: mysql_connect()

Before you can access data in a database, you must create a connection to the database.

In PHP, this is done with the mysql_connect() function.

Page 29: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: mysql_connect()

In the following example we store the connection in a variable ($con) for later use in the script. The "die" part will be executed if the connection fails:

Page 30: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: mysql_close()

The connection will be closed automatically when the script ends. To close the connection before, use the mysql_close() function:

Page 31: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Create a Database

The CREATE DATABASE statement is used to create a database in MySQL.

To get PHP to execute the statement above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.

Page 32: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Create a Database

Page 33: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Create a Table

The CREATE TABLE statement is used to create a table in MySQL.

We must add the CREATE TABLE statement to the mysql_query() function to execute the command.

Page 34: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Create a Table

Page 35: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Create a Table

Page 36: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Create a Table

Important: A database must be selected before a table can be created. The database is selected with the mysql_select_db() function.

Note: When you create a database field of type varchar, you must specify the maximum length of the field, e.g. varchar(15).

Page 37: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Create a Table

Primary Keys and Auto Increment Fields

Each table should have a primary key field.

A primary key is used to uniquely identify the rows in a table. Each primary key value must be unique within the table. Furthermore, the primary key field cannot be null because the database engine requires a value to locate the record.

Page 38: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Create a Table

The following example sets the personID field as the primary key field. The primary key field is often an ID number, and is often used with the AUTO_INCREMENT setting. AUTO_INCREMENT automatically increases the value of the field by 1 each time a new record is added. To ensure that the primary key field cannot be null, we must add the NOT NULL setting to the field.

Page 39: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Create a Table

Page 40: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Inserting Data

The INSERT INTO statement is used to add new records to a database table.Syntax

It is possible to write the INSERT INTO statement in two forms.

Page 41: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Inserting Data

The first form doesn't specify the column names where the data will be inserted, only their values:

The second form specifies both the column names and the values to be inserted:

Page 42: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Inserting Data

To get PHP to execute the statements above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.Example

Previously we created a table named "Persons", with three columns; "Firstname", "Lastname" and "Age". We will use the same table in this example. The following example adds two new records to the "Persons" table:

Page 43: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Inserting Data

Page 44: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Inserting Data - forms

Now we will create an HTML form that can be used to add new records to the "Persons" table.

Page 45: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Inserting Data - forms

When a user clicks the submit button in the HTML form in the example above, the form data is sent to "insert.php".

The "insert.php" file connects to a database, and retrieves the values from the form with the PHP $_POST variables.

Then, the mysql_query() function executes the INSERT INTO statement, and a new record will be added to the "Persons" table.

Page 46: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Inserting Data - forms

Page 47: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Selecting Data

The SELECT statement is used to select data from a database.

To get PHP to execute the statement above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.

Page 48: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Selecting Data

Page 49: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Selecting Data

The example above stores the data returned by the mysql_query() function in the $result variable.

Next, we use the mysql_fetch_array() function to return the first row from the recordset as an array. Each call to mysql_fetch_array() returns the next row in the recordset. The while loop loops through all the records in the recordset. To print the value of each row, we use the PHP $row variable ($row['FirstName'] and $row['LastName']).

Page 50: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Selecting Data

The output of the code above will be:

Here's how to return the data in an HTML table:

Page 51: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Selecting Data

Page 52: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Selecting Data

Page 53: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Selecting Data

The WHERE clause is used to extract only those records that fulfill a specified criterion.

To get PHP to execute the statement above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.

Page 54: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Selecting Data

Page 55: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Ordering

The ORDER BY keyword is used to sort the data in a recordset.

The ORDER BY keyword sorts the records in ascending order by default.

If you want to sort the records in a descending order, you can use the DESC keyword.

Page 56: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Ordering

Page 57: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Ordering

It is also possible to order by more than one column. When ordering by more than one column, the second column is only used if the values in the first column are equal:

Page 58: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Updating

The UPDATE statement is used to update existing records in a table.

Page 59: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Updating

Note: Notice the WHERE clause in the UPDATE syntax. The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!

To get PHP to execute the statement above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.

Page 60: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Updating

Page 61: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Deleting

The DELETE FROM statement is used to delete records from a database table.

Page 62: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Deleting

Note: Notice the WHERE clause in the DELETE syntax. The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted!

To get PHP to execute the statement above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.

Page 63: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Deleting

Page 64: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Examples

Page 65: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Examples

$order = 'date ASC';

$current_time = time();

$getevents = $DB->query("SELECT * FROM " . TABLE_PREFIX . "p18_events WHERE activated = '1' AND date >= '$current_time' ORDER BY " . $order . " LIMIT 5");

$rows = $DB->get_num_rows($getevents);

echo '<br /> <hr /> <br /> <b><font color="maroon"> Upcoming JCF & Mythological RoundTable&reg; Group Events </font></b><br /><br /> <table width="100%" border="0" cellpadding="0" cellspacing="0">';

Page 66: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

$rotate = 0; for($i = 0; $i < $rows && $i < $limit; $i++){ $event = $DB->fetch_array($getevents);

echo '<tr> <td style="padding: 4px;" bgcolor="'.iif($rotate, $rowcolor3, $rowcolor2).'"> <a href="' . RewriteLink('index.php?categoryid=69&p18_action=displayeventdetails&p18_eventid='.$event['eventid']).'">'.$event['title'].'</a><br /> ' . $event['category'] . ': ' . strtr(DisplayDate($event['date']), $sdlanguage) . ' in ' . $event['city'] . iif(strlen($event['city']) > 0 && strlen($event['state']) > 0, ', ', '') . strtoupper($event['state']) . '</td> </tr>';

if($rotate) { $rotate = 0; } else { $rotate = 1; } }

Page 67: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Examples

echo '</table> <p style="padding: 7px 0px 0px 4px;"> <a href="http://www.jcf.org/new/index.php?categoryid=69"><font style="color: maroon;"> See the entire calendar of events >></font></a></p>';

Page 68: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Examples

<br /> <hr /> <br /> <b><font color="maroon">Upcoming JCF & Mythological RoundTable&reg; Group Events</font></b><br /><br /> <table width="100%" border="0" cellpadding="0" cellspacing="0"><tr> <td style="padding: 4px;" bgcolor="#F9FBFF"> <a href="http://www.jcf.org/new/index.php?categoryid=69&amp;p18_action=displayeventdetails&amp;p18_eventid=504">MRT of Santa Monica</a><br />

RoundTable: May 6, 2010 in Santa Monica, CA</td> </tr><tr> <td style="padding: 4px;" bgcolor="#FFFFFF"> <a href="http://www.jcf.org/new/index.php?categoryid=69&amp;p18_action=displayeventdetails&amp;p18_eventid=459">Carl Gustav Jung en Antwoord op Job (Harm Knoop)</a><br />

Page 69: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Examples

Page 70: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Examples

Page 71: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Examples

Page 72: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Examples

Page 73: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Examples

Page 74: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Examples if(is_numeric($quoteid)) { PrintSection("Edit Quotation"); $event = $DB->query_first("SELECT * FROM " . TABLE_PREFIX . "p10009_randomquote WHERE quoteid = '$quoteid'"); } else { PrintSection("New Quotation");

$event = array("quote" => "", "source" => "", "link" => "", "image" => "", "nom_name" => "", "header" => "", "activated" => 1 ); }

Page 75: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Examples

echo '<form method="post" action="'.$refreshpage.'" /> <input type="hidden" name="quoteid" value="'.$quoteid.'" />

<table width="100%" border="0" cellpadding="5" cellspacing="0">';

if(is_numeric($quoteid)) { // delete quotation echo '<tr> <td class="tdrow2" width="25%"><b>Delete Quotation:</b></td> <td class="tdrow3" width="75%" valign="top"> <input type="checkbox" name="deletequotation" value="1"> Delete this quotation? </td> </tr>'; }

Page 76: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Examples

echo '<tr> <td class="tdrow2" width="25%" valign="top"><b>Quotation:</b></td> <td class="tdrow3" width="75%" valign="top"> <textarea name="quote" cols="60" rows="15">'.$event['quote'].'</textarea> </td> </tr>

<tr> <td class="tdrow2" width="25%"><b>Source:</b></td> <td class="tdrow3" width="75%" valign="top"> <input type="text" name="source" size="32" maxlength="128" value="'.htmlspecialchars($event['source']).'" /> </td> </tr>

Page 77: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Examples

<tr> <td class="tdrow2" width="25%"><b>Link:</b></td> <td class="tdrow3" width="75%" valign="top"> <input type="text" name="link" size="32" maxlength="128" value="'.htmlspecialchars($event['link']).'"> (i.e. http://www.jcf.org/new/works/detail.php?wid=104) </td> </tr>

<tr> <td class="tdrow2" width="25%"><b>Image url:</b></td> <td class="tdrow3" width="75%" valign="top"> <input type="text" name="image" size="32" maxlength="128" value="'.htmlspecialchars($event['image']).'" /> (i.e. http://www.domain.com/image.gif) </td> </tr>

Page 78: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

MySQL: Examples <tr> <td class="tdrow2" width="25%"><b>Submitted by:</b></td> <td class="tdrow3" width="75%" valign="top"> <input type="text" name="nom_name" size="32" maxlength="128" value="'.htmlspecialchars($event['nom_name']).'"> </td> </tr>

<tr> <td class="tdrow2" width="25%" valign="top"><b>Options:</b></td> <td class="tdrow3" width="75%" valign="top"> <input type="checkbox" name="activated" value="1" '.iif($event['activated'] == 1, "CHECKED", "").'><b>Display Quotation:</b> Are you ready to display this quotation?<br /> <input type="checkbox" name="header" value="1" '.iif($event['header'] == 1, "CHECKED", "").'><b>Use in site header:</b> Check to include in random quotations.<br /> </td> </tr>

Page 79: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

<tr> <td class="tdrow1" bgcolor="#FCFCFC" colspan="3" align="center"> <input type="hidden" name="PHPSESSID" value="'.strip_tags(session_id()).'" />';

if(is_numeric($quoteid)) { echo '<input type="hidden" name="action" value="updatequotation" /> <input type="submit" value="Update Quotation" />'; } else { echo '<input type="hidden" name="action" value="insertquotation" /> <input type="submit" value="Submit Quotation" />'; }

echo ' </td> </tr> </table> </form>'; EndSection();

}

Page 80: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

function InsertQuotation(){ global $DB; global $refreshpage;

$quote = $_POST['quote']; $source = $_POST['source']; $link = $_POST['link']; $image = $_POST['image']; $nom_name = $_POST['nom_name']; $header = $_POST['header']; $activated = $_POST['activated'];

if(strlen($quote) == 0) { $quote = 'No Quotation'; }

$DB->query("INSERT INTO " . TABLE_PREFIX . "p10009_randomquote (quote, source, link, image, nom_name, header, activated) VALUES ('$quote', '$source', '$link', '$image', '$nom_name', '$header', '$activated') ");

PrintRedirect($refreshpage, 1);}

Page 81: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

function UpdateQuotation(){ global $DB; global $refreshpage;

$quoteid = $_POST['quoteid'];

// delete quotation? if($_POST['deletequotation'] == 1) { DeleteQuotation($quoteid); }

$quote = $_POST['quote']; $source = $_POST['source']; $link = $_POST['link']; $image = $_POST['image']; $nom_name = $_POST['nom_name']; $header = $_POST['header']; $activated = $_POST['activated'];

Page 82: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

if(strlen($quote) == 0) { $quote = 'No Quotation'; }

$DB->query("UPDATE " . TABLE_PREFIX . "p10009_randomquote SET quote = '$quote', source = '$source', link = '$link', image = '$image', nom_name = '$nom_name', header = '$header', activated = '$activated' WHERE quoteid = '$quoteid' ");

PrintRedirect($refreshpage, 1);}

Page 83: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.

function DeleteQuotation($quoteid){ global $DB; global $refreshpage;

// delete quotation $DB->query("DELETE FROM " . TABLE_PREFIX . "p10009_randomquote WHERE quoteid = '$quoteid'");

PrintRedirect($refreshpage, 1);}

Page 84: PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.