PHP Date Class | An Adventure in OOP

Bookmark and Share

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 |  copy code |?
01
02
< ?php
03
 
04
class date_format{
05
 
06
public $date_year;
07
public $date_month;
08
public $date_day;
09
public $date_hour;
10
public $date_minute;
11
public $date_second;
12
 
13
public function date_convert($date,$type){
14
$date_year=substr($date,0,4);
15
$date_month=substr($date,5,2);
16
$date_day=substr($date,8,2);
17
$date_hour=substr($date,11,2);
18
$date_minute=substr($date,14,2);
19
$date_second=substr($date,17,2);
20
if($type == 1):
21
// example: March 23 2005
22
$date=date("F d, Y", mktime(0,0,0,$date_month,$date_day,$date_year));
23
elseif($type == 2):
24
// example: March 23 2005 at 9:15:04
25
$date=date('F d, Y \a\t\ h:i:s', mktime($date_hour,$date_minute,$date_second,$date_month,$date_day,$date_year));
26
endif;
27
echo $date;
28
}
29
 
30
}
31
?>
32

Couple of different things are going on here, but overall it’s pretty simple
 PHP |  copy code |?
1
2
public $date_year;
3
public $date_month;
4
public $date_day;
5
public $date_hour;
6
public $date_minute;
7
public $date_second;
8

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

 PHP |  copy code |?
1
2
$date_year=substr($date,0,4);
3
$date_month=substr($date,5,2);
4
$date_day=substr($date,8,2);
5
$date_hour=substr($date,11,2);
6
$date_minute=substr($date,14,2);
7
$date_second=substr($date,17,2);
8
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 |  copy code |?
01
02
< ?php
03
include('class.php');
04
 
05
$convert_date = new date_format;
06
 
07
$date = "2004-03-02 03:04:58";
08
 
09
$convert_date->date_convert($date, 2);
10
?>
11

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.
 PHP |  copy code |?
1
2
$convert_date=new date_format;
3
$date = "2004-03-02 03:04:58";
4
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
 PHP |  copy code |?
1
2
$convert_date->date_convert($date, 2);
3
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
 PHP |  copy code |?
1
2
March 2 2005 3:04:58
3

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.

4 Responses to “PHP Date Class | An Adventure in OOP”

  1. Britney Kate Says:

    Thanks for this Great Information :)
    Very usefull information to all of us, especially i like your blog design, Very Attractive Design & Try to Add some Social Media Widget This will increase your blog visibility.
    I will bookmark this url to my facebook Account, will visit again.

    Britney

  2. Crazy60 Says:

    This procedure is designed to extract DNA from kiwi in sufficient quantity to be seen and spooled. ,

  3. madgevip Says:

    I have a nice joke for you people! :) How do you revive a drowning rodent? Give it mouse-to-mouse resuscitation.
    ___________________________
    –/ [url=http://viafra.ceallisrx.info/site_map.html]viafra online generic[/url] /–

  4. melliemav Says:

    Sorry, for off top, i wanna tell one joke) Why do bees have sticky hair? Because they use honeycombs!!
    ___________________________
    –/ [url=http://viafra.ceallisrx.info/site_map.html]viafra prescription[/url] /–

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>