Last active
February 20, 2017 08:10
-
-
Save emrahoruc/66bdc150a763d90a41f011bbf51848a0 to your computer and use it in GitHub Desktop.
Converts days to year, month, week and day.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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