2011-07-21 25 views
1

我有一個功能,我發現做這方面的工作,但我試圖使它與GMT UTC時間戳工作:PHP的「倒計時直到」和「因爲時間」 GMT UTC時間功能

編輯: 也許我的問題是與我改怎麼「轉換」用戶輸入時間GMT ...

我是做

$the_user_input_date = strtotime('2011-07-20T01:13:00'); 
$utctime = gmdate('Y-m-d H:i:s',$the_user_input_date); 

是否gmdate('Y-m-d H:i:s',$the_user_input_date);實際上不是它「轉換」爲GMT?它只是格式化它?也許這就是我的問題。

這裏是我能提供的時間將是什麼樣子:

//local time in GMT 
2011-07-20T01:13:00 

//future time in GMT 
2011-07-20T19:49:39 

我試圖得到這個工作,如:

Started 36 mins ago 
    Will start in 33 mins 
    Will start in 6 hrs 21 mins 
    Will start in 4 days 4 hrs 33 mins 

這裏是什麼即時與迄今爲止的工作:

EDIT: new php code im working with, seems to ADD 10 HOURS on to my date. Any ideas? I updated it here: 

function ago($from) 
{ 
    $to = time(); 

    $to = (($to === null) ? (time()) : ($to)); 
    $to = ((is_int($to)) ? ($to) : (strtotime($to))); 
    $from = ((is_int($from)) ? ($from) : (strtotime($from))); 

    $units = array 
    (
    "year" => 29030400, // seconds in a year (12 months) 
    "month" => 2419200, // seconds in a month (4 weeks) 
    "week" => 604800, // seconds in a week (7 days) 
    "day" => 86400, // seconds in a day (24 hours) 
    "hour" => 3600,  // seconds in an hour (60 minutes) 
    "minute" => 60,  // seconds in a minute (60 seconds) 
    "second" => 1   // 1 second 
); 

    $diff = abs($from - $to); 
    $suffix = (($from > $to) ? ("from now") : ("ago")); 

    foreach($units as $unit => $mult) 
    if($diff >= $mult) 
    { 
    $and = (($mult != 1) ? ("") : ("and ")); 
    $output .= ", ".$and.intval($diff/$mult)." ".$unit.((intval($diff/$mult) == 1) ? ("") : ("s")); 
    $diff -= intval($diff/$mult) * $mult; 
    } 
    $output .= " ".$suffix; 
    $output = substr($output, strlen(", ")); 

    return $output; 
} 

@Jason

我想你的建議是什麼在這裏:

function ago($dateto) 
    { 
     $datetime1 = new DateTime($dateto); 
     $datetime2 = new DateTime(); 
     $interval = $datetime1->diff($datetime2); 
     // print_r($interval); 

     $format = ''; 
     if ($interval->h) { 
       $format .= ' %h ' . ($interval->h == 1 ? 'hour' : 'hours'); 
     } 
     if ($interval->i) { 
       $format .= ' %i ' . ($interval->i == 1 ? 'minute' : 'minutes'); 
     } 
     // more logic for each interval 

     if ($format) { 
       echo $interval->format($format), ' ago'; 
     } 
     else { 
       echo 'now'; 
     } 
    } 

它似乎總是對我的時間增加10小時。

任何想法可能會發生什麼?

也許一個錯誤在於我如何節省目標時間? 當有人提出一個時間的轉換和存儲這樣

用戶提交的時間總是開始時看起來像這樣爲自己的本地時間: 2011年7月20日下午11:00

然後:

$time = mysql_real_escape_string($_POST['time']); 

$the_date = strtotime($time); 

//make user input time into GMT time 
$utctime = gmdate('Y/m/d H:i:s',$the_date); 

$query = "INSERT INTO $table (time) VALUES ('$utctime');"; 
mysql_query($query); 
+0

? PHP> = 5.3中的'DateTime'類內置了很多這樣的功能。 –

+0

'PHP 5.2 FastCGI'是目前根據我的主機提供商設置的。它給了我一個選擇'PHP 5.3 FastCGI'的選項,如果我開關。我怎麼能做我想要完成的事情? – brybam

回答

2

假設您有權訪問PHP> = 5.3,我建議您使用DateTime::diff()。返回的DateInterval爲您提供了所有需要顯示的部分以及自己的方法,例如format()

下面是一個示例給你一個想法。 PHP文檔鏈接的評論中有更完整的示例。

<?php 
$datetime1 = new DateTime('2011-07-20'); 
$datetime2 = new DateTime(); 
$interval = $datetime1->diff($datetime2); 
// print_r($interval); 

$format = ''; 
if ($interval->h) { 
     $format .= ' %h ' . ($interval->h == 1 ? 'hour' : 'hours'); 
} 
if ($interval->i) { 
     $format .= ' %i ' . ($interval->i == 1 ? 'minute' : 'minutes'); 
} 
// more logic for each interval 

if ($format) { 
     echo $interval->format($format), ' ago'; 
} 
else { 
     echo 'now'; 
} 

它輸出(在我的系統):

22小時11分鐘前

您正在使用什麼版本的PHP
+0

謝謝傑森,我仍然有一些奇怪的錯誤,似乎是幾個小時了。我更新了我的帖子,向你展示了我的嘗試。有任何想法嗎? – brybam

+0

我相信這是源於你存儲的時間。有很多地方可能發生 - PHP,MySQL等。我的建議是全線使用GMT。 –

+0

這就是我已經在做的事情。如果你看上面,我向你展示了我如何花時間將用戶指定時間轉換爲GMT。我的格林威治標準時間不正確? (在我的問題的底部,我幾分鐘前添加了它) – brybam

0

您的$datefrom是一個字符串,但$dateto是一個int。你不能以這種方式減去它們。

相反的:

$datefrom=gmdate("Y/m/d\TH:i:s\Z"); 

務必:

$datefrom=time(); 

PS。我沒有檢查代碼的其餘部分。

+0

我嘗試了你的建議。然後嘗試回聲前('2011/07/20/T21:23:00Z')'這是一小時後,它顯示'-10919秒前',所以我不認爲這工作:/ – brybam

+0

'2011/07/20/T21:23:00Z'是從現在美國東部時間起一個小時。做'2011/07/20/T21:23:00 EDT',這是正確的。或者只是當地時間的「2011/07/20/T21:23:00」。 – Ariel

+0

另一件事,如果我做的時間()不只是得到服務器的時間?我需要它是格林尼治標準時間,因此它一直是標準化的時間這就是爲什麼我試圖使用gmdate – brybam