Changeset 1291 in ExiteCMS for trunk/includes/core_functions.php
- Timestamp:
- 02/19/08 22:26:25 (4 years ago)
- File:
-
- 1 edited
-
trunk/includes/core_functions.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/includes/core_functions.php
r1269 r1291 746 746 } 747 747 748 // Translate bytes into kb, mb, gb or tb by CrappoMan 748 749 // translate a timestamp into a date relative to now 750 function datediff($datefrom,$dateto=-1) 751 { 752 global $locale; 753 754 // validate parameters 755 if ($datefrom == 0) { return ""; } 756 if ($dateto == -1) { $dateto = time(); } 757 758 // calculate the difference in seconds betweeen the two timestamps 759 $difference = $dateto - $datefrom; 760 761 // determine the interface 762 if ($difference < 60) { 763 // if difference is less than 60 seconds, seconds is a good interval of choice 764 $interval = "h"; 765 } elseif ($difference >= 60 && $difference<60*60) { 766 // if difference is between 60 seconds and 60 minutes, minutes is a good interval 767 $interval = "h"; 768 } elseif ($difference >= 60*60 && $difference<60*60*24) { 769 // if difference is between 1 hour and 24 hours, hours is a good interval 770 $interval = "h"; 771 } elseif ($difference >= 60*60*24 && $difference<60*60*24*7) { 772 // if difference is between 1 day and 7 days, days is a good interval 773 $interval = "d"; 774 } elseif ($difference >= 60*60*24*7 && $difference < 60*60*24*30) { 775 // if difference is between 1 week and 30 days, weeks is a good interval 776 $interval = "w"; 777 } elseif ($difference >= 60*60*24*30 && $difference < 60*60*24*365) { 778 // if difference is between 30 days and 365 days, months is a good interval, again, the same thing 779 // applies, if the 29th February happens to exist between your 2 dates, the function will return 780 // the 'incorrect' value for a day 781 $interval = "m"; 782 } elseif ($difference >= 60*60*24*365) { 783 // if difference is greater than or equal to 365 days, return year. This will be incorrect if 784 // for example, you call the function on the 28th April 2008 passing in 29th April 2007. It will 785 // return 1 year ago when in actual fact (yawn!) not quite a year has gone by 786 $interval = "y"; 787 } 788 789 // based on the interval, determine the number of units between the two dates 790 // from this point on, you would be hard pushed telling the difference between 791 // this function and DateDiff. If the $datediff returned is 1, be sure to return 792 // the singular of the unit, e.g. 'day' rather 'days' 793 $res = ""; 794 switch($interval) { 795 case "w": 796 $datediff = floor($difference / 60 / 60 / 24 / 7); 797 $res = $datediff . " " . (($datediff==1) ? $locale['072'] : $locale['073']); 798 break; 799 800 case "y": 801 $datediff = floor($difference / 60 / 60 / 24 / 365); 802 $res = $datediff . " " . (($datediff==1) ? $locale['078'] : $locale['079']); 803 break; 804 805 case "m": 806 $months_difference = floor($difference / 60 / 60 / 24 / 29); 807 while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($months_difference), date("j", $dateto), date("Y", $datefrom)) < $dateto) { 808 $months_difference++; 809 } 810 $datediff = $months_difference; 811 // we need this in here because it is possible to have an 'm' interval and a months 812 // difference of 12 because we are using 29 days in a month 813 if ($datediff==12) { 814 $datediff--; 815 } 816 $res .= $datediff . " " . (($datediff==1) ? $locale['076'] : $locale['077']); 817 break; 818 819 case "d": 820 $datediff = floor($difference / 60 / 60 / 24); 821 $res .= $datediff . " " . (($datediff==1) ? $locale['074'] : $locale['075']); 822 break; 823 824 case "h": 825 $datediff = floor($difference / 60 / 60); 826 $res .= sprintf("%02d:", $datediff); 827 828 case "n": 829 $datediff = floor($difference / 60); 830 $res .= sprintf("%02d:", $datediff); 831 832 case "s": 833 $datediff = $difference; 834 $res .= sprintf("%02d", $datediff); 835 break; 836 } 837 return $res; 838 } 839 840 // translate bytes into kb, mb, gb or tb by CrappoMan 749 841 function parsebytesize($size,$digits=2,$dir=false) { 750 842 $kb=1024; $mb=1024*$kb; $gb=1024*$mb; $tb=1024*$gb;
Note: See TracChangeset
for help on using the changeset viewer.
