2014-03-27 4 views
0

두 번 time()을 추가하고 결과를 얻고 싶습니다. 1395897487가 지금은 변수 시간 3에서 두 시간을 추가하고 실제로 시간 차이를 찾을 수있는 기능을 사용하고 시간과 분 (시간과 분) 형식의 시간 값의 합계를 구하는 방법은 무엇입니까?

<?php 
    $time1 =time(); 
    $time2 = time(); 
    $time3 = $time1+$time2; 
    echo $time3; 
    ?> 

로 변환 할 예를 들어 내가 1395897426로 시간 1과 TIME2를 얻을. 기능 time_diff (시작 $, $ 끝)

{ 
     $time1 = date('H:i',$start); 
     $time2 = date('H:i',$end); 
     list($hours, $minutes) = explode(':', $time1); 
     $startTimestamp = mktime($hours, $minutes); 

     list($hours, $minutes) = explode(':', $time2); 
     $endTimestamp = mktime($hours, $minutes); 

     $seconds = $endTimestamp - $startTimestamp; 
     $minutes = ($seconds/60) % 60; 
     $hours = round($seconds/(60 * 60)); 
     echo "<b>$hours</b> hours and <b>$minutes</b> minutes"; 
} 
+2

숫자는 날짜와 시간을 나타냅니다. '2011 년 5 월 1 일 08:00:00'과 '2014 년 6 월 4 일 17:20:00'의 합계는 현명하게 무엇입니까? –

+2

하시겠습니까? 2 개의 날짜와 예상을 가진 예제를 게시 할 수 있습니까? – Rikesh

+0

사실 몇 분과 시간 (e.x : 4 시간 5 분, 3 시간 20 분)으로 두 시간의 합계를 더하고 싶습니다. @Rikesh – user3239311

답변

0

대신에 주변 하구의, 날짜 시간 당신을 위해 모든 작업을 할 수 있습니다.

$time1 = new DateTime; 
$time2 = new DateTime("2014-03-25 06:52:21"); 

$interval = $time2->diff($time1); 

// Get number of days and multiply by 24 hours 
$elapsed['h'] = $interval->format('%a') * 24; 
// Then add in hours 
$elapsed['h'] += $interval->format('%h'); 

// Minutes 
$elapsed['m'] = $interval->format('%i'); 

echo $elapsed['h'] . ' hours and ' . $elapsed['m'] . ' minutes'; 
// produces 41 hours and 3 minutes 
관련 문제