如何改善这种PHP方法?

时间:2018-10-22 10:08:45

标签: php

版本1

$aData = $this->get($memberA); // came from db
$bData = $this->get($memberB); // came from db
if(empty($aData)) {
    $aData['updateTime'] = '1970-01-01';
}
if(empty($bData)) {
    $bData['updateTime'] = '1970-01-01';
}
if ($aData['updateTime'] >= $bData['updateTime']) {
     // if both data time have same value will echo A
     echo "A" ; 
} else { 
    echo "B"; 
}  

版本2

$aData = $this->get($memberA); // came from db
$bData =  $this->get($memberB); // came from db
$flagA = empty($aData)? false: true; 
$flagB = empty($bData)? false: true;
if ($flagA === false && $flagB === true) {
    echo "B"; 
} else if ($flagA === true && $flagB === false) {
    echo "A";
} else if ($flagA === false && $flagB === false) { 
    echo "";
} else { 
    if ($aData['updateTime'] >= $bData['updateTime']) {
        // if both data time have same value will echo A 
        echo "A" ; 
    } else { 
        echo "B"; 
    }  
}

此函数用于比较两个不同的时间数组,并且时间数组的值不允许为空。

第一版是我的原始代码。 另外,我设置了一个标志来依赖于此,但是我不确定这是否是更好的方法。

有人有个好主意来使这段代码干净吗?

2 个答案:

答案 0 :(得分:1)

要比较日期,您需要创建一个new DateTime('1970-01-01'); 这样比较就可以正常工作。

您还需要检查if(isset($aData['updateTime'])),如果它存在,则也将字符串转换为日期时间,如下所示:

$aData['updateTime'] = new DateTime($aData['updateTime']);

$bdata

相同

答案 1 :(得分:0)

这是您Version 2的代码,以及如何进行一些修改才能使其起作用:

$aData = $this->get($memberA);
$bData = $this->get($memberB); 

// You create two empty string for the date you want to compare
$aDatetime = "";
$bDatetime = "";    

// You check if both data are not empty and if updateTime is defined
if (!empty($aData) && isset($aData['updateTime'])) {
    $aDatetime = new DateTime($aData['updateTime']);
} else {
    $aDatetime = new DateTime('1970-01-01');
}
if (!empty($bData) && isset($bData['updateTime'])) {
    $bDatetime = new DateTime($bData['updateTime']);
} else {
    $bDatetime = new DateTime('1970-01-01');
}    

您之前所做的事情:

  • 如果$bData为空且$aData不为空,则回显'A'
  • 如果$aData['updateTime'] >= $bData['updateTime']
  • ,您回显“ A”
  • 如果$aData为空且$bData不为空,则回显'B'
  • 如果$aData['updateTime'] < $bData['updateTime']
  • ,您回显“ B”
  • 如果$aData$bData均为空,则回显

您现在可以在三个条件下做到这一点:

// Echo A if $aData['updateTime'] >= $bData['updateTime'] as before
// If $bData is empty, now it's a datetime for date 1970-01-01 so still true
// Add condition to check if $aDatetime is not '1970-01-01' to avoid case both data are empty
if ($aDatetime >= $bDatetime && $aDatetime->format('Y-m-d') !== '1970-01-01') {
    echo "A" ; 
// Echo B if $aData['updateTime'] < $bData['updateTime'] as before
// If $aData is empty, now it's a datetime for date 1970-01-01 so still true
} else if ($aDatetime < $bDatetime) {
    echo "B"; 
// This is the case where $aDatetime == $bDatetime and both are '1970-01-01' so both empty
} else {
    echo '';
}

带有日期测试的输出:

  • $aData = '2018-10-23' && $bData = '2018-10-23':回显A
  • $aData = '2018-10-23' && $bData = '2018-10-20':回显A
  • $aData = '2018-10-20' && $bData = '':回显A
  • $aData = '2018-10-20' && $bData = '2018-10-23':回声B
  • $aData = '' && $bData = '2018-10-20':回声B
  • $aData = '' && $bData = '':回显”