Search This Blog

Thursday, December 9, 2010

PHP :: Date & Time Options

Hey Cakes..!
Lets see how we can modify the Date/Time as we want..


ADD & DIVIDE TIME:

$time = date('H:i:s');
$data = explode(":",$time);
$min += $data['1'];
$min += $data['0'] * 60;
$endresult = $min/$count;
$avg = date("H:i",mktime(0,$endresult,0,0,0,0));

TIME DURATION BETWEEN TWO TIME:

$before = strtotime('2010-08-26 08:00:00');
$after = strtotime('2010-08-26 18:50:00');
$diff = $after - $before;
$hours = floor($diff / 3600);
$minutes = floor(($diff - $hours * 3600) / 60);
$seconds = $diff - $hours * 3600 - $minutes * 60;
IF(STRLEN($hours)<2)$hours="0".$hours;
IF(STRLEN($minutes)<2)$minutes="0".$minutes;
IF(STRLEN($seconds)<2)$seconds="0".$seconds;
debug($hours." : ".$minutes." : ".$seconds);

FIND TOMORROW'S DATE

$start = date('Y-m-d');
$s = strtotime($start);
$start = date('Y-m-d', strtotime('+1 day', $s));

FIND NEXT TIME:

$start = date('Y-m-d H:i:s');
$s = strtotime($start);
$start = date('Y-m-d H:i:s', strtotime('+20 min', $s)); // Finds the date and time for after 20 minutes

ADD TWO TIME:

function add_time($start, $end){
       $str = explode(':', $start);
       $en = explode(':', $end);
       $hours = $str[0] + $en[0];
       $minutes = $str[1] + $en[1];
       $sec = $str[2] + $en[2];
       while($sec > 60){
               $minutes++;
               $sec = $sec - 60;
       }
       while($minutes > 60){
               $hours++;
               $minutes = $minutes - 60;
       }
       return($hours.":".$minutes.":".$sec);
}

CONVERT TIME TO SECONDS:

$totaltime = '15:45:00';
$sat=explode(':',$totaltime);

$totalsec =  ( (int)$sat[0]*3600 ) + ( (int)$sat[1]*60 ) + (int)$sat[2];

FIND AVERAGE TIME:

function avg_time($time_in_seconds=null,$number=null){
       if($number){
          $totaltime =$time_in_seconds/$number;
       }else{
         $totaltime=0;
      }
       $hours = floor( $totaltime/ (60*60));
       $mins = floor(($totaltime - $hours * 60*60) / (60));
       $secs = floor(($totaltime - $hours * 60*60 - $mins*60));
       IF(STRLEN($hours)<2){$hours="0".$hours;}
       IF(STRLEN($mins)<2){$mins="0".$mins;}
       IF(STRLEN($secs)<2){$secs="0".$secs;}

       $tic_time = $hours.":".$mins.":".$secs;
    
 }

FIND LAST DAY OF ANY MONTH AND YEAR:

date("t", mktime(1, 1, 1, $month, 1, $year))


TIME DIFFERENCE BETWEEN THE TWO DATES:

function time_diff($date1=null,$date2=null){
    // Gets the time difference between the dates passed and returns it
in the format HH:MM:SS
         if(!$date1){
               $date1=$this->params[0];
         }
         if(!$date2){
               $date2=$this->params[1];
         }
           $before = strtotime($date1);
       $after = strtotime($date2);
       $diff = $after - $before;
       $hours = floor($diff / 3600);
       $minutes = floor(($diff - $hours * 3600) / 60);
       $seconds = $diff - $hours * 3600 - $minutes * 60;
               IF(STRLEN($hours)<2)$hours="0".$hours;
               IF(STRLEN($minutes)<2)$minutes="0".$minutes;
               IF(STRLEN($seconds)<2)$seconds="0".$seconds;
        $difference=$hours.":".$minutes.":".$seconds;
}


DAYS DIFFERENCE BETWEEN THE TWO DATES:

function dateDiff($dformat, $endDate, $beginDate) {
 // Function returns the number of days differing b/w two dates.
 // Here the $dformat is the delimiter say '-', '/', etc..
          $date_parts1=explode($dformat, $beginDate);
          $date_parts2=explode($dformat, $endDate);
          $start_date=gregoriantojd($date_parts1[1], $date_parts1[2],
$date_parts1[0]);
          $end_date=gregoriantojd($date_parts2[1], $date_parts2[2],
$date_parts2[0]);
          return $end_date - $start_date;
}

--
ಅರ್ಜುನ್ ಅರಸ್

No comments:

Post a Comment