【问题标题】:Why doesn't php behave the same as when I explicitly set a value?为什么 php 的行为与我明确设置值时的行为不同?
【发布时间】: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_rvar_dump 进行调试。
  • 尝试检查$fields['field_deal_to_value']-&gt;content 的值。
  • 当你使用 print_r($fields['field_deal_from_value']->content) 和 print_r($fields['field_deal_to_value']->content) 会发生什么?您是否尝试使用 PHP 的 DateTime 类进行日期操作?
  • 这看起来像是 for 循环的反模式。把这些东西拿出来,否则将来你再回来就会很痛苦。
  • 当我使用两个 print_r 语句时,它只会正确打印出两个日期,正如我所期望的那样

标签: php string drupal date


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    看看这个:

    <?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']!

    【讨论】:

    • 当我从 Drupal 6 中的视图获取数据时,该值已经设置。我需要获取 Drupal 输出的值(它确实输出正确的日期),然后使用这些值。因此,自己设置值不会实现我想要做的事情。
    • 不,我只是想表明,通过手动设置这些值,这段代码可以完美运行。所以问题一定出在你的 $fields 数组或 -> 内容中。尝试 var_dump($fields) 和 var_dump($fields['field_deal_from_value']->content,$fields['field_deal_to_value']->content)
    • 谢谢,我今天下午晚些时候试试。不知道为什么它不起作用。我可能只是再次从数据库中获取值,这将是一个额外的调用,但无论如何页面中没有太多内容
    【解决方案3】:

    您有错字或逻辑错误。如果你替换这一行

    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 循环中比较的内容。

    在您的代码中,第二个示例本质上只是要求它迭代一次,而第一个示例会迭代几次。

    【讨论】:

    • 我不太明白。当输入正确的数据时,这两行有什么区别?
    【解决方案4】:

    我建议您设置 XDebug 并单步执行代码,这样您就可以准确地看到正在发生的事情。它适用于许多常见的 IDE(NetBeans、Eclipse 等)

    【讨论】:

      猜你喜欢
      • 2019-09-10
      • 2016-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多