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

Friday, March 18, 2022

[FIXED] Convert time object of cakephp 3 in Y-m-d format

 March 18, 2022     cakephp, cakephp-3.0     No comments   

Issue

I am working in cakephp 3 and I want to print my time object in Y-m-d format.

This is my object

'expiry' => object(Cake\I18n\Time) {
        'time' => '2015-07-31T00:00:00+0000',
        'timezone' => 'UTC',
        'fixedNowTime' => false
},

Can anyone help me with this?


Solution

In Cakephp 3.* you use the Time::i18nFormat() method. It worked for me

  $customformat = $date->i18nFormat('YYY-MM-dd');

Edit:

I looked through the 3.3 docs and found this good example. There are constants you can use to format dates for common use cases like Time::UNIX_TIMESTAMP_FORMAT,\IntlDateFormatter::FULL

$time = new Time('2014-04-20 22:10');
$time->i18nFormat(); // outputs '4/20/14, 10:10 PM' for the en-US locale
$time->i18nFormat(\IntlDateFormatter::FULL); // Use the full date and time format
$time->i18nFormat([\IntlDateFormatter::FULL, \IntlDateFormatter::SHORT]); // Use full date but short time format
$time->i18nFormat('yyyy-MM-dd HH:mm:ss'); // outputs '2014-04-20 22:10'
$time->i18nFormat(Time::UNIX_TIMESTAMP_FORMAT); // outputs '1398031800'

CakePHP documentation

Edit 2:

Also look at the time helper class

You can reference it in views like so:

$this->Time->format($format);



Answered By - Peter Chaula
  • 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