Php-mysql Course

Since version 5.2, PHP has provided a DateTime class that can handle much more date and time information at the same time, rather than working with separate date and time functions. Also, it works hand-in-hand with the new DateTimeZone class.

DateTime()

To use the DateTime class, and generally any class, you need to create an object of the class, with the new keyword.
  - Syntax:
$now = new DateTime("time", obj_timezone);
This creates an instance of the DateTime class and stores it in a DateTime object called $now. The DateTime object is aware not only of the date and time it was created but also of the time zone used by the web server.
- The "time" argument is optional, it is a string with a valid literaly date/time format. If it is not added, PHP use current time.
- The "obj_timezone" is also optional, an object created with "new DateTimeZone()" representing the desired time zone (i.g new DateTimeZone('America/New_York')).
When using the 'obj_timezone' parameter, enter NULL for "time" argument to obtain the current time.

Most classes have properties and methods, which are similar to variables and functions, they're related only to a particular instance of a class. For example, you can use the methods of the DateTime classs to change certain values, such as the month, year, or perform date calculations.
The object's properties and methods can be accessed using the "->" operator.
$object->property       or       $object->method()
The DateTime object uses only methods, here is some examples:

format("format")

Returns a date string according to given "format" on success, or FALSE on failure.
  - Example (get and output the current date/time and the day of the week of a specified date):
<?php
// get and output the current date/time
$date = new DateTime();
echo $date->format('Y-m-d H:i:s');              // 2011-03-21 14:08:00

// output the day of the week of a specified date
$date = new DateTime('15-10-2012');          // can also be used: "15-October-2012", "2012-Oct-15"
echo '<br /> 15-10-2012 ia a '. $date->format('l');              // 15-10-2012 ia a Monday
?>
Output:
2011-03-21 14:08:00
15-10-2012 ia a Monday

setDate(year, month, day)

Resets the current date of the DateTime object to a different date (given with the parameters).

setTime(hour, minute, seconds)

Resets the current date of the DateTime object to a different time (given with the parameters, "seconds" is optional).
  - Example (set a new date and time for DateTime object and display it):
<?php
// set a new date and time and display it
$date = new DateTime();

// set a new date
$date->setDate(2012, 10, 15);

// set a new time
$date->setTime(12, 32);

echo $date->format('Y-m-d H:i:s');
?>
Output:
2012-10-15 12:32:00

getTimestamp()

Returns the Unix timestamp of a DateTime object.
  - Example ():
<?php
$date = new DateTime();
echo 'Current Timestamp: '. $date->getTimestamp();
?>
Outputs something like:
Current Timestamp: 1300710802

setTimestamp()

Sets the date and time based on an Unix timestamp.
  - Example ():
<?php
$date = new DateTime();
echo 'Current date/time: '. $date->format('Y-m-d H:i:s');

$date->setTimestamp(1178902725);
echo '<br /> Date time of the 1178902725 timestamp: '. $date->format('Y-m-d H:i:s');
?>
Outputs something like:
Current date/time: 2011-03-21 14:40:49
Date time of the 1178902725 timestamp: 2007-05-11 19:58:45

setTimezone()

The setTimezone class can be used to reset /change the time zone of a DateTime object.
  - Example:
<?php
// get and display the current time in Pacific/Auckland
$dtz = new DateTimeZone('Pacific/Auckland') ;
$date = new DateTime(NULL, $dtz);

echo  $date->format('H:i:s');              // 01:58:02
?>
Outputs something like:
01:58:02

• If you want to get the difference between two dates, you need to set a DateTime object for each one. Than, apply the diff() method (returns a DateInterval object representing the difference between the two dates).
  - Example, output the difference between the current time and "1996-07-07 14:45:00":
<?php
// set an object with the current date
$dateNow = new DateTime();
$now = $dateNow->format("Y-m-d H:i");           // store the currend date

// the second date
$date2 = new DateTime('1996-07-07 14:45:00');

// apply the diff() method, getting a DateInterval object ($diDiff)
$diDiff = $dateNow->diff($date2) ;

echo 'The difference between: '. $now. ' and "1996-07-07 14:45" is: '. $diDiff->format('%y year, %m months, %d days, %h hours, and %i minutes.');
?>
This code will output something like:
The difference between: 2011-03-21 15:26 and "1996-07-07 14:45" is: 14 year, 8 months, 14 days, 0 hours, and 41 minutes.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
PHP DateTime and DateTimeZone classes

Last accessed pages

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (141749)
  2. Node.js Move and Copy file (28420)
  3. MouseEvent - Events for Mouse (2909)
  4. PHPMailer (2311)
  5. Uploading images to server with Ajax (6095)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (473)
  2. CSS cursor property - Custom Cursors (79)
  3. The Mastery of Love (70)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (62)
  5. CSS3 2D transforms (46)