Php 精确到“秒”;日/月/年-分钟:小时:秒”;转换

Php 精确到“秒”;日/月/年-分钟:小时:秒”;转换,php,time,converter,Php,Time,Converter,我正试图将秒数转换为日期。以下是我目前拥有的(php): 但是这还不够准确,因为它没有考虑到不同的月份长度…有没有一个已知的算法或什么的,来正确地完成它 提前谢谢 编辑: 很抱歉,英语不是我的语言…我说我需要转换成日期,但我实际上要做的是两个日期之间的差异,我以秒为单位得到结果,我需要年、月、日、小时、分和秒的数量。你不需要重新发明轮子。像这样使用: function getInterval($seconds) { $obj = new DateTime(); $obj->se

我正试图将秒数转换为日期。以下是我目前拥有的(php):

但是这还不够准确,因为它没有考虑到不同的月份长度…有没有一个已知的算法或什么的,来正确地完成它

提前谢谢

编辑:


很抱歉,英语不是我的语言…我说我需要转换成日期,但我实际上要做的是两个日期之间的差异,我以秒为单位得到结果,我需要年、月、日、小时、分和秒的数量。你不需要重新发明轮子。像这样使用:

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

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

您这么做有什么特殊原因吗?内置函数用于获取历元时间并进行转换

#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     

我想这就是你想要的:

对于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);
}
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.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()));
  $diff = $then->diff($now);
  return array('years' => $diff->y, 'months' => $diff->m, 'days' => $diff->d, 'hours' => $diff->h, 'minutes' => $diff->i, 'seconds' => $diff->s);
}
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);
}
函数秒到秒($inputSeconds){
$then=新日期时间(日期('Y-m-d H:i:s',$inputSeconds));
$now=新的日期时间(日期('Y-m-dh:i:s',time());
$years\u then=$then->format('Y');
$years\u now=$now->format('Y');
$years=$years\u now-$years\u then;
$months\u then=$then->format('m');
$months\u now=$now->format('m');
$months=$months\u now-$months\u then;
$days_then=$then->format('d');
$days_now=$now->format('d');
$days=$days\u now-$days\u then;
$hours_then=$then->format('H');
$hours_now=$now->format('H');
$hours=$hours\u now-$hours\u then;
$minutes_then=$then->format('i');
$minutes_now=$now->format('i');
$minutes=$minutes\u now-$minutes\u then;
$seconds_then=$then->format('s');
$seconds_now=$now->format('s');
$seconds=$seconds\u now-$seconds\u then;
如果($s<0){
$minutes-=1;
$seconds+=60;
}
如果($分钟<0){
$hours-=1;
$minutes+=60;
}
如果($h<0){
$days-=1;
$hours+=24;
}
$months\u last=$months\u now-1;
如果($months\u now==1){
$years_now-=1;
$months\u last=12;
}
//谢谢你,二年级
如果($months|u last==9 |$$months|u last==4 |$$months|u last==6 |$$months|u last==11){
$days\u last\u month=30;
}
否则如果($months\u last==2){
如果($years\u now%4)==0){
$days\u last\u month=29;
}
否则{
$days\u last\u month=28;
}
}
否则{
$days\u last\u month=31;
}
如果($天<0){
$months-=1;
$days+=$days\u上月;
}
如果($months<0){
$years-=1;
$months+=12;
}
返回数组('years'=>$years,'months'=>$months,'days'=>$days,'hours'=>$hours,'minutes'=>$minutes,'seconds'=>$seconds);
}

我想这正是他想要的。 我从Jerdigit的答案中实现了这一点。所有的功劳都归于他

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

(PHP>5.3)


我发现Jerdigty的解决方案对我的有些不同的问题非常有帮助。我需要显示自创建人类可读格式的行以来的最高完整时间单位,如“添加{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
        );
    }
}

谢谢,杰迪吉蒂

30天的时间差应该有多少个月?从什么时候开始有多少秒?如果我遗漏了什么,我很抱歉,但为什么不回显日期('Y-m-d H:I:s',$numberofsecs)?我认为至少应该有1000个脚本在上面。谢谢,这很有效…但我使用的是PHP5.2.17,我得到了
致命错误:调用未定义的方法DateTime::setTimeStamp()
谢谢,这在PHP5.3中是有效的……但是在PHP5.2.17中有没有办法做同样的事情?@Sylar请查看最新版本。()