【发布时间】:2011-04-04 22:10:51
【问题描述】:
我用两种方式都尝试过相同的代码,第一种方式有效,当我采用第二种方式时,它不会出错,但似乎什么也没做。
我在 Drupal 的视图中获得了一些值(两个日期)。我可以打印这些值并获得与我明确设置的完全相同的值。我已经使用 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/>";
}
?>
【问题讨论】:
-
使用
print_r或var_dump进行调试。 -
尝试检查
$fields['field_deal_to_value']->content的值。 -
当你使用 print_r($fields['field_deal_from_value']->content) 和 print_r($fields['field_deal_to_value']->content) 会发生什么?您是否尝试使用 PHP 的 DateTime 类进行日期操作?
-
这看起来像是 for 循环的反模式。把这些东西拿出来,否则将来你再回来就会很痛苦。
-
当我使用两个 print_r 语句时,它只会正确打印出两个日期,正如我所期望的那样