开发者

Why doesn't php behave the same as when I explicitly set a value?

I have tried the same code both ways, it works the first way, when I do it the second way, it doesn't error, but just appears to do nothing.

I'm getting some values (two dates) in a View in Drupal. I can print the values and get EXACTLY the same values as I have set explicitly. I've tested this using print.

Although the values using print are identical to those i've set explicitly, it's not working with the data pulled from Drupal.

Example of printing:

$fields['field_deal_from_value']->content;

//The result from this is that it prints the following:

2011-04-24

Version 1 - Working with explicitly set value

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

Version 2 - Not Working - Values are definitely set correctly

<?php
$pastDateStr = $fields['field_deal_from_value']->content; 
$pastDateTS = strtotime($pastDateStr); 

for ($currentDateTS = $pastDateTS; $currentDateTS <= strtotime($fields['field_dea开发者_如何转开发l_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/>"; 
}
?>


You should turn error reporting all the way so you can see warnings.
I suspect you'll see an error saying that the result of $fields['..']->content cannot be used in a write context (ie as the argument of strtotime).
I'd try to save the value of $fields['..']->content in a variable and use that variable in the loop's condition.


Look at this:

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


?>

works fine because i've set the $fields['field_deal_to_value']!


You have a typo or logic error. If you replace this line

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

with

for ($currentDateTS = $pastDateTS; $currentDateTS <= strtotime("2011-05-28"); $currentDateTS += (60 * 60 * 24)) { 

then you'll get the same output. The difference is what you're comparing in the for loop.

In your code the second sample is essentially only asking it to iterate once while the first iterates several times.


I'd recommend setting up XDebug and stepping through the code so that you can see exactly what's happening. It works fine with many of the common IDEs (NetBeans, Eclipse, etc.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜