Good afternoon, I want to make comments on the site to the goods as on the site lamoda.ru http://joxi.ru/Gq6qU_3JTJA8Y463FBY implemented everything, except for the date. Those. I need to display not the date when the comment was left, but the number of the past days, weeks, months after the comment, in the form of a sentence "one day ago" or at least "1 day ago, 1 week 3 days ago". the main problem with the observance of the declination of words in the sentence (day, day, day, etc.) Advise how to implement the required logic? Naturally, the date of leaving a comment and the current date I can get.

Thank you in advance!

  • Any framework used? --- I did this thing on Yii, I did the thing, I hope the project will be completed in a week (in the current version it is unlikely to start in turnips, because I started keeping this turnip): component / localization --- If it is very rough, then you need to get a number and make a table of endings or "translations", like mine. - etki
  • To be honest, this conclusion annoys me immensely. I don’t want to do calculations when it was “a week and two days ago,” especially since there are already 24 hours in one day, and there is no information about the exact time in this format. Not to mention the fact that “an hour ago” is from the moment the page loads, which may hang in the window for another hour while you switch to it again, and the relevance of such information is zero. - user6550

2 answers 2

To calculate, subtract from the current date (microtime) the date of the comment. About the endings found in the open spaces:

function plural_type($n) { return ($n%10==1 && $n%100!=11 ? 0 : ($n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20) ? 1 : 2)); } $_plural_years = array('год', 'года', 'лет'); $_plural_months = array('месяц', 'месяца', 'месяцев'); $_plural_days = array('день', 'дня', 'дней'); $_plural_times = array('раз', 'раза', 'раз'); $var = 1; echo $var.' '.$_plural_years[plural_type($var)]; //1 год $var = 3; echo $var.' '.$_plural_days[plural_type($var)]; //3 дня $var = 5; echo $var.' '.$_plural_months[plural_type($var)]; //5 месяцев $var = 8; echo $var.' '.$_plural_times[plural_type($var)]; //8 раз 

Learn to use search.

  • I have already rummaged everything, could not find what I need - maler1988

I will add to the answer above how to perform the calculation.

 $postDate = new DateTime('2014-02-15'); $nowDate = new DateTime('2014-06-25'); $result = $nowDate->diff($postDate); print_r($result); //Результат DateInterval Object ( [y] => 0 //прошло лет [m] => 4 // месяцев [d] => 10 // дней [h] => 0 // часов [i] => 0 // минут [s] => 0 // секунд [weekday] => 0 [weekday_behavior] => 0 [first_last_day_of] => 0 [invert] => 1 [days] => 130 [special_type] => 0 [special_amount] => 0 [have_weekday_relative] => 0 [have_special_relative] => 0 ) 
  • 2
    Thank! This is really the best programming site, 90% of the questions I have been answered here within 1 hour. - maler1988