Calculation with DateTime class causes php range() error...

Calculation with DateTime class causes php range() error…

Possibly a bug?

Insert a simple DateTime calculation with DateTime::diff() in the index action of the default tutorial yii application.

Astonishing is that it causes an php range() error when you change from the home view to the contact view.

It also seem that the more often you call the diff() the greater is the chance to get the error. In other words the more often you recall the home view the greater is the chance to get the error when you switch to the contact view.

Here the code:

date_default_timezone_set(‘Europe/London’);

//Aktuelle Zeit als DateTime

$dakt = new DateTime("2011-10-15 10:45:00");

//Endzeit Zeit als DateTime

$dend = new DateTime("2011-10-15 10:00:00");

$intv = $dakt->diff($dend);

OS: Windows XP

Yii: 1.1.8.r3324

XAMPP for win: 1.7.4

Why you provide Yii version ? :lol:

it is not Yii class, it is php class

check this out http://php.net/manual/en/datetime.diff.php

DateTime is very strict in the date format You give it…

also put attention to echo $interval->format(’%R%a days’);

I do not find any error in the use of DateTime.

I test with:

<?php

date_default_timezone_set(‘Europe/London’);

//Aktuelle Zeit als DateTime

$dakt = new DateTime("2011-10-15 10:45:00");

//Endzeit Zeit als DateTime

$dend = new DateTime("2011-10-15 10:00:00");

$intv = $dakt->diff($dend);

echo 'Zeit1: ’ . $dakt->format(‘Y-m-d H:i:s’) . “<br>”;

echo 'Zeit2: ’ . $dend->format(‘Y-m-d H:i:s’) . “<br>”;

echo $intv->format(’%H:%I:%S’) . “<br>”;

?>

Result printed is correct:

Zeit1: 2011-10-15 10:45:00

Zeit2: 2011-10-15 10:00:00

00:45:00

but used with yii described as i mention above i got a php error…

PHP Error

range() [<a href=‘function.range’>function.range</a>]: step exceeds the specified range

C:\xampp\htdocs\yii\framework\web\helpers\CJavaScript.php(83)

71 if($value===-INF)

72 return ‘Number.NEGATIVE_INFINITY’;

73 else if($value===INF)

74 return ‘Number.POSITIVE_INFINITY’;

75 else

76 return rtrim(sprintf(’%.16F’,$value),‘0’); // locale-independent representation

77 }

78 else if(is_object($value))

79 return self::encode(get_object_vars($value));

80 else if(is_array($value))

81 {

82 $es=array();

83 if(($n=count($value))>0 && array_keys($value)!==range(0,$n-1))

84 {

85 foreach($value as $k=>$v)

86 $es[]="’".self::quote($k)."’:".self::encode($v);

87 return ‘{’.implode(’,’,$es).’}’;

88 }

89 else

.

.

.

I have the same problem, with Yii 1.1.6 version. When i use DateTime diff. Please any solutions idea?

I have this problem too. Yii 1.1.9 version. :angry:

Ran into the same problem. Seems to be platform dependent, according to this: code.google.com/p/yii/issues/detail?id=2317. It happened for me on WAMPSERVER with PHP 5.3.0 and Apache 2.2.11. The problem went away when I switched to PHP 5.3.13 on Apache 2.2.22. The 4th comment on the above linked bug report suggests a solution which worked on the older version, but was not necessary on the more recent one.

hi i am trying to show in a custom column grid that shows me the remains of two of the fields grid, and what would they be fecha_det_a1 fecha_ocu_a1

this function should develop in my admin because that’s where I want it to display. Already declared the variable in the model (), now this is the function:

<?php

function dateDiff($start, $end)

{

$start_ts = strtotime($start);

$end_ts = strtotime($end);

$diff = $end_ts - $start_ts;

return round($diff / 86400);

}

$fdfo=dateDiff($model->fecha_det_a1,$model->fecha_ocu_a1);

?>

When I invoke the function within the grid, I do soÑ

<?php $this->widget(‘zii.widgets.grid.CGridView’, array(

‘id’=>‘anexo1-grid’,

‘itemsCssClass’=>“table table-striped”,

‘dataProvider’=>$model->search(),

.

.

‘columns’=>array(

‘id’,

array(‘name’=>‘no_hc_a1’,‘value’=>’$data->no_hc_a1’),

array(‘name’=>‘entidadhc_fk’,‘value’=>’$data->entidadhcFk->enthc_nombre’),

array(‘name’=>‘orga_hc_siglas’,‘value’=>’$data->entidadhcFk->orgaFk->orga_hc_siglas’),

.

.

array(‘name’=>‘fdfo’,‘value’=>$fdfo),

.

.

array(

‘class’=>‘CButtonColumn’,

),

),

)); ?>

but when I go to access the admin view returns me the following error:

call_user_func_array() expects parameter 1 to be a valid callback, no array or string given

could you help me? thanks