计算与字段的年差-从今天开始到今天结束

时间:2019-09-12 22:40:27

标签: php

此代码计算字段之间的年差 “开始” 和 今天。 我有另一个领域 “结束”。 我希望它在编译时 “开始” 和 “结束” 帐户今天停止, 如果只编译 “开始” 从今天开始计数


<?php

$start = $row['start'];

$dateOfStart = $start;
$today = date("Y-m-d");
$diff = abs($dateOfStart - $today);

$a=explode(' ',$diff);
if($a[0]>1){
    $y=$a[0]."  Years  ";
}else{$y=$diff."  Year";}


echo   '<font color="blue">'. $y . '&nbsp' . In . '&nbsp' . The . '&nbsp' . Business . '</font>';

?>  

1 个答案:

答案 0 :(得分:1)

<?php

$start = $row['start'];

$dateOfStart = $start;
if(isset($row['end'])){
   $dateOfEnd = $row['end'];
}
else{
   $dateOfEnd = date("Y-m-d");
}
$diff = abs($dateOfStart - $dateOfEnd);

/* NOTE: the value of $diff at that time is an integer, 
so you don't need explode ??. In fact $a[0] will 
always be >= 0, and is equal to $diff, so you should 
consider checking the end of your script to... */

$a=explode(' ',$diff);
if($a[0]>1){
    $y=$a[0]."  Years  ";
}else{$y=$diff."  Year";}

/* NOTE: Why not simply this instead: */
$y=$diff."  Year"; 
if($diff>1){
    $y.="s";
}

echo   '<font color="blue">'. $y . '&nbsp' . In . '&nbsp' . The . '&nbsp' . Business . '</font>';

?>