【问题标题】:Accurate seconds to "day / month / year - minutes : hours : seconds" conversion精确秒到“日/月/年-分:时:秒”的转换
【发布时间】:2013-11-12 18:44:24
【问题描述】:

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

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

提前致谢

编辑:

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

【问题讨论】:

  • 30天相差多少个月?
  • 从什么时候开始有多少秒?
  • 对不起,如果我遗漏了什么但为什么不回显 date('Y-m-d H:i:s', $numberofsecs) ?
  • 我认为至少应该有一千个脚本。

标签: php time converter


【解决方案1】:

我想这就是你要找的东西:

对于 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);
}

【讨论】:

  • 谢谢,这适用于 php 5.3....但是有没有办法在 php 5.2.17 中做同样的事情?
  • @Sylar 请查看最新版本。 (Credit where credit is due)
【解决方案2】:

您无需重新发明轮子。使用DateTime,像这样:

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

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

【讨论】:

  • 谢谢,这行得通....但我正在使用 php 5.2.17,我得到Fatal error: Call to undefined method DateTime::setTimeStamp()
【解决方案3】:

我认为这正是他所要求的。 我从 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);
}

【讨论】:

    【解决方案4】:

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

    谢谢,胆小鬼。

    【讨论】:

      【解决方案5】:

      您长期这样做有什么特别的原因吗?有内置函数可以获取纪元时间并进行转换。

      #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     
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-19
        • 1970-01-01
        • 2018-08-24
        • 2017-11-22
        • 1970-01-01
        • 1970-01-01
        • 2016-03-26
        • 2011-06-09
        相关资源
        最近更新 更多