在PHP中将Unix时间戳从现在转换为时间

时间:2013-03-05 01:46:00

标签: php date date-math

我有一个这样的时间戳:1362466800

我想把它输出到这样的东西:

Time left: 1 Year 2 Months 5 Days 17 hours 6 Minutes Left

此外,如果少于1年或少于1个月等。该部分字符串需要隐藏。

我知道在5.3+中有一些内置的PHP函数,但它们似乎无法隐藏0的值。

感谢您的帮助。

4 个答案:

答案 0 :(得分:4)

$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;

See it in action

修改

如果您要对任何值为零的句点进行缩略处理,可以使用下面的代码段删除它们。

$empties = array('0 years', '0 months', '0 days', '0 hours', '0 minutes', '0 seconds');
echo str_replace($empties, '', $elapsed);

<强>参考

答案 1 :(得分:1)

你可以这样做:

   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');
}

答案 2 :(得分:0)

根据@John Condes的回答,我能够提出一个功能,从现在的显示中输出一个非常容易阅读的时间:

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;
}

如果有人试图做类似的事情。

答案 3 :(得分:0)

基于@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 <andrewstarlike@gmail.com>
 */
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;
}

}