PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 11, 2022

[FIXED] CakePHP 3.6: Object of class Cake\I18n\FrozenTime could not be converted to int

 March 11, 2022     cakephp, php, time     No comments   

Issue

I am trying to check if a date is later than a week ago in index.ctp:

(((!isset($task->date_end) || is_null($task->date_end))? 
        strotime('now') : $task->date_end) > strtotime('-1 week'))

But I receive this error:

Object of class Cake\I18n\FrozenTime could not be converted to int

To check if there is anything wrong with the dates in the database I changed them all to : 2019-01-02 05:06:00.000000


Solution

When you compare a non-integer to an integer, PHP's type juggling will try to convert the former to an integer, and FrozenTime objects cannot be converted to integers.

You can avoid this fragile construct by using date objects all the way, and use the comparison methods provided by them, for example.

$result = true;
if ($task->date_end !== null) {
    $lastWeek = \Cake\I18n\Time::now()->subWeek(1);
    $result = $task->date_end->gt($lastWeek);
}

See also

  • PHP Manual > Language Reference > Types > Type Juggling
  • Cookbook > Date & Time
  • Cookbook > Chronos


Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing