Dates in PHP and MySQL using DATETIME fields

Date & Time 1 Comment »

This post covers the basic date & time operations which most of us are using in PHP and MySQL. The manipulation of the date and date & time fields in PHP is difficult because PHP is not very rich in date & time functions. This is why in most situations we have to write additional functions to subtract dates, calculate time intervals, calculate the difference between datetime elements and the list goes on.

Unlike PHP, MySQL is very rich in functions and supports for datetime elements. Its very good that we can always find what we need but this comes with a price: having so many options make the decision difficult.

In PHP there are 4 functions which can be used to handle most of the datetime operations:

  • time() – returns the current time measured in the number of seconds since January 1 1970 00:00:00 GMT.
  • mktime($hour,$minute,$second,$month,$day,$year) - returns the time specified through arguments measured in the number of seconds since January 1 1970 00:00:00 GMT.
  • date($format,[$timestamp]) – transforms from a time variable produced by time or mktime into a formated string
  • strtotime - transforms from a string to a time variable.

Lets start this tutorial creating an mysql table containing a DATETIME field: Read the rest of this entry »

Reflection

Reflection No Comments »

Reflection was introduced in PHP 5 to inspect and to retrieve information and reverse-engineer classes, interfaces, functions and methods as well as extensions during runtime.

Snippets:

How to create an instance of a class using reflection in PHP:

// this code does the same as: $instance = new ClassName();
$class = new ReflectionClass("ClassName");
if ($class->isInstantiable())
	$instance = $class->newInstance();

PHP Arrays

Arrays No Comments »

Iterating through Arrays:

$items = array('PHP', 'JavaScript','AJAX', 'Python','ASP', 'C#');

//iterate using foreach
foreach($items as $val)
{
     echo "$val";
}

// iterate using for loop
for($i = 0; $i < count($items); $i++)
{
     print("$items[$i]\n");
}

Iterating through Associative Arrays:

$associative_array
    = array("a" => "Pizza", "b" => "Burgers", "c" => "Chips");

foreach ($associative_array as $key => $val) {
    print "$key = $val\n";
}

while (list($key, $val) = each($associative_array)) {
    print "$key is $val\n";
}

PHP POST GET Method

Uncategorized No Comments »

Displays all the POST variables:

foreach ($_POST as $key => $value) {
	echo "$key = $value";
}

Displays all the GET variables:
foreach ($_POST as $key => $value) {
echo “$key = $value
“;
}

Displays all the GET and POST variables:

foreach ($_REQUEST as $key => $value) {
	echo "$key = $value";
}

PHP MySql Tutorial

Uncategorized No Comments »
$connection = mysql_connect(Config::$db_host,Config::$db_user,Config::$db_pass);
if (!$connection)  {	die('Could not connect: ' . mysql_error());  }

mysql_select_db(Config::$db_database, $connection);
mysql_query($query) or die("Couldn't execute query. " . mysql_error() . "
" . $query);; mysql_close($connection);
$connection = mysql_connect(Config::$db_host,Config::$db_user,Config::$db_pass);
if (!$connection)  {   die('Could not connect: ' . mysql_error());  }

mysql_select_db(Config::$db_database, $connection);

$query  = "SELECT * FROM table_name";
$result = mysql_query($query) or die("Couldn't execute query. " . mysql_error() . "
" . $query); while($row = mysql_fetch_assoc($result)) { foreach ($row as $key => $val) { echo $key." = ".$value." | "; } }

Classes in PHP

Classes No Comments »

Class Example

class Triangle{
	// a public member can be used from any point of the application:
	public $publicMember;

	// a protected member can be accessed only from within this class or from its subclasses:
	public $protectedMember;

	// a protected member can be accessed only from within this class:
	private $privateMember;

	// the constructor is a method(function) which does not return a parameter
	// and is invoked when the object is created:
	public function __construct($parameter)
	{
		echo "Constructor is invoked
"; } }

Interfaces – an interface defines a set of methods. Each class that implement and interface has to implement the methods defined in the interface.

A class can implement more than one interfaces.

interface Shape
{
	public function calculateArea();
}
class Triangle implements Shape{
	// because now Triangle implements shape, it has to implement the calculateArea function:
	public function calculateArea()
	{
		return 1000; // for the moment it returns a hardcoded value;
	}
}

Invoke parent constructor

class Car
{
    public function __construct($color)
    {
        parent::__construct($color);
        print "Creating a car\n";
    }

    public function accelerate() {
        print "Vroom!";
    }
}

class Toyota extends Car
{
    public function __construct($color) {
        parent::__construct($color);
        print "Creating a Toyota  Car\n";
    }

    public function accelerate() {
        print "Vroom Vroom!";
    }
}
Design by j david macor.com.Original WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in

Download from Free Wordpress templates