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

Tuesday, January 25, 2022

[FIXED] CakePHP 3 : date comparison in view

 January 25, 2022     cakephp, cakephp-3.2, date-comparison     No comments   

Issue

I'm working on CakePHP 3.2.

I want to compare date from timestamp in database with isThisMonth() function of CakePHP.

I am listing products whose created date is within 1 month. It will show NEW badge with product.

This is what I have done in view

<?php $date = new DateTime($product->created);
      $date = $date->format('Y-m-d');
      if ($date->isThisMonth()): ?>   // error is pointing this line
         <span class="new-product"> NEW</span>
<?php endif; ?>

Here $product is an error item and created is a timestamp column in database.

But this gives error as

Call to a member function isThisMonth() on string

Solution

Try adding this below $date = $date->format('Y-m-d');

$time = new Time($date);

then swap $date->isThisMonth()): with $time->isThisMonth()):

http://book.cakephp.org/3.0/en/core-libraries/time.html#comparing-with-the-present

include use Cake\I18n\Time; in view.ctp.



Answered By - bill
  • 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