View Single Post
Old 05-28-2008, 11:50 PM   #10 (permalink)
Wildhoney
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,578
Thanks: 72
Wildhoney is on a distinguished road
Default

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.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote