我有这个代码应该改变输出的日期,但我没有得到任何改变。任何人都可以看到有什么问题吗?
<?php
$tempStartDate = "2011-07-04 15:00:00";
$TempDaysFromEvent = "-14";
$newtempStartDate = explode(" ", $tempStartDate);
echo "New Temp Start Date: ".$newtempStartDate[0];
echo "<br>";
list($year, $month, $day) = explode("-", $newtempStartDate[0]);
echo $year." ". $month. " ". $day;
echo "<br>";
$tempStartDate = $tempEndDate = date("Y-m-d", mktime(0,0,0,$month,($day+$tempDaysFromEvent),$year));
echo "TempStart Date:".$tempStartDate."<br>";
?>
这给了我这个输出:
New Temp Start Date: 2011-07-04
2011 07 04
TempStart Date:2011-07-04
但我希望它能给我:
New Temp Start Date: 2011-07-04
2011 07 04
TempStart Date:2011-06-20
答案 0 :(得分:2)
您应该收到此通知:
Notice: Undefined variable: tempDaysFromEvent in C:\tmp\test.php on line 12
您的变量实际上是$TempDaysFromEvent
。 PHP中的变量区分大小写。
答案 1 :(得分:2)
您定义$TempDaysFromEvent
时有资金,因此请将其更改为
$tempStartDate = $tempEndDate = date("Y-m-d", mktime(0,0,0,$month,($day+$TempDaysFromEvent),$year));
答案 2 :(得分:1)
你有错字
$tempDaysFromEvent
代替$TempDaysFromEvent
答案 3 :(得分:1)
一个不太复杂的解决方案:
$tempStartDate = "2011-07-04 15:00:00";
print date('Y-m-d H:i:s', strtotime('-14 days', strtotime($tempStartDate)));