PHP Date Class | An Adventure in OOP

One of the things that I find myself doing most often when creating a web app or even just a website using PHP/MySQL is formatting dates to the format that I want. It’s not hard to do but it gets annoying and repetative very quickly So, since I was looking for a good reason to [...]

One of the things that I find myself doing most often when creating a web app or even just a website using PHP/MySQL is formatting dates to the format that I want.

It’s not hard to do but it gets annoying and repetative very quickly

So, since I was looking for a good reason to try OOP I managed to write a little date class.

Note that this was written for php5, not sure how well it will port to 4, but worse case, you can take the functions and use them. They are cross version to the best of my knowledge.

This class also assumes that you can pull a MySQL formatted date from the database, and that the date you pass to the function is in MM-DD-YYYY HH:MM:SS format.

Here is the date class, save this as class.php

< ?php

class date_format{

public $date_year;
public $date_month;
public $date_day;
public $date_hour;
public $date_minute;
public $date_second;

public function date_convert($date,$type){
$date_year=substr($date,0,4);
$date_month=substr($date,5,2);
$date_day=substr($date,8,2);
$date_hour=substr($date,11,2);
$date_minute=substr($date,14,2);
$date_second=substr($date,17,2);
if($type == 1):
// example: March 23 2005
$date=date("F d, Y", mktime(0,0,0,$date_month,$date_day,$date_year));
elseif($type == 2):
// example: March 23 2005 at 9:15:04
$date=date('F d, Y \a\t\ h:i:s', mktime($date_hour,$date_minute,$date_second,$date_month,$date_day,$date_year));
endif;
echo $date;
}

}
?>

Couple of different things are going on here, but overall it’s pretty simple

public $date_year;
public $date_month;
public $date_day;
public $date_hour;
public $date_minute;
public $date_second;

This section is simply setting up the variables that we are going to be using in the date_convert function.

$date_year=substr($date,0,4);
$date_month=substr($date,5,2);
$date_day=substr($date,8,2);
$date_hour=substr($date,11,2);
$date_minute=substr($date,14,2);
$date_second=substr($date,17,2);

These are looking at the value passed and splitting each chunk of it (hours, minutes, days, months, year, etc) into seperate variables.

The if statements are checking to see what the value of $type is, depending on that value, that’s the format your date will be returned in. In this example if you pass a “1″ if will give you Month Day Year, but if you pass a “2″ it will give you Month Day Year and the Time.
Pretty neat huh?

How to use it.

Create a new php page named “test_date.php” and insert the following code into it:

< ?php
include('class.php');

$convert_date = new date_format;

$date = "2004-03-02 03:04:58";

$convert_date->date_convert($date, 2);
?>

Okay so the first line with the include statement is including all the code for the class that we just wrote. It’s better to do it this way so that you can have multiple classes/functions in one file and just have to type on include statement.

$convert_date=new date_format;
$date = "2004-03-02 03:04:58";

This is just a value I’ve set up for testing purposes by typically you would want to set this equal to a value from your MySQL DB

$convert_date->date_convert($date, 2);

Calls the date_convert function in our class and passes the $date variable we just set and a “2″ for type meaning that we will return a value something similar to

March 2 2005 3:04:58

So I hope this helps somebody. If you spot any errors or have any problems. Leave a comment and I’ll try to help you out/fix errors as need be.