PHP Classes
elePHPant
Icontem

File: class.Date_Diff_with_Age

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Frank  >  Calculate Date Difference  >  class.Date_Diff_with_Age  >  Download  
File: class.Date_Diff_with_Age
Role: Application script
Content type: text/plain
Description: Added new method get Age
Class: Calculate Date Difference
Compute the time difference between two dates
Author: By
Last change:
Date: 7 years ago
Size: 4,034 bytes
 

Contents

Class file image Download
<?php

/*
 * Date Created: March 12, 2008
 * Create By: Frank Marcelo
 * Please feel free to email me: frankitoy@gmail.com
 * If you have problems
 * first parameter show be greater than second parameter
 * dates should be in this format etc September 16, 2008 00:00:00 09/16/2008 01:01:01
 *
 */

class Date_Diff
{
  public
$dateFrom ;
  public
$dateTo ;
  private
$intDays ;
  private
$intHours ;
  private
$intMinutes ;
  private
$sError ;

  public function
getError()
  {
    return
"Invalid date format from dateFrom or dateTo.";
  }

  public function
setDate_Diff( $dateFrom = null , $dateTo = null )
  {
   
$from = $this->isValidDate($dateFrom);
   
$to = $this->isValidDate($dateTo);

    if(
$from && $to )
    {
     
$dateDiff = mktime( $from['hour'] , $from['minutes'] , $from['seconds'] ,
                         
$from['month'] , $from['day'] , $from['year'] ) -
                 
mktime( $to['hour'] , $to['minutes'] , $to['seconds'] ,
                         
$to['month'] , $to['day'] , $to['year'] );
     
$this->intDays = floor( $dateDiff / (60*60*24) );
     
$this->intHours = floor( ($dateDiff - ($this->intDays*60*60*24) ) / (60*60) );
     
$this->intMinutes = floor( ($dateDiff - ($this->intDays*60*60*24) - ($this->intHours*60*60) ) /60 );
    }
  }

  public function
isValidDate( $sDate = "12/11/2008 00:00:00" )
  {
    if( !
is_null($sDate) && !empty($sDate) )
    {
      if(
preg_match( '/^[0-9][0-9][0-9][0-9]/' , $sDate ) )
      {
        list(
$strFullDate , $strHMS ) = explode( ' ' , $sDate );
        list(
$year , $month , $day ) = explode( '-' , $strFullDate );
       
$sDate = $month . DS . $day . DS . $year .' '.$strHMS;
      }

     
$dateString = split( " " , $sDate );
     
$dateParts = split( "[/-]" , $dateString[0] );
     
$dateParts2 = split( "[:]" , $dateString[1] );

      if( !
checkdate($dateParts[0],$dateParts[1],$dateParts[2]) )
      { return
false; }
      return array
             (
              
'month' => intval($dateParts[0]) ,
              
'day' => intval($dateParts[1]) ,
              
'year' => intval($dateParts[2]) ,
              
'hour' => intval($dateParts2[0]) ,
              
'minutes' => intval($dateParts2[1]) ,
              
'seconds' => intval($dateParts2[2])
             );
    }
    else
    {
      return
false;
    }
  }

  public function
getDiffInDays()
  { return
$this->intDays; }

  public function
getDiffInHours()
  { return
$this->intHours; }
 
  public function
getDiffInMinutes()
  { return
$this->intMinutes; }

  public function
getAge( $sDate = "12/11/2008 00:00:00" )
  {
   
$strDateNow = gmdate("m/d/Y H:i:s",time());
   
$arrOfDateBefore = $this->isValidDate( $sDate );
   
$arrOfDateNow = $this->isValidDate( $strDateNow );
   
/*
    implement using timezone offset
    $offset = +8;
    $now = gmdate("m/d/Y H:i:s", time() + (60*60*$offset) );
    */
    //print_r($arrOfDateNow);

   
$this->setDate_Diff( $strDateNow , $sDate );
   
$intAge = $arrOfDateNow['year'] - $arrOfDateBefore['year'];
    if( (
$arrOfDateNow['month'] == $arrOfDateBefore['month'] ) &&
        (
$arrOfDateNow['day'] >= $arrOfDateBefore['day'] ) &&
        (
$this->getDiffInMinutes() > 0 ) &&
        (
$arrOfDateNow['seconds'] >= $arrOfDateBefore['seconds'])
    )
    { return
$intAge; }
    else
    {
     
$intAge--;
      return
$intAge;
    }
  }

  function
__destruct()
  {
    unset(
$this->dateFrom);
    unset(
$this->dateTo);
    unset(
$this->intDays);
    unset(
$this->intHours);
    unset(
$this->intMinutes);
  }
}

//$date = new Date_Diff();
//$date->setDate_Diff( '02/01/2007 23:02:01', '01/31/2007 22:01:01');
//echo $date->getDiffInMinutes();
//echo $date->getAge( '04/25/1984 05:09:00' );
//echo $date->getDiffInMinutes();
//echo $date->getError();
?>