多个IF语句不起作用?

时间:2014-03-07 18:54:55

标签: php if-statement

我正在研究一些代码,这些代码会在一个月内变为数字。

$postdate = "09:24:33 Mar 07, 2014 PST";

//SPLIT UP DATE
$hour = substr("$postdate", 0, -23);
$min = substr("$postdate", 3, -20);
$sec = substr("$postdate", 6, -17);
$month = substr("$postdate", 9, -13);
$day = substr("$postdate", 13, -10);
$year = substr("$postdate", 17, -4); 

//SET MONTH TO NUMBER
if($month = "Jan") {$month = 01;}
if($month = "Feb") {$month = 02;}
if($month = "Mar") {$month = 03;} 
if($month = "Apr") {$month = 04;} 
if($month = "May") {$month = 05;} 
if($month = "Jun") {$month = 06;}
if($month = "Jul") {$month = 07;} 
if($month = "Aug") {$month = 08;} 
if($month = "Sep") {$month = 09;}
if($month = "Oct") {$month = 10;} 
if($month = "Nov") {$month = 11;} 
if($month = "Dec") {$month = 12;} 

//Display Month
month: <?php echo $month ?>

为什么这不起作用?如果我只使用一个语句,它工作正常。我也会在这里走很远的路吗?是否已有一个功能可以将一个月变为其数量?

感谢。

4 个答案:

答案 0 :(得分:5)

比它需要的方式更复杂:

echo date('m', strtotime("09:24:33 Mar 07, 2014 PST"));
// Output: 03

Demo

答案 1 :(得分:4)

你正在使用=你应该在if语句中使用==(或= =如果$ month是一个字符串)。

答案 2 :(得分:1)

PHP有一个名为DateTime的本机类,可以使用您想要的日期执行几乎所有操作。您应该看看它的方法以及如何使用它们,它是PHP网站上记录的所有内容,它将为您完成工作。

答案 3 :(得分:0)

您需要比较而不是作业

==而非=

所以第一行应该是

if($ month ==“Jan”){$ month = 01;}

最好还是使用关联列表

相关问题