My first OOP\Class attempt: Date Conversion.
Posted: Sun Oct 14, 2007 6:10 pm
I just threw myself into the world of OOP. Well... I'm guessing this code is far from what one would called "real" OOP, but it's a start for me at least. So now i came here to be bashed around and get told what a useless piece of code i just wrote
. However i already put this into use on some of my scripts where i have scattered functions all over for converting different dates into "readable" figures on posts on guestbooks, messages, news items. Etc. Anyways, Here it is:
Now for some questions 
1) I'm trying to figure out a way to extract what day of the week the actual date is just based on the raw input. Any ideas?
[s]
2) I'm using public functions and variables here, i tried converting everything to "private" and it still works. I think i'm misunderstanding the concept here. What did i actually change? And which should i use?[/s] Ignore all of this. It didn't work, i got it now.
Code: Select all
/**
class DateConversion
-------------------------
A class for converting all kinds of timestamps
into differnt formats throughout the website.
Raw input is format: YYYYMMDDHHIISS
Y: YEAR
M: MONTH
D: DAY
H: HOUR
I: MINUTE
S: SECOND
**/
class DateConversion {
public $year;
public $month;
public $day;
public $hour;
public $minute;
public $second;
public $monthNumbers = array (
"01",
"02",
"03",
"04",
"05",
"06",
"07",
"08",
"09",
"10",
"11",
"12"
);
public $monthNames = array (
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
);
function dateConversion() {
}
public function getYear($dateInput) {
$this->year = substr($dateInput,0,4);
return $this->year;
}
public function getMonth($dateInput) {
$this->month = substr($dateInput,4,2);
return $this->month;
}
public function getMonthName($dateInput) {
$this->month = $this->getMonth($dateInput);
$this->month = str_replace($this->monthNumbers,$this->monthNames,$this->month);
return $this->month;
}
public function getDay($dateInput) {
$this->day = substr($dateInput,6,2);
$zero_length = strspn($this->day, "0"); /* Remove leading zero */
$this->day = substr($this->day, $zero_length);
return $this->day;
}
public function getHour($dateInput) {
$this->hour = substr($dateInput,8,2);
return $this->hour;
}
public function getMinute($dateInput) {
$this->minute = substr($dateInput,10,2);
return $this->minute;
}
public function getSecond($dateInput) {
$this->second = substr($dateInput,12,2);
return $this->second;
}
}
/* Usage: Let's use the date\time right now as an example */
$today = date("YmdHis");
echo "Raw output: $today\n<br />";
/* Creating the date conversion object */
$dateOuput = new DateConversion();
echo "\n<br />";
echo $dateOuput->getYear($today);
echo "\n<br />";
echo $dateOuput->getMonth($today);
echo "\n<br />";
echo $dateOuput->getDay($today);
echo "\n<br />";
echo $dateOuput->getHour($today);
echo "\n<br />";
echo $dateOuput->getMinute($today);
echo "\n<br />";
echo $dateOuput->getSecond($today);
echo "\n<br />";
echo $dateOuput->getMonthName($today);1) I'm trying to figure out a way to extract what day of the week the actual date is just based on the raw input. Any ideas?
[s]
2) I'm using public functions and variables here, i tried converting everything to "private" and it still works. I think i'm misunderstanding the concept here. What did i actually change? And which should i use?[/s] Ignore all of this. It didn't work, i got it now.