2012-11-18 49 views
0

標準功能:(見codepad推薦使用:mktime():該is_dst參數已被棄用

<?php 

$start_time_o = "18:00"; 
$end_time_o = date("H:i"); 

function get_time_difference(){ 

    global $start_time_o, $end_time_o; 

    $start_time = explode(":", $start_time_o); 
    $end_time = explode(":", $end_time_o); 

    $start_time_stamp = mktime($start_time[0], $start_time[1], 0, 0, 0, 0); 
    $end_time_stamp = mktime($end_time[0], $end_time[1], 0, 0, 0, 0); 

    $time_difference = $end_time_stamp - $start_time_stamp; 

    return gmdate("H:i", $time_difference); 
} 

echo get_time_difference(); 

?> 

修正函數:(見codepad

<?php 

$start_time_o = "18:00:01"; 
$end_time_o = date("H:i:s"); 

function get_time_difference(){ 

    global $start_time_o, $end_time_o; 

    $start_time = explode(":", $start_time_o); 
    $end_time = explode(":", $end_time_o); 

    $start_time_stamp = mktime($start_time[0], $start_time[1], $start_time[2], 0, 0, 0, 0); 
    $end_time_stamp = mktime($end_time[0], $end_time[1], $end_time[2], 0, 0, 0, 0); 

    $time_difference = $end_time_stamp - $start_time_stamp; 

    return gmdate("H:i:s", $time_difference); 
} 

echo get_time_difference(); 

?> 

第一功能沒有任何錯誤或通知。

修改函數給出了消息:

Deprecated: mktime(): The is_dst parameter is deprecated in C:\xampp\htdocs\index.php on line 13 
Deprecated: mktime(): The is_dst parameter is deprecated in C:\xampp\htdocs\index.php on line 14 

我不知道爲什麼,我只在修改函數接收消息? 這裏有什麼問題?

+0

你是在'mktime()'函數上使用一個參數太多+1 –

回答

2

使用strtotime

$someString = '10/10/2012 4:34 pm'; 
date_default_timezone_set('American/New_York'); 
$timestamp = strtotime($someString); 
echo date('Y/m/d H:i:s', $timestamp); 
+1

+1更好的方法來實現問題想要做的事情:-) –

2

您正在使用的mktime()一個函數的參數太多。刪除零點之一,像這樣:

$start_time_stamp = mktime($start_time[0], $start_time[1], $start_time[2], 0, 0, 0); 
$end_time_stamp = mktime($end_time[0], $end_time[1], $end_time[2], 0, 0, 0); 

這是什麼原因造成您的問題,但是作爲@NullPointer已經指出了他的答案,沒有達到這個一個更有效和更好的方式。用他的解決方案!

3

如果你想使用更簡單的方式做到這一點嘗試使用DateTime::diff

<?php 
$datetime1 = new DateTime('18:00:01'); 
$datetime2 = new DateTime(); 
$interval = $datetime1->diff($datetime2); 
echo $interval->format('H:i:s'); 
?> 

這是作爲一個函數,它可以使你更容易使用:

<?php 
function diff($first_datetime, $second_datetime = "now") 
{ 
    $datetime1 = new DateTime($first_datetime); 
    $datetime2 = new DateTime($second_datetime); 
    $interval = $datetime1->diff($datetime2); 
    return $interval->format('H:i:s'); 
} 

// One of the date/time parameters is now so 
// we only have to pass one parameter 
echo diff('18:00:01'); 

// Neither parameter is now 
echo diff('18:00:01', '20:00:00'); 
?> 
+2

+1另一個很好的答案,但對@bernte可能太過先進,因爲它使用對象:-) –

+0

嗨約翰..我試過你的小代碼:)但我沒有收到我想要的結果..我得到了+ 1h ..和-1h格式的區別..但我需要絕對時間 – bernte

+0

作爲echo diff('18:00: 01','17:00:01');我得到01:00:00,但我需要23:00:00。那可能嗎? – bernte

相關問題