问题
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....
回答1:
A little improvement:
$dateExample = '2009-12 16:37:00'
function timepass($dt){
$dt = (is_int($dt) ? $dt : strtotime($dt));
$timeSpan = round(time() - $dt);
if(($timeSpan/60)<=60){
return $timeSpan." minutes";
}elseif(($timeSpan/(60*60))<=24){
return ($timeSpan/(60*60))." hours";
}else{
return date('o-m-d H:i:s',$dt);
}
}
echo timepass($dateExample);
回答2:
You really can't get rid of the if statements, but if your point is to condense the code, this does the same thing:
function timePassed($date) {
$diff = time() - strtotime($date);
return $diff > 86400 ? $date : ($diff > 3600 ? ($diff / 3600).' hours' : ($diff / 60).' minutes');
}
echo timePassed('2009-12-29 16:30:00');
Not sure if that's good for readability, though.
回答3:
You could simply shuffle the if's into a switch if you're concerned with the layout.
For example:
$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));
switch(true) {
case (($timeSpan/60)<=60): { echo $timeSpan." minutes"; break; }
case (($timeSpan/(60*60))<=24): { echo ($timeSpan/(60*60))." Hours"; break; }
default: { echo $dateExample; }
}
That said, this is just syntactic sugar.
回答4:
If you 're concerned about the code being clear to readers of your library date conversion function, then you might want to consider this way (rewriting the function posted by thephpdeveloper):
function timepass($dt){
$timeSpan = round(time() - (is_int($dt) ? $dt : strtotime($dt)));
$hours = intval($timeSpan / SECONDS_PER_HOUR); // define this somewhere
$minutes = intval($timeSpan / SECONDS_PER_MINUTE); // define this somewhere
if($hours == 0) {
return $minutes.' minutes';
}
else if($hours < 24) {
return $hours.' hours';
}
else {
return ''; // your ISO date logic here
}
}
I don't see how you can get rid of the if, but personally I find this code to be super clear on what the logic for selecting a format to return is and what is the returned value in each case.
回答5:
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
来源:https://stackoverflow.com/questions/1972167/php-logic-with-date-differences