- $ Date1 = date ('Y-m-D ');
- $ Date2 = "2015-12-04 ";
- $ Diff = abs (strtotime ($ date2)-strtotime ($ date1 ));
- $ Years = floor ($ diff/(365*60*60*24 ));
- $ Months = floor ($ diff-$ years * 365*60*60*24)/(30*60*60*24 ));
- $ Days = floor ($ diff-$ years * 365*60*60*24-$ months * 30*60*60*24) /(60*60*24 ));
- Printf ("% d years, % d months, % d days \ n", $ years, $ months, $ days );
- -------------------------------------------------------- OR
- $ Date1 = new DateTime ("2007-03-24 ");
- $ Date2 = new DateTime ("2009-06-26 ");
- $ Interval = $ date1-> diff ($ date2 );
- Echo "difference". $ interval-> y. "years,". $ interval-> m. "months,". $ interval-> d. "days ";
- // Shows the total amount of days (not pided into years, months and days like above)
- Echo "difference". $ interval-> days. "days ";
- -------------------------------------------------------- OR
-
-
- /**
- * Calculate differences between two dates with precise semantics. Based on PHPs DateTime: diff ()
- * Implementation by Derick Rethans. Ported to PHP by e il H, 2011-05-02. No rights reserved.
- *
- * See here for original code:
- * Http://svn.php.net/viewvc/php/php-src/trunk/ext/date/lib/tm2unixtime.c? Revision = 302890 & view = markup
- * Http://svn.php.net/viewvc/php/php-src/trunk/ext/date/lib/interval.c? Revision = 298973 & view = markup
- */
- Function _ date_range_limit ($ start, $ end, $ adj, $ a, $ B, $ result)
- {
- If ($ result [$ a] <$ start ){
- $ Result [$ B]-= intval ($ start-$ result [$ a]-1)/$ adj) + 1;
- $ Result [$ a] + = $ adj * intval ($ start-$ result [$ a]-1)/$ adj + 1 );
- }
- If ($ result [$ a] >=$ end ){
- $ Result [$ B] + = intval ($ result [$ a]/$ adj );
- $ Result [$ a]-= $ adj * intval ($ result [$ a]/$ adj );
- }
- Return $ result;
- }
- Function _ date_range_limit_days ($ base, $ result)
- {
- $ Days_in_month_leap = array (31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31 );
- $ Days_in_month = array (31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31 );
- _ Date_range_limit (1, 13, 12, "m", "y", & $ base );
- $ Year = $ base ["y"];
- $ Month = $ base ["m"];
- If (! $ Result ["invert"]) {
- While ($ result ["d"] <0 ){
- $ Month --;
- If ($ month <1 ){
- $ Month + = 12;
- $ Year --;
- }
- $ Leapyear = $ year % 400 = 0 | ($ year % 100! = 0 & $ year % 4 = 0 );
- $ Days = $ leapyear? $ Days_in_month_leap [$ month]: $ days_in_month [$ month];
- $ Result ["d"] + = $ days;
- $ Result ["m"] --;
- }
- } Else {
- While ($ result ["d"] <0 ){
- $ Leapyear = $ year % 400 = 0 | ($ year % 100! = 0 & $ year % 4 = 0 );
- $ Days = $ leapyear? $ Days_in_month_leap [$ month]: $ days_in_month [$ month];
- $ Result ["d"] + = $ days;
- $ Result ["m"] --;
- $ Month ++;
- If ($ month> 12 ){
- $ Month-= 12;
- $ Year ++;
- }
- }
- }
- Return $ result;
- }
- Function _ date_normalize ($ base, $ result)
- {
- $ Result = _ date_range_limit (0, 60, 60, "s", "I", $ result );
- $ Result = _ date_range_limit (0, 60, 60, "I", "h", $ result );
- $ Result = _ date_range_limit (0, 24, 24, "h", "d", $ result );
- $ Result = _ date_range_limit (0, 12, 12, "m", "y", $ result );
- $ Result = _ date_range_limit_days (& $ base, & $ result );
- $ Result = _ date_range_limit (0, 12, 12, "m", "y", $ result );
- Return $ result;
- }
- /**
- * Accepts two unix timestamps.
- */
- Function _ date_diff ($ one, $ two)
- {
- $ Invert = false;
- If ($ one> $ two ){
- List ($ one, $ two) = array ($ two, $ one );
- $ Invert = true;
- }
- $ Key = array ("y", "m", "d", "h", "I", "s ");
- $ A = array_combine ($ key, array_map ("intval", explode ("", date ("Y m d H I s", $ one ))));
- $ B = array_combine ($ key, array_map ("intval", explode ("", date ("Y m d H I s", $ two ))));
- $ Result = array ();
- $ Result ["y"] = $ B ["y"]-$ a ["y"];
- $ Result ["m"] = $ B ["m"]-$ a ["m"];
- $ Result ["d"] = $ B ["d"]-$ a ["d"];
- $ Result ["h"] = $ B ["h"]-$ a ["h"];
- $ Result ["I"] = $ B ["I"]-$ a ["I"];
- $ Result ["s"] = $ B ["s"]-$ a ["s"];
- $ Result ["invert"] = $ invert? 1: 0;
- $ Result ["days"] = intval (abs ($ one-$ two)/86400 ));
- If ($ invert ){
- _ Date_normalize (& $ a, & $ result );
- } Else {
- _ Date_normalize (& $ B, & $ result );
- }
- Return $ result;
- }
- $ Date = "19:37:22 ";
- Echo'
'; - print_r( _date_diff( strtotime($date), time() ) );
- echo '
';
- ?>
|