×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: userc865b
Added: Aug 7, 2017 10:00 AM
Views: 6
Tags: no tags
  1. <?
  2. //TOTAL!! Differnce in Seconds between two dates!
  3. // http://thisinterestsme.com/calculating-difference-dates-php/
  4. $date1 = "2013-03-01 19:12:45";
  5. $date2 = "2014-03-01 06:37:04";
  6.  
  7. //Convert them to timestamps.
  8. $date1Timestamp = strtotime($date1);
  9. $date2Timestamp = strtotime($date2);
  10.  
  11. //Calculate the difference.
  12. $difference = $date2Timestamp - $date1Timestamp;
  13.  
  14. echo $difference;
  15.  
  16. // Difference between two days: hier kann bekommt man nicht die Differnz insgesamt, sondern bezogen auf den Monat Minute Sekunde des Vergleichsdatums
  17. // ObjektOrientiert
  18. $then = '2005-09-01 09:02:23';
  19. $then = new DateTime($then);
  20.  
  21. $now = new DateTime();
  22.  
  23. $sinceThen = $then->diff($now);
  24.  
  25. //Combined
  26. echo $sinceThen->y.' years have passed.<br>';
  27. echo $sinceThen->m.' months have passed.<br>';
  28. echo $sinceThen->d.' days have passed.<br>';
  29. echo $sinceThen->h.' hours have passed.<br>';
  30. echo $sinceThen->i.' minutes have passed.<br>';
  31.  
  32. // Oder mit formatAngaben
  33. $date1 = new DateTime("2011-07-06");
  34. $date2 = new DateTime();
  35.  
  36. $diff = $date2->diff($date1)->format("%a");
  37.  
  38. echo $diff;
  39. /*
  40. http://php.net/manual/en/dateinterval.format.php
  41. format character        Description     Example values
  42. %       Literal %       %
  43. Y       Years, numeric, at least 2 digits with leading 0        01, 03
  44. y       Years, numeric  1, 3
  45. M       Months, numeric, at least 2 digits with leading 0       01, 03, 12
  46. m       Months, numeric 1, 3, 12
  47. D       Days, numeric, at least 2 digits with leading 0 01, 03, 31
  48. d       Days, numeric   1, 3, 31
  49. a       Total number of days as a result of a DateTime::diff() or (unknown) otherwise   4, 18, 8123
  50. H       Hours, numeric, at least 2 digits with leading 0        01, 03, 23
  51. h       Hours, numeric  1, 3, 23
  52. I       Minutes, numeric, at least 2 digits with leading 0      01, 03, 59
  53. i       Minutes, numeric        1, 3, 59
  54. S       Seconds, numeric, at least 2 digits with leading 0      01, 03, 57
  55. s       Seconds, numeric        1, 3, 57
  56. F       Microseconds, numeric, at least 6 digits with leading 0 007701, 052738, 428291
  57. f       Microseconds, numeric   7701, 52738, 428291
  58. R       Sign "-" when negative, "+" when positive       -, +
  59. r       Sign "-" when negative, empty when positive     -,
  60. */