Errors When Running Locally

Hello,

I have created a project based on Yii that runs smoothly on my web server. I decided to make a local copy of the project that will run on MAMP.

Things work fine on some pages but on some others I am getting some errors, which are related to the way some constants have been declared.

For example, I was getting an error that said:

So I replaced in my code the


const myvar=myvar;

with the:


const myvar='myvar';

Now on some pages that display dates are giving me the following error:

So, my first question is why am I having those errors locally (PHP version 5.4.10) while on the web server (PHP version 5.2.9) the application runs smoothly.

And secondly, how to fix the date issue.

Thank you in advance for your help :)

You should use same PHP version on both your local enviroment and on the webserver! I dont have the differences in my head, but this is most likley caused but the version difference. Try upgrading your server, or downgrading your local version.

Hello again,

Thanks Solid for your reply but downgrading is not an option and sooner or later my webhost will upgrade PHP to a more modern version so i will still be having the same issue.

I have been trying these days to find solution but I haven’t been successful.

I will give a bit more of my code in case it helps.

In my main.php I have the following settings:


'format'=>array(

   'datetimeFormat'=>'d F Y H:i',

   'dateFormat'=>'d F Y',

 ),

Then in my model I have the following code




protected function afterFind() {

   $this->date_created = strtotime($this->date_created);

   $this->date_created = date ('d F Y, H:i', $this->date_created);


   parent::afterFind ();

}


protected function beforeSave(){

   if(parent::beforeSave()) {

      $this->date_created = date('d F Y, H:i');

      return true;

   }

   return false;

}



And then in the admin.php file I have this:




//'date_created',

array(

   'name'=>'date_created',

   'type'=>'date',  

),



I tried changing the database date_created field type from datetime to timestamp or integer but that didn’t help either.

I would appreciate if anyone can provide some help. Thank you :)

Because in 5.3 there were some changes regarding strict code stuff (you may encounter some errors about ‘static’ calls also). This is good, because your local server forces you to write good code.

Error is caused by this: date(‘d F Y, H:i’, $this->date_created);

Here’s the signature of date function:


string date ( string $format [, int $timestamp = time() ] )

so the second argument must be unix timestamp. Use debugging tools to see what’s actually in $this->date_created and modify if needed.

Hello Orey,

Thank you very much for your explanation and help. I already had errors about ‘static’ calls and they were easy to fix.

Regarding the issue with dates, I read the PHP documentation, googled thoroughly and searched in forums, made changes to my code but I still have not managed to resolve my issue.

The date_created field is stored in the database as datetime (e.g. 2013-04-18 20:17:49).

As you mentioned, the 2nd argument of the date function should be a unix timestamp. But I am already using strtotime to convert the datetime field into a unix timestamp.




protected function afterFind() {

   $this->date_created = strtotime($this->date_created);

   $this->date_created = date ('d F Y, H:i', $this->date_created);


   parent::afterFind ();

}



Am I missing something or doing something wrong? :blink:

Would it be helpful if I would paste something from the stack trace?

And by the way, what debugging tools would you suggest?

Thank you once again for your time and assistance. :)

Yes but DB value may be wrong, so do simple ‘echo’ of the value before the conversion and after the conversion and see if generated timestamp looks right.

Yes, stack trace would be helpful.

Hello ORey,

Thank you for your help once again.

I eventually managed to figure out what I was doing wrong but I was looking in the wrong direction.

The problem was here:




protected function afterFind() {

   $this->date_created = strtotime($this->date_created);

   $this->date_created = date ('d F Y, H:i', $this->date_created);


   parent::afterFind ();

}



I was converting the database value from datetime to string with strtotime. But then on the next line I was converting it into a string again.

So in the view.php or admin.php, when I was trying to display the value of date time in an array like this…




array(

	'name'=>'date_created',

	'type'=>'datetime',  

),



…Yii was attempting to convert the string to datetime.

I hope this helps others who might come across such a (silly) issue.

Thanks :)