Based on the suggested ideas, I finally came up with a working multilingual solution to return the time difference.
Be careful with me as I am a complete newbie in object-oriented style programming, and this is actually my first task. I would be more than happy if you left comments or suggested ideas.
Special thanks to John, and I'm sorry that I could not realize all your ideas. I accept your answer because he has very good suggestions and explanations!
Description
In my database, I have an added_time field that refers to the time of creation of each uploaded video in Unix timestamp format, for example. 1345284190 .
This timestamp is passed to a constructor of type new creationTime( '@' . $timestamp) ;
I have a special function that goes through each video and inserts: for example, a video was added 27 days ago.
I finally achieved this using the excellent example provided by Cokidoo on the “PHP Time Guide” and added support for multilingual websites, replacing years ago , months ago , days ago , etc. any text received from the language file.
Tweaking
I changed the initial values (e.g. a year ago, a month ago, a day ago, etc.) that were written in English (to be able to use them on multilingual websites) to something that could be easily processed year_ago .: year_ago , month_ago , day_ago , etc.
Then I blew up the original $value variable that returns the output, for example: 1 year ago, 1 month ago, 1 day ago and used sprintf as the return, as suggested.
Finally, I added a getLocalized function that would connect this class with my language files.
This is the final and working code that does not cause any warnings (Warnings are included; PHP version 5.4.6).
I renamed the class name from Cokidoo_DateTime to creationTime :
class creationTime extends DateTime { private $strings = array( 'y' => array('1 year_ago', '%d years_ago'), 'm' => array('1 month_ago', '%d months_ago'), 'd' => array('1 day_ago', '%d days_ago'), 'h' => array('1 hour_ago', '%d hours_ago'), 'i' => array('1 minute_ago', '%d minutes_ago'), 's' => array('just_now', '%d seconds_ago'), ); private function getLocalized($string) { global $lang; $string == 'year_ago' ? $string = $lang['global.year_ago'] : null; $string == 'years_ago' ? $string = $lang['global.years_ago'] : null; $string == 'month_ago' ? $string = $lang['global.month_ago'] : null; $string == 'months_ago' ? $string = $lang['global.months_ago'] : null; $string == 'day_ago' ? $string = $lang['global.day_ago'] : null; $string == 'days_ago' ? $string = $lang['global.days_ago'] : null; $string == 'hour_ago' ? $string = $lang['global.hour_ago'] : null; $string == 'hours_ago' ? $string = $lang['global.hours_ago'] : null; $string == 'hour_ago' ? $string = $lang['global.hour_ago'] : null; $string == 'hours_ago' ? $string = $lang['global.hours_ago'] : null; $string == 'minute_ago' ? $string = $lang['global.minute_ago'] : null; $string == 'minutes_ago' ? $string = $lang['global.minutes_ago'] : null; $string == 'just_now' ? $string = $lang['global.just_now'] : null; $string == 'seconds_ago' ? $string = $lang['global.seconds_ago'] : null; return $string; } public function __toString() { $now = new DateTime('now'); $diff = $this->diff($now); foreach($this->strings as $key => $value){ if( ($text = $this->getDiffText($key, $diff)) ){ return $text; } } return ''; } protected function getDiffText($intervalKey, $diff){ $pluralKey = 1; $value = $diff->$intervalKey; if($value > 0){ if($value < 2){ $pluralKey = 0; } $value = explode(' ', sprintf($this->strings[$intervalKey][$pluralKey], $value) ); return sprintf('%d %s', implode(array_slice($value, 0)), $this->getLocalized(implode(array_slice($value, 1)))); } return null; } }
EDITED
This is a great solution, does the same, but has the best formatting and structure ( Jon ).
class DateFormatter { private $localizer; private $strings = array( 'y' => array('global.years_ago_1', 'global.years_ago_n'), 'm' => array('global.months_ago_1', 'global.months_ago_n'), 'd' => array('global.days_ago_1', 'global.days_ago_n'), 'h' => array('global.hours_ago_1', 'global.hours_ago_n'), 'i' => array('global.minutes_ago_1', 'global.minutes_ago_n'), 's' => array('global.seconds_ago_1', 'global.seconds_ago_n'), ); public function __construct(Localizer $localizer) { $this->localizer = $localizer; } public function formatHowLongAgo(DateTime $date) { $now = new DateTime('now'); $diff = $date->diff($now); foreach($this->strings as $unitOfTime => $formatStrings){ $howMany = $diff->$unitOfTime; if (!$howMany) { continue; } $plural = $howMany > 1; return $this->localizer->format($this->strings[$unitOfTime][$plural], $howMany); } return '??'; } }