Created
June 28, 2016 09:46
-
-
Save andreipetcu/20ded7cc20b99c2119394fb5c9634971 to your computer and use it in GitHub Desktop.
Date/time php helper
This file contains hidden or 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 | |
| /** | |
| * Calculates the difference between two timestamps | |
| * @param integer $d1 first date | |
| * @param integer $d2 second date | |
| * @return string | |
| */ | |
| function dateDiff($d1, $d2) { | |
| if ($d1 < $d2) { | |
| $tmp = $d1; | |
| $d1 = $d2; | |
| $d2 = $tmp; | |
| } | |
| $all = round(($d1 - $d2) / 60); | |
| $d = floor ($all / 1440); | |
| $h = floor (($all - $d * 1440) / 60); | |
| $m = $all - ($d * 1440) - ($h * 60); | |
| return ($d > 0 ? $d . ' zile ' : '') . ($h < 10 ? "0" . $h : $h) . ':' . ($m < 10 ? "0" . $m : $m); | |
| } | |
| /** | |
| * Converts minutes to days, hours and minutes | |
| * @param integer $time number of minutes | |
| * @param string $format sprintf format | |
| * @return string | |
| */ | |
| function min2time($time, $format = '%02d days %02d:%02d') { | |
| if ($time < 1) { | |
| return; | |
| } | |
| $days = 0; | |
| $hours = floor($time / 60); | |
| if ($hours > 59) { | |
| $days = $hours / 24; | |
| $hours = $hours % 24; | |
| } | |
| $minutes = ($time % 60); | |
| return sprintf($format, $days, $hours, $minutes); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment