Wednesday, January 23, 2008, 12:57 AM Source Code by John (Article #172)
One of my really, really bad weaknesses as a programmer is a tendency to not tidy up commonly-used code into object classes. Of course, one of the primary reasons for this is that object-oriented programming is not necessary for anything to function. Another reason is that I tend to be a tinkerer. I like to dip in and out of the code constantly rechecking the functionality. I rarely have a sustained bout of steady coding.
But, I thought it would be a fun bit of teaching to show you the construction of an object-oriented piece of PHP code. The code I chose is my old date conversion code. This code converts dates from either SQL (yyyy-mm-dd) or regular (mm/dd/yyyy) format to SQL, regular or full English expression (Saturday, February 2, 2008). Advertisements
The code is broken up into two pieces. The first in the include, 'dateconv.php' and the second is the actual script, 'test.php', that shows the results as applied to a regular set of dates.
dateconv.php
/*
The following class was found at http://blog.procontentanddesign.com/
*/
class dateconv {
var $date; var $sql; var $flag;
function set_date($new_date) { $this->date = $new_date; }
function get_date() { return $this->date; }
function get_sql() { //Check if the date already is SQL-style and return that if it is. if(stristr($this->date , '-')){ return $this->date; //Check if the date is regular, do conversion and return the converted value. }elseif(stristr($this->date , '/')){ list($m, $d, $y)=explode('/', $this->date); $sql='$y-$m-$d'; return $sql; }else{ return 'Invalid Date Format'; } } function lose_sql() { if(stristr($this->date , '-')){ list($y, $m, $d)=explode('-', $this->date); $m=$m+0; $d=$d+0; $sql='$m/$d/$y'; return $sql; }elseif(stristr($this->date , '/')){ return $this->date; }else{ return 'Invalid Date Format'; } }
function get_full_english() { $flag=FALSE; if(stristr($this->date , '/')){ list($month, $day, $year)=explode('/', $this->date); $month=$month+0; $day=$day+0; $flag=TRUE; }elseif(stristr($this->date , '-')){ list($year, $month, $day)=explode('-', $this->date); $month=$month+0; $day=$day+0; $flag=TRUE; } if($flag==TRUE){ $dayname=date('l', mktime(0, 0, 0, $month, $day, $year)); $monthname=date('F', mktime(0, 0, 0, $month, $day, $year)); $englishdate='$dayname, $monthname $day, $year'; return $englishdate; }else{ return 'Invalid date format'; } } }
test.php
// Make sure to include the file with your class in it. include 'dateconv.php';
// Gotta create 'new' instances of the objects you intend to use. $today = new dateconv(); $todays = new dateconv();
// We're gonna set a regular date and an SQL-tyle date. $today->set_date('2/2/2008'); $todays->set_date('2008-02-02');
// Convert the regular date to SQL, English and a regular date. echo 'Converting '. $today->get_date() . ' '; echo $today->get_sql() . '!'; echo $today->get_full_english() . '!'; echo $today->lose_sql() . '!';
// Convert the SQL date to SQL, English and a regular date. echo 'Converting '. $todays->get_date() . '!'; echo $todays->get_sql() . '!'; echo $todays->get_full_english() . '!'; echo $todays->lose_sql() . '!';
I apologize if some of the code formatting is a tad unreadable. My input scrubbing script is mean to the code. You can get a cleaner copy of the code in a ZIP file at the end of the article.Advertisements
Most of the code is commented (and take some time to appreciate that, because I'm meaner than cat piss about wasting time commenting my code). If you have a sense of how object-oriented programming works, you'll see this is a pretty simple set of methods.
If you're not so clear on how OOP, stick around. The next few days I will try to post a more basic guide to how to do object-oriented code.
|
© 2012 Pro Content and Design. All rights reserved.
|
Tools
Check Google PageRank
Recent articles- Government cuts and tech spending
- What's the deal with Japanese web design?
- Did the July PageRank update come early?
- Servers handling "Pending Delete" .COM domains failing
- Photoshop CS5, first impressions
- Google PageRank toolbar updates coming today
- To Microsoft's credit
- Tracking expiring and dropping domain names
- GoDaddy finally cleans up its checkout process
- Back to basics: clean up your link names
Welcome!
Wonder where to start with your web design business?
This blog follows along with my efforts to build and grow a website design business, Pro Content and Design.
The goal of this blog is to fill in blanks that may be empty as you get your business rolling.
This blog, particularly the source code section, is not intended for beginners. If you are not comfortable with databases, Ajax, DOM objects and other advanced methods, I strongly suggest you go take a look over at W3 Schools before even reading -- let alone tinkering with -- any of the code here.
I hope this blog has some value to web designers as they attempt to get their businesses going.
Good luck, and happy reading.
Thank you,
John Crawford
Pro Content and Design

Books
I highly recommend Art of the Start if you have no idea where to start with marketing.
Links
Coding
W3 Schools
IBM's Mastering Ajax Series
Graphic Design
Worth 1000
Stock.XCHNG
Urban Fonts
Website Software
Apache Web Server
SquirrelMail
PHP/Zend
Website Design Issues
Non-Standard Character Guide
Google Trends
Search Engine Optimization Analyzer
Business
Guy Kawasaki's Blog
Seth Godin's Blog
Freakonomics
Computers
NewEgg
My Main Website
Pro Content and Design
Websites I have built
PunxsyPage: local free classifieds website
Groundhog Festival: for the local summer festival
My Webapps
TV Stations Transmitter Database
Google PageRank Checker
|