我已经尝试了两种方式相同的代码,它以第一种方式工作,当我以第二种方式执行时,它不会出错,但似乎什么都不做。
我在Drupal的View中得到了一些值(两个日期)。我可以打印值并获得与我明确设置的值完全相同的值。我用print打印过这个。
虽然使用print的值与我明确设置的值相同,但它不能处理从Drupal中提取的数据。
打印示例:
$fields['field_deal_from_value']->content;
//The result from this is that it prints the following:
2011-04-24
版本1 - 使用显式设置值
<?php
$pastDateStr = "2011-04-24";
$pastDateTS = strtotime($pastDateStr);
for ($currentDateTS = $pastDateTS; $currentDateTS <= strtotime("2011-05-28");$currentDateTS += (60 * 60 * 24)) {
// use date() and $currentDateTS to format the dates in between
$currentDateStr=date("d-m-Y",$currentDateTS);
print $currentDateStr."<br/>";
}
?>
版本2 - 无法正常工作 - 值无法正确设置
<?php
$pastDateStr = $fields['field_deal_from_value']->content;
$pastDateTS = strtotime($pastDateStr);
for ($currentDateTS = $pastDateTS; $currentDateTS <= strtotime($fields['field_deal_to_value']->content); $currentDateTS += (60 * 60 * 24)) {
// use date() and $currentDateTS to format the dates in between
$currentDateStr=date("d-m-Y",$currentDateTS);
print $currentDateStr."<br/>";
}
?>
答案 0 :(得分:0)
您应该一直打开错误报告,以便看到警告
我怀疑你会看到一个错误,说$ fields ['..'] - &gt;内容的结果不能在写上下文中使用(即作为strtotime的参数)。
我试图在变量中保存$ fields ['..'] - &gt;内容的值,并在循环条件中使用该变量。
答案 1 :(得分:0)
看看这个:
<?php
$pastDateStr = "2011-04-24";
$pastDateTS = strtotime($pastDateStr);
for ($currentDateTS = $pastDateTS; $currentDateTS <= strtotime("2011-05-28"); $currentDateTS += (60 * 60 * 24)) {
// use date() and $currentDateTS to format the dates in between
$currentDateStr=date("d-m-Y",$currentDateTS);
print $currentDateStr."<br/>";
}
var_dump("break");
class X {
var $content = "2011-04-24";
}
class Y {
var $content = "2011-05-28";
}
$fields['field_deal_from_value'] = new X();
$fields['field_deal_to_value'] = new Y();
$pastDateStr = $fields['field_deal_from_value']->content;
$pastDateTS = strtotime($pastDateStr);
for ($currentDateTS = $pastDateTS; $currentDateTS <= strtotime($fields['field_deal_to_value']->content); $currentDateTS += (60 * 60 * 24)) {
// use date() and $currentDateTS to format the dates in between
$currentDateStr=date("d-m-Y",$currentDateTS);
print $currentDateStr."<br/>";
}
?>
工作正常,因为我设置了$ fields ['field_deal_to_value']!
答案 2 :(得分:0)
您有拼写错误或逻辑错误。如果你替换这一行
for ($currentDateTS = $pastDateTS; $currentDateTS <= strtotime($fields['field_deal_to_value']->content); $currentDateTS += (60 * 60 * 24)) {
带
for ($currentDateTS = $pastDateTS; $currentDateTS <= strtotime("2011-05-28"); $currentDateTS += (60 * 60 * 24)) {
然后你会获得相同的输出。不同之处在于您在for循环中的比较。
在你的代码中,第二个样本基本上只是要求它迭代一次,而第一个迭代几次。
答案 3 :(得分:0)
我建议设置XDebug并逐步执行代码,以便您可以准确地看到发生了什么。它适用于许多常见的IDE(NetBeans,Eclipse等)