-1

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

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$xmth写两次同一行?回覆: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>
4

2 回答 2

1

if( statement || statement)如果任一陈述为真,则将通过。

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

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

于 2012-10-04T21:34:48.577 回答
0

我相信 OR 语句是您正在寻找的。

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

请阅读PHP 运算符

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

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

代替:

if(something) { 
    if(somethingelse) {
       //do this
 }
于 2012-10-04T21:36:11.177 回答