Advertisement
Guest User

Untitled

a guest
Jun 6th, 2012
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.28 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  *  @author Dejan Marjanovic <dejan.marjanovic@gmail.com>
  5.  *  @package eng5
  6.  *  @name Webarto DateTime
  7.  */
  8.  
  9. class WA_DT
  10. {
  11.  
  12.     /**
  13.     * Calculate shift span in one calendar day, if shift crosses midnight
  14.     *
  15.     * @return array
  16.     */
  17.     public static function shift_span($start, $end)
  18.     {    
  19.         $time_diff = self::time_diff($start, $end);
  20.         $time_diff = $time_diff['hours'];
  21.    
  22.         # ignore minutes, safe after we calculated difference
  23.        $start = (int) $start;
  24.         $end = (int) $end;
  25.                
  26.         if($start + $time_diff < 24)
  27.             return array($time_diff);
  28.    
  29.         return array(
  30.             abs($end - $time_diff),
  31.             abs(($start + $time_diff) % 24)
  32.         );
  33.     }
  34.  
  35.     /**
  36.     * Calculate shift span in hours (sort of TIMEDIFF MySQL)
  37.     *
  38.     * @return array
  39.     */
  40.     public static function time_diff($start, $end)
  41.     {
  42.         # do not ignore minutes
  43.        $start = strtotime($start);
  44.         $end = strtotime($end);
  45.    
  46.         $diff = abs($start - $end);
  47.         $hours = floor($diff / 3600);
  48.         $minutes = ($diff - ($hours * 3600)) / 60;
  49.    
  50.         $hours = $start > $end? 24 - $hours: $hours;
  51.        
  52.         return compact('hours', 'minutes');
  53.     }
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement