2011-09-16 143 views
2

我正在尋找一種函數將「X小時前」和「X分鐘前」中提供的日期轉換回php中的時間戳,任何人都可以對此進行分解?轉換x小時x分鐘前回到unix時間戳

+1

爲什麼這個問題標記爲MySQL? –

+0

因爲我想在轉換它之後將它插入到MySQL中,所以抱歉不提。 – inviz

+0

我不明白這是如何相關的。如果你喜歡,你可以打印並粘貼在你的頭上,但它與問題無關。 –

回答

1
$hourago= "-1 hour"; 
$minago = "-2 minute"; 

$timestamp = strtotime($hourago.' '.$minago); 
echo $timestamp; 

$hourago= "-1"; 
$minago = "-2"; 

$timestamp = strtotime($hourago.' hour '.$minago.' minute'); 
echo $timestamp; 
2

我幫計算器上的人寫,做的這種反向,這裏是代碼的函數,我敢肯定,如果你解構和扭轉它,你將有你的答案:

<? 
$unix_time = 6734; 
echo howLongAgo($unix_time); 

function howLongAgo($time_difference){ 

// Swtich logic based on the time difference passed to this function, sets the english string and what number the difference needs to be divided by 
    switch($time_difference){ 
     case ($time_difference < 60): 
       $string = " second"; 
       break; 
     case ($time_difference >= 60 && $time_difference < 3600): 
       $string = " minute"; 
       $divider = 60; 
       break; 
     case ($time_difference >= 3600 && $time_difference < 86400): 
       $string = " hour"; 
       $divider = 3600; 
       break; 
     case ($time_difference >= 86400 && $time_difference < 2629743): 
       $string = " day"; 
       $divider = 86400; 
       break; 
     case ($time_difference >= 2629743 && $time_difference < 31556926): 
       $string = " month"; 
       $divider = 2629743; 
       break; 
     case ($time_difference >= 31556926): 
       $string = " year"; 
       $divider = 31556926; 
       break; 
    } 

// If a divider value is set during the switch, use it to get the actual difference 
if($divider){$diff = round($time_difference/$divider);}else{$diff = round($time_difference);} 
// If the difference does not equal 1, pluralize the final result EG: hours, minutes, seconds 
if($diff != 1){$pluralize="s";} 
// Concatenate all variables together and return them 
$final = $diff . $string . $pluralize . " ago"; 
return $final; 

} 
?> 
+4

好天啊...... –

+1

那種矯枉過正。此外,這有一個微妙的錯誤,因爲它不會正確說明DST(間隔是硬編碼的)。 – Jon

+1

並不回答這個問題。這兩項任務實際上完全不同。 – Artefacto