如果两者中的任何一个等于

时间:2012-10-04 21:31:38

标签: php date datetime calendar logical-operators

具有以这种格式存储在数据库中的开始日期和结束日期:

start date= 20121004     //4th October 2012
end date= 20121004     //16th November 2012

所以我可以使用日期格式:

$date = date("Ymd"); // returns: 20121004

确定何时显示而不显示

重新填充我使用的更新输入框:

$start=(str_split($stdate,4));// START DATE: splits stored date into 2x4 ie: 20121209 = 2012 1209
$syr =  $start[0];// re first half ie: 2012 which is the year
$start2 = $start[1];//re second half ie: 1209
$start3=(str_split($start2,2));// splits second half date into 2x2 ie: 1209 = 12 09
$smth = $start3[0]; // first half = month ie: 12
$sday = $start3[1];  // second half = day ie: 09
$expiry=(str_split($exdate,4)); ///SAME AGAIN FOR EXPIRY DATE ...
$xyr =  $expiry[0];
$expiry2 = $expiry[1];
$expiry3=(str_split($expiry2,2));
$xmth = $expiry3[0]; 
$xday = $expiry3[1];

工作正常,但我需要重新填充数据库中显示日期的月份的输入框

<option value="01">January</option`>
使用

if ($smth==01):$month='January'; endif;
if ($xmth==01):$month='January'; endif;
// if the start and/or expiry month number = 01 display $month as January
if ($smth==02):$smonth='February'; endif;
if ($xmth==02):$smonth='February'; endif;
if ($smth==03):$month='March'; endif;

<select name="stmonth" class="input">
<option value="<?=$smth?>"><?=$month?></option>
...
</select>

是否有更简单的方法来显示IF EITHER ONE EQUALS,而不是每次 $ smth AND $ xmth 必须两次写同一行? re:if ($smth **and or** $xmth ==01):$month='January'; endif;

====================== UPDATE ==================== 找到一种更简单的显示方式,而不使用嵌套的if循环。与原始问题无关但可能对某人有用:

$months = array(X,January,February,March,April,May,June,July,August,September,October,November,December); 

///第一个记录虚拟以保持代码更相对

//simple swith command
switch ($smth){
case 1: $s = 1; break;
case 2: $s = 2; break;
case 3: $s = 3; break;
case 4: $s = 4; break;
case 5: $s = 5; break;
case 6: $s = 6; break;
case 7: $s = 7; break;
case 8: $s = 8; break;
case 9: $s = 9; break;
case 10: $s = 10; break;
case 11: $s = 11; break;
case 12: $s = 12; break;
default; }
switch ($xmth){
case 1: $x = 1; break;
case 2: $x = 2; break;
case 3: $x = 3; break;
case 4: $x = 4; break;
case 5: $x = 5; break;
case 6: $x = 6; break;
case 7: $x = 7; break;
case 8: $x = 8; break;
case 9: $x = 9; break;
case 10: $x = 10; break;
case 11: $x = 11; break;
case 12: $x = 12; break;
default; }

然后显示如下:

<select name="stmonth" class="input">
<option value="<?=$smth?>"><?=$months[$s]?></option>

2 个答案:

答案 0 :(得分:1)

如果任一陈述为真,

if( statement || statement)将通过。

if( $smth == 1 || $xmth == 1)

请注意,带有前导0的数字被视为八进制数字,因此09将变为0,因为9不是有效的八进制数字。

答案 1 :(得分:0)

我相信你正在寻找OR语句。

if ($smth == 1 || $xmth == 1)

请阅读PHP Operators

如果它们都必须是正确的,那么有一个AND,而不是使用您将使用的嵌套if语句:

if(something && somethinelse) {
    //do this 
}

而不是:

if(something) { 
    if(somethingelse) {
       //do this
 }