准确秒到“日/月/年 - 分:小时:秒”转换

时间:2013-11-01 14:36:53

标签: php time converter

我正在尝试将秒数转换为日期。这是我目前拥有的(在php中):

function secondsToTime($inputSeconds) {
    $secondsInAMinute = 60;
    $secondsInAnHour  = 60 * $secondsInAMinute;
    $secondsInADay    = 24 * $secondsInAnHour;
    $secondsInAMonth = 30 * $secondsInADay;
    $secondsInAYear = 12 * $secondsInAMonth;

    $years = floor($inputSeconds / $secondsInAYear);

    $monthSeconds = $inputSeconds % $secondsInAYear;
    $months = floor($monthSeconds / $secondsInAMonth);

    $daySeconds = $monthSeconds % $secondsInAMonth;
    $days = floor($daySeconds / $secondsInADay);

    $hourSeconds = $daySeconds % $secondsInADay;
    $hours = floor($hourSeconds / $secondsInAnHour);

    $minuteSeconds = $hourSeconds % $secondsInAnHour;
    $minutes = floor($minuteSeconds / $secondsInAMinute);

    $remainingSeconds = $minuteSeconds % $secondsInAMinute;
    $seconds = ceil($remainingSeconds);

    $obj = array(
        'years' => (int) $years,
        'months' => (int) $months,
        'days' => (int) $days,
        'hours' => (int) $hours,
        'minutes' => (int) $minutes,
        'seconds' => (int) $seconds
    );
    return $obj;
}

但这不够准确,因为它没有考虑到月份的不同长度......是否有已知的算法或其他东西,要做得恰到好处?

提前致谢

编辑:

对不起,英语不是我的语言...我说我需要转换为约会,但我实际上做的是两个约会之间的差异,我得到的结果只需几秒钟,我需要年,月,日,小时,分钟和秒的数量

5 个答案:

答案 0 :(得分:10)

我认为这就是你要找的东西:

对于PHP> = 5.3.0

function secondsToTime($inputSeconds) {
  $then = new DateTime(date('Y-m-d H:i:s', $inputSeconds));
  $now = new DateTime(date('Y-m-d H:i:s', time()));
  $diff = $then->diff($now);
  return array('years' => $diff->y, 'months' => $diff->m, 'days' => $diff->d, 'hours' => $diff->h, 'minutes' => $diff->i, 'seconds' => $diff->s);
}

对于PHP> = 5.2.0

function secondsToTime($inputSeconds) {
  $then = new DateTime(date('Y-m-d H:i:s', $inputSeconds));
  $now = new DateTime(date('Y-m-d H:i:s', time()));
  $years_then = $then->format('Y');
  $years_now = $now->format('Y');
  $years = $years_now - $years_then;

  $months_then = $then->format('m');
  $months_now = $now->format('m');
  $months = $months_now - $months_then;

  $days_then = $then->format('d');
  $days_now = $now->format('d');
  $days = $days_now - $days_then;

  $hours_then = $then->format('H');
  $hours_now = $now->format('H');
  $hours = $hours_now - $hours_then;

  $minutes_then = $then->format('i');
  $minutes_now = $now->format('i');
  $minutes = $minutes_now - $minutes_then;

  $seconds_then = $then->format('s');
  $seconds_now = $now->format('s');
  $seconds = $seconds_now - $seconds_then;

  if ($seconds < 0) {
    $minutes -= 1;
    $seconds += 60;
  }
  if ($minutes < 0) {
    $hours -= 1;
    $minutes += 60;
  }
  if ($hours < 0) {
    $days -= 1;
    $hours += 24;
  }
  $months_last = $months_now - 1;
  if ($months_now == 1) {
    $years_now -= 1;
    $months_last = 12;
  }
  // Thank you, second grade. ;)
  if ($months_last == 9 || $months_last == 4 || $months_last == 6 || $months_last == 11) {
    $days_last_month = 30;
  }
  else if ($months_last == 2) {
    if (($years_now % 4) == 0) {
      $days_last_month = 29;
    }
    else {
      $days_last_month = 28;
    }
  }
  else {
    $days_last_month = 31;
  }
  if ($days < 0) {
    $months -= 1;
    $days += $days_last_month;
  }
  if ($months < 0) {
    $years -= 1;
    $months += 12;
  }
  return array('years' => $years, 'months' => $months, 'days' => $days, 'hours' => $hours, 'minutes' => $minutes, 'seconds' => $seconds);
}

答案 1 :(得分:4)

您无需重新发明轮子。使用DateTime,如下所示:

function getInterval($seconds)
{
   $obj = new DateTime();
   $obj->setTimeStamp(time()+$seconds);
   return (array)$obj->diff(new DateTime());
}
//var_dump(getInterval(300));

- 您可能需要检查结果中的哪些字段,并仅选择您真正需要的字段

答案 2 :(得分:2)

我认为这正是他所要求的。 我是从jerdiggity回答中实现的。所有学分归他所有。

此函数实际上将给定的秒数转换为可读格式。

(PHP 5.3)

function seconds_in_redable($seconds) {
   $then = new DateTime(date('Y-m-d H:i:s', 0));
   $now = new DateTime(date('Y-m-d H:i:s', $seconds));
   $diff = $then->diff($now);
   return array('years' => $diff->y, 'months' => $diff->m, 'days' => $diff->d, 'hours' => $diff->h, 'minutes' => $diff->i, 'seconds' => $diff->s);
}

答案 3 :(得分:1)

我发现jerdiggity的解决方案对我有些不同的问题很有帮助。我需要显示自人类可读格式的行创建以来最长的整个时间单位,如&#34;添加{number} {正确复数单位}之前。&#34; 唯一真正的改变是基于多个有条件地分配数组键,以便只处理视图之外的数组。作为ZF2助手,它变成了:

class PrettyDate extends AbstractHelper
{

    /**
     * Ultra-simple Human readable date to-string conversion
     *
     * @param date - epoch
     *
     */
    public function __invoke($date)
    {   
        foreach ($this->secondsToTime($date) as $unit=>$measure) {
            if ($measure) {
                return "Added $measure $unit ago.";
            }
        }
    }

    private function secondsToTime($inputSeconds) {
        $then = new DateTime(date('Y-m-d H:i:s', $inputSeconds));
        $now = new DateTime(date('Y-m-d H:i:s', time()));
        $diff = $then->diff($now);
        return array(
          ($diff->y>1?'years':'year') => $diff->y, 
          ($diff->m>1?'months':'month') => $diff->m, 
          ($diff->d>1?'days':'day') => $diff->d, 
          ($diff->h>1?'hours':'hour') => $diff->h,
          ($diff->i>1?'minutes':'minute') => $diff->i,
          ($diff->s>1?'seconds':'second') => $diff->s
        );
    }
}

谢谢,jerdiggity。

答案 4 :(得分:0)

你做这件事有什么特别的原因吗?有内置函数来获取纪元时间并将其转换。

#Getting current epoch time in PHP
time()  // current Unix timestamp 

#Convert from epoch to human readable date in PHP
$epoch = 1340000000;
echo date('r', $epoch); // output as RFC 2822 date - returns local time
echo gmdate('r', $epoch); // returns GMT/UTC time    

#Use the DateTime class.
$epoch = 1344988800; 
$dt = new DateTime("@$epoch");  // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s'); // output = 2012-08-15 00:00:00