I'm trying to return a list of Time formatted fields from a MySQL database and using the Cake\I18n\Time class to manipulate them. Currently, when the data is taken from the database, the current date is used for these fields. I'd like to set my own date on them (as the data can be for multiple dates) but I can't seem to find an elegant solution.
So far my solution during testing has been to set the date using Time::setTestNow($now)
and then creating a function in my Entity to set the date correctly, like so:
$newTime = new Time();
$newTime->hour($savedTime->format("H"))
->minute($savedTime->format("i"))
->second($savedTime->format("s"));
Obviously this seems like a fairly cumbersome method of setting the date for these fields. I have other functions in my application that will need to compare dates as well as times (the date can be taken from another table) so Time::setTestNow()
seems like the wrong way to properly set the correct date, and setting the day, month and year for each new time also seems to be a cumbersome solution.
Are there other methods I can be using to set the date when the time is pulled from the database? I'd much rather have the correct time in my Model than have to convert it each time I want to use it).
Thanks.