0

I'm trying to use CakePHP's TimeHelper.

Let's say I have this date: Tue, 07 Jun 2011 10:53:31 GMT

Its' epoch time is: 1307444011

I need to get difference output with now, like this: 2 years, 24 days, 15 hours, 27 minutes and 43 seconds

I tried this:

$userCreatedTimeStr = $this->Time->timeAgoInWords(
       1307444011, array(
        'end' => '+10 year',
        'accuracy' => array('second' => 'second')
    )
);

But this code gives 2 years ago.

How can I fix this?

Edit: some test code is like this: http://apigen.juzna.cz/doc/cakephp/cakephp/source-class-CakeTimeTest.html#189-229

It seems like using core PHP functions is mandatory, rather than TimeHelper.

How to calculate the difference between two dates using PHP?

How to get time difference in minutes in PHP

Community
  • 1
  • 1
trante
  • 33,518
  • 47
  • 192
  • 272

1 Answers1

3

Try putting in the 'format' option to the options array?

E.g:

$userCreatedTimeStr = $this->Time->timeAgoInWords(
       1307444011, array(
        'end' => '+10 year',
        'format' => 'F jS, Y',
        'accuracy' => array('second' => 'second')
    )
);

EDIT: CakePHP limits the timeAgoInWords method like so:

If the difference is one week or more, the lowest level of accuracy is day

Source: http://api.cakephp.org/2.3/class-CakeTime.html#_timeAgoInWords

I guess what you were trying to do simply isn't possible using this method, at least in version 2.3.

SharkofMirkwood
  • 11,483
  • 2
  • 17
  • 25
  • This gives 2 years, 3 weeks, 3 days ago – trante Jul 01 '13 at 11:22
  • Yeah, it was just an example. I don't know the parameters for PHP time very well, but it gives an idea of what you need. If you wait a few minutes I can probably figure out exactly what to type. – SharkofMirkwood Jul 01 '13 at 11:26
  • Hmm, after testing, it doesn't seem to make much of a difference. Tried removing the 'format' option from my code and I get the same output... Seems strange to me... – SharkofMirkwood Jul 01 '13 at 11:38
  • Okay, I've been trying to get this working for ages! And just now came across this: http://api.cakephp.org/2.3/class-CakeTime.html#_timeAgoInWords `NOTE: If the difference is one week or more, the lowest level of accuracy is day` So I guess that's why my answer is the closest you can get - probably going to have to use PHP's date function to get more accuracy. It seems weird to me that they'd limit it like this though... – SharkofMirkwood Jul 01 '13 at 12:04
  • Hmm. Thank you for your effort. – trante Jul 01 '13 at 12:22