note 107434 modified in datetime.diff by danbrown

From: Date: Thu, 10 May 2012 21:15:42 +0000
Subject: note 107434 modified in datetime.diff by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-187862@lists.php.net to get a copy of this message
Though I found a number of people who ran into the issue of 5.2 and lower not supporting this function, I was unable to find any solid examples to get around it. Therefore I hope this can help some others:&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;function get_timespan_string($older, $newer) {&#13;&#10; $Y1 = $older-&#62;format(&#39;Y&#39;);&#13;&#10; $Y2 = $newer-&#62;format(&#39;Y&#39;);&#13;&#10; $Y = $Y2 - $Y1;&#13;&#10;&#13;&#10; $m1 = $older-&#62;format(&#39;m&#39;);&#13;&#10; $m2 = $newer-&#62;format(&#39;m&#39;);&#13;&#10; $m = $m2 - $m1;&#13;&#10;&#13;&#10; $d1 = $older-&#62;format(&#39;d&#39;);&#13;&#10; $d2 = $newer-&#62;format(&#39;d&#39;);&#13;&#10; $d = $d2 - $d1;&#13;&#10;&#13;&#10; $H1 = $older-&#62;format(&#39;H&#39;);&#13;&#10; $H2 = $newer-&#62;format(&#39;H&#39;);&#13;&#10; $H = $H2 - $H1;&#13;&#10;&#13;&#10; $i1 = $older-&#62;format(&#39;i&#39;);&#13;&#10; $i2 = $newer-&#62;format(&#39;i&#39;);&#13;&#10; $i = $i2 - $i1;&#13;&#10;&#13;&#10; $s1 = $older-&#62;format(&#39;s&#39;);&#13;&#10; $s2 = $newer-&#62;format(&#39;s&#39;);&#13;&#10; $s = $s2 - $s1;&#13;&#10;&#13;&#10; if($s &#60; 0) {&#13;&#10; $i = $i -1;&#13;&#10; $s = $s + 60;&#13;&#10; }&#13;&#10; if($i &#60; 0) {&#13;&#10; $H = $H - 1;&#13;&#10; $i = $i + 60;&#13;&#10; }&#13;&#10; if($H &#60; 0) {&#13;&#10; $d = $d - 1;&#13;&#10; $H = $H + 24;&#13;&#10; }&#13;&#10; if($d &#60; 0) {&#13;&#10; $m = $m - 1;&#13;&#10; $d = $d + get_days_for_previous_month($m2, $Y2);&#13;&#10; }&#13;&#10; if($m &#60; 0) {&#13;&#10; $Y = $Y - 1;&#13;&#10; $m = $m + 12;&#13;&#10; }&#13;&#10; $timespan_string = create_timespan_string($Y, $m, $d, $H, $i, $s);&#13;&#10; return $timespan_string;&#13;&#10;}&#13;&#10;&#13;&#10;function get_days_for_previous_month($current_month, $current_year) {&#13;&#10; $previous_month = $current_month - 1;&#13;&#10; if($current_month == 1) {&#13;&#10; $current_year = $current_year - 1; //going from January to previous December&#13;&#10; $previous_month = 12;&#13;&#10; }&#13;&#10; if($previous_month == 11 || $previous_month == 9 || $previous_month == 6 || $previous_month == 4) {&#13;&#10; return 30;&#13;&#10; }&#13;&#10; else if($previous_month == 2) {&#13;&#10; if(($current_year % 4) == 0) { //remainder 0 for leap years&#13;&#10; return 29;&#13;&#10; }&#13;&#10; else {&#13;&#10; return 28;&#13;&#10; }&#13;&#10; }&#13;&#10; else {&#13;&#10; return 31;&#13;&#10; }&#13;&#10;}&#13;&#10;&#13;&#10;function create_timespan_string($Y, $m, $d, $H, $i, $s)&#13;&#10;{&#13;&#10; $timespan_string = &#39;&#39;;&#13;&#10; $found_first_diff = false;&#13;&#10; if($Y &#62;= 1) {&#13;&#10; $found_first_diff = true;&#13;&#10; $timespan_string .= pluralize($Y, &#39;year&#39;).&#39; &#39;;&#13;&#10; }&#13;&#10; if($m &#62;= 1 || $found_first_diff) {&#13;&#10; $found_first_diff = true;&#13;&#10; $timespan_string .= pluralize($m, &#39;month&#39;).&#39; &#39;;&#13;&#10; }&#13;&#10; if($d &#62;= 1 || $found_first_diff) {&#13;&#10; $found_first_diff = true;&#13;&#10; $timespan_string .= pluralize($d, &#39;day&#39;).&#39; &#39;;&#13;&#10; }&#13;&#10; if($H &#62;= 1 || $found_first_diff) {&#13;&#10; $found_first_diff = true;&#13;&#10; $timespan_string .= pluralize($H, &#39;hour&#39;).&#39; &#39;;&#13;&#10; }&#13;&#10; if($i &#62;= 1 || $found_first_diff) {&#13;&#10; $found_first_diff = true;&#13;&#10; $timespan_string .= pluralize($i, &#39;minute&#39;).&#39; &#39;;&#13;&#10; }&#13;&#10; if($found_first_diff) {&#13;&#10; $timespan_string .= &#39;and &#39;;&#13;&#10; }&#13;&#10; $timespan_string .= pluralize($s, &#39;second&#39;);&#13;&#10; return $timespan_string;&#13;&#10;}&#13;&#10;&#13;&#10;function pluralize( $count, $text )&#13;&#10;{&#13;&#10; return $count . ( ( $count == 1 ) ? ( &#34; $text&#34; ) : ( &#34; ${text}s&#34; ) );&#13;&#10;}&#13;&#10;?&#62; --was-- Though I found a number of people who ran into the issue of 5.2 and lower not supporting this function, I was unable to find any solid examples to get around it. Therefore I hope this can help some others: function get_timespan_string($older, $newer) { $Y1 = $older->format('Y'); $Y2 = $newer->format('Y'); $Y = $Y2 - $Y1; $m1 = $older->format('m'); $m2 = $newer->format('m'); $m = $m2 - $m1; $d1 = $older->format('d'); $d2 = $newer->format('d'); $d = $d2 - $d1; $H1 = $older->format('H'); $H2 = $newer->format('H'); $H = $H2 - $H1; $i1 = $older->format('i'); $i2 = $newer->format('i'); $i = $i2 - $i1; $s1 = $older->format('s'); $s2 = $newer->format('s'); $s = $s2 - $s1; if($s < 0) { $i = $i -1; $s = $s + 60; } if($i < 0) { $H = $H - 1; $i = $i + 60; } if($H < 0) { $d = $d - 1; $H = $H + 24; } if($d < 0) { $m = $m - 1; $d = $d + get_days_for_previous_month($m2, $Y2); } if($m < 0) { $Y = $Y - 1; $m = $m + 12; } $timespan_string = create_timespan_string($Y, $m, $d, $H, $i, $s); return $timespan_string; } function get_days_for_previous_month($current_month, $current_year) { $previous_month = $current_month - 1; if($current_month == 1) { $current_year = $current_year - 1; //going from January to previous December $previous_month = 12; } if($previous_month == 11 || $previous_month == 9 || $previous_month == 6 || $previous_month == 4) { return 30; } else if($previous_month == 2) { if(($current_year % 4) == 0) { //remainder 0 for leap years return 29; } else { return 28; } } else { return 31; } } function create_timespan_string($Y, $m, $d, $H, $i, $s) { $timespan_string = ''; $found_first_diff = false; if($Y >= 1) { $found_first_diff = true; $timespan_string .= pluralize($Y, 'year').' '; } if($m >= 1 || $found_first_diff) { $found_first_diff = true; $timespan_string .= pluralize($m, 'month').' '; } if($d >= 1 || $found_first_diff) { $found_first_diff = true; $timespan_string .= pluralize($d, 'day').' '; } if($H >= 1 || $found_first_diff) { $found_first_diff = true; $timespan_string .= pluralize($H, 'hour').' '; } if($i >= 1 || $found_first_diff) { $found_first_diff = true; $timespan_string .= pluralize($i, 'minute').' '; } if($found_first_diff) { $timespan_string .= 'and '; } $timespan_string .= pluralize($s, 'second'); return $timespan_string; } function pluralize( $count, $text ) { return $count . ( ( $count == 1 ) ? ( " $text" ) : ( " ${text}s" ) ); } http://php.net/manual/en/datetime.diff.php

« previous php.notes (#187862) next »