为什么';php的行为是否与显式设置值时相同?

为什么';php的行为是否与显式设置值时相同?,php,string,drupal,date,Php,String,Drupal,Date,我用两种方法都试过同样的代码,第一种方法有效,当我用第二种方法时,它不会出错,只是看起来什么也不做 我在Drupal的视图中得到了一些值(两个日期)。我可以打印这些值,得到与我显式设置的值完全相同的值。我已经用print测试过了 虽然使用print的值与我显式设置的值相同,但它不能处理从Drupal中提取的数据 打印示例: $fields['field_deal_from_value']->content; //The result from this is that it prints

我用两种方法都试过同样的代码,第一种方法有效,当我用第二种方法时,它不会出错,只是看起来什么也不做

我在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/>"; 
}
?>

您应该一直打开错误报告,以便看到警告。
我猜想您会看到一个错误,即$fields['']->content的结果不能在写上下文中使用(即作为strotime的参数)。
我会尝试将$fields[''']->content的值保存在一个变量中,并在循环的条件中使用该变量。

看看这个:

<?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\u deal\u to\u value']

您有输入错误或逻辑错误。如果你换这条线

for ($currentDateTS = $pastDateTS; $currentDateTS <= strtotime($fields['field_deal_to_value']->content); $currentDateTS += (60 * 60 * 24)) { 
for($currentDateTS=$pastDateTS;$currentDateTS内容)$CurrentDates+=(60*60*24)){


for($currentDateTS=$pastDateTS;$currentDateTS我建议设置XDebug并逐步查看代码,以便您能够准确地看到发生了什么。它可以与许多常见IDE(NetBeans、Eclipse等)配合使用。

使用
print\r
var\u dump
进行调试。尝试检查
$字段的值['field\u-deal\u-to\u-value']->content
。使用print\r('fields['field\u-deal\u-from\u-value']->content)和print\r('fields['field\u-deal\u-to\u-value']->content)时会发生什么?您是否尝试使用PHP的DateTime类进行日期操作?这看起来像是一个for循环的反模式。中断这些内容,否则将来您回到它时会很痛苦。当我使用两个print_r语句时,它只是正确地打印出两个日期,正如我期望的那样。当我从中获取数据时,值已经设置好了Drupal 6中的一个视图。我需要获取Drupal输出的值(它输出正确的日期)然后使用这些。所以自己设置值并不能达到我想要的效果。不,我只是想表明,通过手动设置这些值,这段代码工作得很好。所以问题一定在$fields数组或->content中的某个地方。试试var_dump($fields)和var_dump($fields['field_deal_from_value']->content,$fields['field\u deal\u to\u value']->content)谢谢,我今天下午晚些时候会试试。不知道为什么它不起作用。我可能只是从数据库中再次获取值,这将是一个额外的调用,但页面中没有太多内容。我真的不明白。当输入正确的数据时,这两行之间有什么区别?
for ($currentDateTS = $pastDateTS; $currentDateTS <= strtotime("2011-05-28"); $currentDateTS += (60 * 60 * 24)) {