我有這樣一個時間戳:1362466800
從現在開始轉換Unix時間戳時間在PHP
我想它輸出到這樣的事情:
Time left: 1 Year 2 Months 5 Days 17 hours 6 Minutes Left
此外,如果有小於1年或不到1個月等。該部分字符串需要隱藏。
我知道有一些內置PHP函數這個在5.3+,但他們似乎並沒有能夠隱藏均爲0
感謝任何幫助值。
我有這樣一個時間戳:1362466800
從現在開始轉換Unix時間戳時間在PHP
我想它輸出到這樣的事情:
Time left: 1 Year 2 Months 5 Days 17 hours 6 Minutes Left
此外,如果有小於1年或不到1個月等。該部分字符串需要隱藏。
我知道有一些內置PHP函數這個在5.3+,但他們似乎並沒有能夠隱藏均爲0
感謝任何幫助值。
$datetime1 = new DateTime();
$datetime2 = new DateTime('@1362466800');
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format('%y years %m months %a days %h hours %i minutes %S seconds');
echo $elapsed;
編輯
如果你想elimate具有零個值,可以使用下面的代碼片段刪除他們的任何時期。
$empties = array('0 years', '0 months', '0 days', '0 hours', '0 minutes', '0 seconds');
echo str_replace($empties, '', $elapsed);
參考
基於@約翰德斯回答我能想出輸出一個非常簡單的閱讀時間的函數從現在起顯示:
function daysLeft($timestamp) {
$datetime1 = new DateTime('@'.time());
$datetime2 = new DateTime('@' . $timestamp);
$interval = $datetime1->diff($datetime2);
$years = $interval->format('%y');
$months = $interval->format('%m');
$days = $interval->format('%a');
$hours = $interval->format('%h');
$minutes = $interval->format('%i');
$seconds = $interval->format('%S');
if($seconds):
$elapsed = $seconds == 1 ? $seconds . ' Second ' : $seconds . ' Seconds ';
endif;
if($minutes):
$elapsed = $minutes == 1 ? $minutes . ' Minute ' : $minutes . ' Minutes ';
endif;
if($hours):
$elapsed = $hours == 1 ? $hours . ' Hour ' : $hours . ' Hours ';
endif;
if($days):
$elapsed = $days == 1 ? $days . ' Day ' : $days . ' Days ';
endif;
if($months):
$elapsed = $months == 1 ? $months . ' Month ' : $months . ' Months ';
endif;
if($years):
$elapsed = $years == 1 ? $years . ' Year ' : $years . ' Years ';
endif;
return $elapsed;
}
如果有人試圖做類似的事情。
基於@Talon我決定去遠一點,寫一個小類:
/**
* Calculates the time between two UNIX timestamps
* Like 3 hours or 180 minutes.
*
* @package Add Package Name Here
* @version 1
* @since Jul 12, 2013
* @author Andrew Starlike <[email protected]>
*/
class NiceDate
{
private $translation;
/**
* @param array $translation
*/
public function __construct($translation = null)
{
$this->setTranslation($translation);
}
/**
* Sets the translation of the time string
*
* @param array $translation (the array must have this keys 'second', 'minute', 'hour', 'day', 'month', 'year')
*/
private function setTranslation($translation = null)
{
if ($translation === null) {
$translation = array();
$plural = 's';
$formats = array('second', 'minute', 'hour', 'day', 'month', 'year');
foreach ($formats as $format) {
$translation[$format] = ucfirst($format);
$translation[$format . $plural] = ucfirst($format . $plural);
}
}
$this->translation = $translation;
}
/**
*
* @param int $old (UNIX timestamp)
* @param int $newer (UNIX timestamp)
* @return string
*/
public function timeLeft($old, $newer)
{
$datetime1 = new DateTime('@' . $newer);
$datetime2 = new DateTime('@' . $old);
$interval = $datetime1->diff($datetime2);
$years = $interval->format('%y');
$months = $interval->format('%m');
$days = $interval->format('%a');
$hours = $interval->format('%h');
$minutes = $interval->format('%i');
$seconds = $interval->format('%S');
$translation = $this->translation;
if ($seconds):
$elapsed = $seconds . ' ';
$elapsed .= $seconds == 1 ? $translation['second'] : $translation['seconds'];
endif;
if ($minutes):
$elapsed = $minutes . ' ';
$elapsed .= $minutes == 1 ? $translation['minute'] : $translation['minutes'];
endif;
if ($hours):
$elapsed = $hours . ' ';
$elapsed .= $hours == 1 ? $translation['hour'] : $translation['hours'];
endif;
if ($days):
$elapsed = $days . ' ';
$elapsed .= $days == 1 ? $translation['day'] : $translation['days'];
endif;
if ($months):
$elapsed = $months . ' ';
$elapsed .= $months == 1 ? $translation['month'] : $translation['months'];
endif;
if ($years):
$elapsed = $years . ' ';
$elapsed .= $years == 1 ? $translation['year'] : $translation['years'];
endif;
return $elapsed;
}
}
你可以這樣說:
echo secondsToTime(1362466800);
function secondsToTime($seconds) {
$dtF = new \DateTime('@0');
$dtT = new \DateTime("@$seconds");
return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds');
}
你可以使用DateTime類在PHP中,然後使用差異來獲得差異。就隱藏而言,只要檢查每一個是否爲0,並且如果它不顯示它。 – kennypu 2013-03-05 01:50:31