2

我正在尝试将秒数转换为日期。这是我目前拥有的(在 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;
}

但这还不够准确,因为它没有考虑到月份的不同长度……是否有已知的算法或其他东西可以正确执行?

提前致谢

编辑:

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

4

5 回答 5

12

我想这就是你要找的:

对于 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);
}
于 2013-11-01T20:28:40.223 回答
5

你不需要重新发明轮子。使用DateTime,如下所示:

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

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

于 2013-11-01T14:42:04.097 回答
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);
}
于 2014-02-21T02:37:22.843 回答
1

我发现 jerdiggity 的解决方案对我有些不同的问题非常有帮助。我需要以人类可读的格式显示自创建行以来的最高整个时间单位,如“添加 {number} {正确复数单位}前”。 唯一真正的变化是基于多个有条件地分配数组键,以便在视图之外处理它。作为 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
        );
    }
}

谢谢,胆怯。

于 2015-02-02T19:32:49.787 回答
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     
于 2013-11-01T14:43:58.393 回答