本文介绍了[PHP]:具有日期差异的逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求一个优雅的解决方案,根据现在之间的时间长度和数据库中的ISO格式日期来更改时间格式。



I 'd喜欢输出看起来像这样

  //少于一个小时
'X分钟已经过去了'
//少于24小时
'X小时已经通过'
//大于24小时
ISO日期
/ pre>

这是我到目前为止...

  $ now = date(omd H:i:s)// now =像'2009-12-28 16:39:00'

$ dateExample ='2009-12 16: 37:00'

$ timeSpan = round(strtotime($ now) - strtotime($ dateExample));

if(($ timeSpan / 60)< = 60)
{
echo $ timeSpan。minutes;
}
if(($ timeSpan /(60 * 60))< = 24)
{
echo($ timeSpan /(60 * 60)) ;
}
else
{
echo $ dateExample;
}

如果语句真的困扰着我,我似乎无法想象出一个更好的方式来做...

解决方案

这是我最后做的事情。 b
$ b

  $ now = date(omd H:i:s); 

$ timeSpan = round(strtotime($ now) - strtotime($ lastCheckIn));

$ minuteCheck = round($ timeSpan / 60);
$ hourCheck = round($ timeSpan /(60 * 60));

if($ minuteCheck {
echo $ minuteCheck。minutes;
}
else if($ hourCheck< = 24)
{
echo $ hourCheck。hours;
}
else
{
echo $ lastCheckIn;
}

整个分钟/小时检查真的使IMO更加清晰IMO


I'm seeking an elegant solution to change the format of time based on the length of time between now, and a ISO formated date in a DB.

I'd like the output to look something like this

//less than an hour
'X minutes have gone by'
//less than 24 hours
'X hours have gone by'
//greater than 24 hours
ISO date

Here is what I have so far...

$now = date("o-m-d H:i:s")  //now = something like '2009-12-28 16:39:00'

$dateExample = '2009-12 16:37:00'

$timeSpan = round(strtotime($now) - strtotime($dateExample));

if(($timeSpan/60)<=60)
{
  echo $timeSpan." minutes";
}
if(($timeSpan/(60*60))<=24)
{
  echo ($timeSpan/(60*60))." Hours";
}
else
{
  echo $dateExample;
}

The sloppy if statements are really bothering me and I can't seem to figure out a better way to do it....

解决方案

Here is how I ended up doing it.

$now = date("o-m-d H:i:s");

$timeSpan = round(strtotime($now) - strtotime($lastCheckIn));

$minuteCheck = round($timeSpan/60);
$hourCheck   = round($timeSpan/(60*60));

if($minuteCheck<=60)
{
  echo $minuteCheck." minutes";
}
else if($hourCheck<=24)
{
    echo $hourCheck." hours";
}
else
{
    echo $lastCheckIn;
}

The whole minute/hour check really makes it much more clear IMO

这篇关于[PHP]:具有日期差异的逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 00:26