<?php
 
    class time{
        
        public static function asString( $input ){
            
                $input = self::getFormat( $input , "Y-m-d H:i:s" );
            
               list( $data  , $czas ) = explode(" ",$input);
               list( $godzina , $minut , $sekund ) = explode( ":" , $czas );
               list( $Ngodzina , $Nminut , $Nsekund ) = explode( ":" , date("H:i:s") );
               
               if( $data == date("Y-m-d") ){
                    
                    $string = '';
                    
                    $Rgodzin = $Ngodzina-$godzina;
                    $Rminut = $Nminut-$minut;
                    if($Rminut<0){
                        $Rminut = ($Rminut*-1)+(60-$Nminut);
                        if($Rminut>60){
                            $Rminut -= 60;
                        } else $Rgodzin-=1;
                        
                    }
                    $Rsekund = $Nsekund-$sekund;

                    if( $Rgodzin>0 ){
                        switch( $Rgodzin ){
                            case 1:
                                $string .= l('Godzinę') . ' ' . (($Rminut==0) ? l('temu') : '');
                            break;
                            
                            case 2:
                            case 3:
                            case 4:
                                $string .= $Rgodzin . ' '.l('godzin').' ' . (($Rminut==0) ? l('temu') : '');
                            break;
                            
                            default:
                                $string .= $Rgodzin . ' '.l('godzin').' ' . (($Rminut==0) ? l('temu') : '');
                            break;
                        }
                    }
                    
                    if( $Rminut>0 ){
                        if($Rminut==1) $string.= l('minutę');
                        else $string .= $Rminut . ' '.l('minut temu');
                    }
                    
                    if( $Rsekund and $Rminut==0 and $Rgodzin==0 ){
                        switch( $Rsekund ){
                            case 1:
                                $string .= l('przed chwilą');
                            break;
                            
                            case 2:
                            case 3:
                            case 4:
                                $string .= $Rsekund . ' ' . l('sekund temu');
                            break;
                            
                            default:
                                $string .= $Rsekund . ' ' . l('sekund temu');
                            break;
                        }
                    }
                    
                    return $string;
                    
               } else {
                    $diff = self::dayDiff( $data , date("Y-m-d") );
                    if( $diff>1 ){
                        return $diff . ' ' . l('dni temu');
                    } else {
                        return l('wczoraj o') . ' ' . $czas; 
                    }
               }
            
        }

        public static function asVideoPlayer( $sec ){
            
            $sec = intval( $sec );
            $total = '';
            
            if( $sec<60 ){
                
                $total = '00:' . ($sec>10?'':'0') . $sec;
                
            } elseif( $sec == 60 ){
                
                $total = '01:00';
                
            } else {
                
                // Hour
                if( $sec>3600 ){
                    
                    $hour = intval( $sec/3600 );
                    $total .= $hour.':';
                    
                    $sec = intval($sec-($hour*3600));
                    
                    if( $sec>60 ){
                        
                        $min = intval( $sec/60 );
                        $total .= ($min>9?'':'0') . $min . ':';
                        
                        $sec = intval( $sec-($min*60) );
                        $total .= ($sec>9?'':'0') . $sec;
                        
                        
                    } else $total .= '00:' . ($sec>9?'':'0') . $sec;
                    
                } else {
                        $min = intval( $sec/60 );
                        $total .= $min . ':';
                        
                        $sec = intval( $sec-($min*60) );
                        $total .= ($sec>9?'':'0') . $sec;
                }
                
            }
            
                return $total;
            
        }
        
        public static function download_time( $videoPlayerFormat ){
           $time = time::asVideoPlayer( $videoPlayerFormat );
           
           $el = explode(":",$time);
           if( count($el)==3 ){
                
                return $el[0] . ' H, ' . $el[1] . ' min.<br />' . $el[2] . ' sec.';
                
           } else {
            
                if( $el[0]>0 ){
                    return  $el[0] . ' min. ' . $el[1] . ' sec.';
                } else {
                    if($el[1]=='00') return '1 seconds';
                    else return $el[1] . ' seconds';
                }
            
           }
           
        }

		public static function dayDiff( $d1 , $d2 ){
			
			$d1 = @explode( " " , $d1 );		
			$d1Date = @explode( "-" , $d1[0] );
			//$d1Time = explode( ":" , $d1[1] );
			$d1 = @mktime( 0 , 0 , 0 , $d1Date[1] , $d1Date[2] , $d1Date[0] );

			$d2 = @explode( " " , $d2 );
			$d2Date = @explode( "-" , $d2[0] );
			//$d2Time = explode( ":" , $d2[1] );		
			$d2 = @mktime( 0 , 0 , 0 , $d2Date[1] , $d2Date[2] , $d2Date[0] );

				return @round(($d2-$d1)/(60*60*24));
			
			
			
		}
        
        public static function getModify( $modify , $date=false , $format="Y-m-d H:i:s" ){
            
            if(!$date) $date = date( $format );
            
            $time = new DateTime( $date );
            $time->modify( $modify );
                return $time->format( $format );
            
        }
        
        public static function secoundDiff( $olddate , $newdate ){
            return strtotime( $newdate )-strtotime( $olddate );
        }
        
        public static function workDays($date1, $date2, $withStartDay = false) { 
            $date1=strtotime($date1); 
            $date2=strtotime($date2); 
            if ($date2===$date1 && !$withStartDay) return 0; 
            $char=1; 
            if ($date1>$date2) 
                {$datePom=$date1;$date1=$date2;$date2=$datePom;$char=-1;} 
            $count=0; 
            if (!$withStartDay) 
                $date1=strtotime('+1 day',$date1); 
            $date2=strtotime('+1 day',$date2); 
            $lastYear = null; 
            $hol=array('01-01','05-01','05-03','08-15','11-01','11-11','12-25','12-26'); 
            while ($date1<$date2) { 
                $year = date('Y', $date1); 
                if ($year !== $lastYear){ 
                    $lastYear = $year; 
                    $easter = date('m-d', easter_date($year)); 
                    $date = strtotime($year . '-' . $easter); 
                    $easterSec = date('m-d', strtotime('+1 day', $date)); 
                    $cc = date('m-d', strtotime('+60 days', $date)); 
                    $hol[8] = $easter; 
                    $hol[9] = $easterSec; 
                    $hol[10] = $cc; 
                } 
                $weekDay=date('w',$date1); 
                $md = date('m-d',$date1);
                if (!($weekDay==0 || $weekDay==6 || in_array($md,$hol) || $year>2010 && $md=='01-06')) {
                    $count++; 
                }
                $date1=strtotime('+1 day',$date1); 
            } 
            $count*=$char; 
            return $count; 
        }
        
        public static function getFormat( $date , $format = 'l, M, jS, Y h:i A' ){
            
            $a = new DateTime( $date );
            return $a->format( $format );
            
        }
         
        
        public static function hourDiff($firstTime,$lastTime) {
            $firstTime=strtotime($firstTime);
            $lastTime=strtotime($lastTime);
            $timeDiff=$lastTime-$firstTime;
            return $timeDiff/60/60;
        }
    }


?>