05-28-2008, 10:50 PM
|
#10 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
That's quite OK! How about the following?
php Code:
class DateDiff { private $m_pDates; private $m_aConversions; public function __construct($iStartDate, $iEndDate) { $this-> m_aConversions = array ( 'Seconds' => 0, 'Minutes' => 60, 'Hours' => 3600, 'Days' => 86400, 'Weeks' => 604800 ); $this-> m_pDates-> start = $iStartDate; $this-> m_pDates-> end = $iEndDate; } public function __call($szCall, $aArgs) { if(! preg_match('~^in(.+)~', $szCall, $aMatches)) { throw new Exception ('Invalid conversion format supplied.'); } $szCall = $aMatches[ 1]; if (! in_array($szCall, $this-> m_aConversions)) { throw new Exception ('Specified conversion is not available.'); } if($this-> m_aConversions[ $szCall] == 0) { return strtotime($this-> m_pDates-> end) - strtotime($this-> m_pDates-> start); } return(strtotime($this-> m_pDates-> end) - strtotime($this-> m_pDates-> start)) / $this-> m_aConversions[ $szCall]; }}
Then we can use it like so:
php Code:
$pDate = new DateDiff ('2008-05-29', '2008-06-01'); printf('Difference: %d seconds<br />', $pDate-> inSeconds()); printf('Difference: %d minutes<br />', $pDate-> inMinutes()); printf('Difference: %d hours<br />', $pDate-> inHours()); printf('Difference: %d days<br />', $pDate-> inDays()); printf('Difference: %d weeks<br />', $pDate-> inWeeks());
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|