Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 23rd, 2012  |  syntax: PHP  |  size: 2.29 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. $visualDNATimeObj = new VisualDNATime();
  4. $time1 = 1800000000;
  5. $time2 = 1800000000;
  6.  
  7. $numOfMinDifference = $visualDNATimeObj->getTimeDifferenceInMinutes($time1, $time2);
  8.  
  9. $visualDNATimeObj->showTimeDifferenceInMinutes($time1, $time2);
  10.  
  11. /**
  12.  * VisualDNATime class is used to manage timing functions for VisualDNA
  13.  *
  14.  * @author Stefano Margelli
  15.  *
  16.  */
  17. class VisualDNATime {
  18.        
  19.         /**
  20.          * It returns the number of minutes between two timestamps $time1 and $time2
  21.          *
  22.          * @param var $time1
  23.          * @param var $time2
  24.          *
  25.      * @return int
  26.          *
  27.          */
  28.         public function getTimeDifferenceInMinutes($time1, $time2){
  29.                
  30.                 //Control if both time are valid timestamps
  31.                 if(!($this->isTimestamp($time1) and $this->isTimestamp($time2))){
  32.                         return 0;
  33.                 }
  34.                
  35.                 if($time1 == $time2) return 0;
  36.                
  37.                 $timestampDifference = $time1 - $time2;
  38.                
  39.                 $numOfMinutes = (int)($timestampDifference/60);
  40.                
  41.                 /**
  42.                  * To allow the function showing a positive difference number of seconds
  43.                  * even if $time2 is a bigger value than $time1
  44.                  */
  45.                 if($numOfMinutes < 0) $numOfMinutes = $numOfMinutes * -1;
  46.                
  47.                 return $numOfMinutes;
  48.         }
  49.        
  50.         /**
  51.          * It prints the number of minutes between two timestamps $time1 and $time2
  52.          *
  53.          * @param var $time1
  54.          * @param var $time2
  55.          *
  56.      * @return void
  57.          *
  58.          */
  59.         public function showTimeDifferenceInMinutes($time1, $time2){
  60.                
  61.                 //Control if both time are valid timestamps
  62.                 if(!($this->isTimestamp($time1) and $this->isTimestamp($time2))){
  63.                         echo "Exception: no valid timestamps have been passed to the function as parameters";
  64.                         return;
  65.                 }
  66.                
  67.                 //Set default time zone
  68.                 date_default_timezone_set('Europe/Dublin');
  69.                
  70.                 //Format date to RFC 2822
  71.                 $date1Text = date('r',$time1);
  72.                 $date2Text = date('r',$time2);
  73.                                
  74.                 $numOfMinutes = $this->getTimeDifferenceInMinutes($time1, $time2);
  75.                
  76.                 //print number of minutes between the dates
  77.                 echo "Number of minutes between \"$date1Text\" and \"$date2Text\" is: $numOfMinutes";
  78.         }
  79.        
  80.         /**
  81.          * It check if the value $timestamp is a valid timestamp
  82.          *
  83.          * @param var $timestamp
  84.          *
  85.      * @return boolean
  86.          *
  87.          */
  88.         public function isTimestamp($timestamp){
  89.            if(is_numeric($timestamp) && (int)$timestamp == $timestamp){
  90.                         return true;
  91.            }
  92.            return false;
  93.         }
  94.                
  95. }
  96.  
  97.  
  98. ?>