Randy Hall
Randy Hall

Reputation: 8127

CakePHP TimeHelper (or similar) in controller

I have a number of records I'm pulling from a database that contain a time stamp, namely Cake's automagic created field. I want to store them AS IS, but in certain controller actions I want them to be reformatted to a "friendlier" format.

Is there a Cake method for changing the format of a datetime without changing how it's stored?

Thanks

edit

I can totally write a loop in the controller myself to modify every returned datetime. I'm hoping there's a more elegant solution.

Upvotes: 0

Views: 3147

Answers (3)

Santosh Yadav
Santosh Yadav

Reputation: 460

You can load CakeTime Utility in controller,
The caketime utility contain all functions in CakeTime Helper

App::uses('CakeTime', 'Utility');
CakeTime::timeAgoInWords('2014-04-01 00:15:20');

Upvotes: 1

Mindaugas Norvilas
Mindaugas Norvilas

Reputation: 532

You can format your date in your model's afterFind callback. From CakePHP manual:

Use this callback to modify results that have been returned from a find operation, or to perform any other post-find logic. The $results parameter passed to this callback contains the returned results from the model’s find operation.

For example, if you want to change date format, you could do this like:

public function afterFind($results, $primary = false) {
    App::uses('CakeTime', 'Utility');
    foreach ($results as $key => $val) {
        if (isset($val['Event']['created'])) {
            $results[$key]['Event']['created'] = CakeTime::format($val['Event']['created'], '%d-%m-%Y');
        }
    }
    return $results;
}

As you see, I used CakeTime utility to format the date. It's the same as Time helper, just you can use it anywhere you want.

Upvotes: 0

scrowler
scrowler

Reputation: 24406

You're right, you should leave them stored as they are in the database.

The easiest way is to use PHP's date() function. E.g. to output dd/mm/YYYY:

<?php echo date('d/m/Y', strtotime($your_datetime_field)); ?>

For consistency across a large application, you may want to use a central source for formatting the time, e.g. CakePHP's TimeHelper, so that you only have to change the format in one place. I haven't used this helper before as I haven't needed to go past PHP's inbuilt functions, but it looks like you still specify the date format. I'm sure that if it doesn't default to something you like, it would be easy to modify it. The power of this helper is in the flexible functionality, output/return options and SQL compatibility options.

It seems that it's really just a simple wrapper for most of PHP's inbuilt functions, and would save you coding time.

E.g:

// inbuilt - get timestamp
$timestamp = strtotime($yourdate);
// cake helper
$timestamp = $this->Time->fromString($yourdate);

// inbuilt - get timestamp for 3 days away
$timestamp = strtotime($yourdate . ' + 3 days');
// cake helper
$timestamp = $this->Time->fromString('+3 days');

Also seems useful for comparing and checking dates.

Docs:

Upvotes: 1

Related Questions