Skip to content

Instantly share code, notes, and snippets.

@emrahoruc
Last active February 20, 2017 08:10
Show Gist options
  • Save emrahoruc/66bdc150a763d90a41f011bbf51848a0 to your computer and use it in GitHub Desktop.
Save emrahoruc/66bdc150a763d90a41f011bbf51848a0 to your computer and use it in GitHub Desktop.
Converts days to year, month, week and day.
<?php
function formatDays($days_value)
{
$conditions = [
365 => 'Year',
30 => 'Month',
7 => 'Week',
1 => 'Day'
];
$result = null;
foreach ($conditions as $days => $str) {
$value = floor($days_value / $days);
if ($value > 0) {
$days_value = $days_value % $days;
$result .= $value .' '. $str . ($value > 1 ? 's ' : ' ');
}
}
return $result;
}
echo formatDays(30); // Output: 1 Month
echo formatDays(98); // Output: 3 Months 1 Week 1 Day
echo formatDays(830); // Output: 2 Years 3 Months 1 Week 3 Days
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment