login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Datum en Tijd > Datum verschil berekenen

Datum verschil berekenen

Auteur: I love Jack Daniels.com - 05 oktober 2007 - 09:44 - Gekeurd door: marten - Hits: 8277 - Aantal punten: 2.75 (2 stemmen)




Bereken het verschil tussen 2 dagen. Je kan dit verschil terug krijgen in een aantal vormen namelijk:

yyyy - Number of full years
q - Number of full quarters
m - Number of full months
y - Difference between day numbers
(eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".)
d - Number of full days
w - Number of full weekdays
ww - Number of full weeks
h - Number of full hours
n - Number of full minutes
s - Number of full seconds (default)

Het script is niet van mij en ik kan niet herleiden van wie het wel is maar hij is zeker handig. Je kan trouwens ook timestamps gebruiken. Laatste parameter op true zetten dan zou het moeten lukken. Anders kan je gewoon dd-mm-yyyy gebruiken voor de functie.

Code:
  1. <?php
  2. function datediff($interval, $datefrom, $dateto, $using_timestamps = false) {
  3. /*
  4. $interval can be:
  5. yyyy - Number of full years
  6. q - Number of full quarters
  7. m - Number of full months
  8. y - Difference between day numbers
  9. (eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".)
  10. d - Number of full days
  11. w - Number of full weekdays
  12. ww - Number of full weeks
  13. h - Number of full hours
  14. n - Number of full minutes
  15. s - Number of full seconds (default)
  16. */
  17.  
  18. if (!$using_timestamps) {
  19. $datefrom = strtotime($datefrom, 0);
  20. $dateto = strtotime($dateto, 0);
  21. }
  22. $difference = $dateto - $datefrom; // Difference in seconds
  23.  
  24. switch($interval) {
  25.  
  26. case 'yyyy': // Number of full years
  27.  
  28. $years_difference = floor($difference / 31536000);
  29. if (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom), date("j", $datefrom), date("Y", $datefrom)+$years_difference) > $dateto) {
  30. $years_difference--;
  31. }
  32. if (mktime(date("H", $dateto), date("i", $dateto), date("s", $dateto), date("n", $dateto), date("j", $dateto), date("Y", $dateto)-($years_difference+1)) > $datefrom) {
  33. $years_difference++;
  34. }
  35. $datediff = $years_difference;
  36. break;
  37.  
  38. case "q": // Number of full quarters
  39.  
  40. $quarters_difference = floor($difference / 8035200);
  41. while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($quarters_difference*3), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
  42. $months_difference++;
  43. }
  44. $quarters_difference--;
  45. $datediff = $quarters_difference;
  46. break;
  47.  
  48. case "m": // Number of full months
  49.  
  50. $months_difference = floor($difference / 2678400);
  51. while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($months_difference), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
  52. $months_difference++;
  53. }
  54. $months_difference--;
  55. $datediff = $months_difference;
  56. break;
  57.  
  58. case 'y': // Difference between day numbers
  59.  
  60. $datediff = date("z", $dateto) - date("z", $datefrom);
  61. break;
  62.  
  63. case "d": // Number of full days
  64.  
  65. $datediff = floor($difference / 86400);
  66. break;
  67.  
  68. case "w": // Number of full weekdays
  69.  
  70. $days_difference = floor($difference / 86400);
  71. $weeks_difference = floor($days_difference / 7); // Complete weeks
  72. $first_day = date("w", $datefrom);
  73. $days_remainder = floor($days_difference % 7);
  74. $odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder?
  75. if ($odd_days > 7) { // Sunday
  76. $days_remainder--;
  77. }
  78. if ($odd_days > 6) { // Saturday
  79. $days_remainder--;
  80. }
  81. $datediff = ($weeks_difference * 5) + $days_remainder;
  82. break;
  83.  
  84. case "ww": // Number of full weeks
  85.  
  86. $datediff = floor($difference / 604800);
  87. break;
  88.  
  89. case "h": // Number of full hours
  90.  
  91. $datediff = floor($difference / 3600);
  92. break;
  93.  
  94. case "n": // Number of full minutes
  95.  
  96. $datediff = floor($difference / 60);
  97. break;
  98.  
  99. default: // Number of full seconds (default)
  100.  
  101. $datediff = $difference;
  102. break;
  103. }
  104.  
  105. return $datediff;
  106. }
  107. ?>
Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (3)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.066s