note 102899 added to function.date
| From: | marat dot abilov at gmail dot com | Date: | Sun, 13 Mar 2011 17:35:28 +0000 |
| Subject: | note 102899 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-177844@lists.php.net to get a copy of this message | ||
Function for get date interval in Russian format
function getDateDiff($firstDate, $secondDate) {
$firstTimeStamp = strtotime($firstDate);
$secondTimeStamp = strtotime($secondDate);
$base_day = date('j', $secondTimeStamp);
$base_mon = date('n', $secondTimeStamp);
$base_yr = date('Y', $secondTimeStamp);
$current_day = date ('j', $firstTimeStamp);
$current_mon = date ("n", $firstTimeStamp);
$current_yr = date ("Y", $firstTimeStamp);
// and now .... calculate the difference! :-)
// overflow is always caused by max days of $base_mon
// so we need to know how many days $base_mon had
$base_mon_max = date ("t",mktime (0,0,0,$base_mon,$base_day,$base_yr));
// days left till the end of that month
$base_day_diff = $base_mon_max - $base_day;
// month left till end of that year
// substract one to handle overflow correctly
$base_mon_diff = 12 - $base_mon - 1;
// start on jan 1st of the next year
$start_day = 1;
$start_mon = 1;
$start_yr = $base_yr + 1;
// difference to that 1st of jan
$day_diff = ($current_day - $start_day) + 1; // add today
$mon_diff = ($current_mon - $start_mon) + 1; // add current month
$yr_diff = ($current_yr - $start_yr);
// and add the rest of $base_yr
$day_diff = $day_diff + $base_day_diff;
$mon_diff = $mon_diff + $base_mon_diff;
// handle overflow of days
if ($day_diff >= $base_mon_max) {
$day_diff = $day_diff - $base_mon_max;
$mon_diff = $mon_diff + 1;
}
// handle overflow of years
if ($mon_diff >= 12) {
$mon_diff = $mon_diff - 12;
$yr_diff = $yr_diff + 1;
}
$result = '';
if ($yr_diff > 0) {
if ($yr_diff >= 5 && $yr_diff <= 20) {
$yearLabel = 'леÑ';
}
elseif ($yr_diff % 10 == 1) {
$yearLabel = 'год';
}
elseif ($yr_diff % 10 <= 4) {
$yearLabel = 'года';
}
else {
$yearLabel = 'леÑ';
}
$result .= $yr_diff . ' ' . $yearLabel . ' ';
}
if ($mon_diff > 0) {
if ($mon_diff == 1) {
$monthLabel = 'меÑÑÑ';
}
elseif ($mon_diff <= 4) {
$monthLabel = 'меÑÑÑа';
}
else {
$monthLabel = 'меÑÑÑев';
}
$result .= $mon_diff . ' ' . $monthLabel . ' ';
}
if ($day_diff > 0) {
if ($day_diff >= 5 && $day_diff <= 20) {
$dayLabel = 'дней';
}
elseif ($day_diff % 10 == 1) {
$dayLabel = 'денÑ';
}
elseif ($day_diff % 10 <= 4) {
$dayLabel = 'днÑ';
}
else {
$dayLabel = 'дней';
}
$result .= $day_diff . ' ' . $dayLabel . ' ';
}
return array($yr_diff, $mon_diff, $day_diff);
}
----
Server IP: 69.147.83.197
Probable Submitter: 83.221.190.79
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- https://master.php.net/note/edit/102899
Del: integrated -- https://master.php.net/note/delete/102899/integrated
Del: useless -- https://master.php.net/note/delete/102899/useless
Del: bad code -- https://master.php.net/note/delete/102899/bad+code
Del: spam -- https://master.php.net/note/delete/102899/spam
Del: non-english -- https://master.php.net/note/delete/102899/non-english
Del: in docs -- https://master.php.net/note/delete/102899/in+docs
Del: other reasons-- https://master.php.net/note/delete/102899
Reject -- https://master.php.net/note/reject/102899
Search -- https://master.php.net/manage/user-notes.php