<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: PHP: Calculate Real Differences Between Two Dates or Timestamps</title>
	<atom:link href="http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/</link>
	<description>If !1 0 &#124; Linux and Development Guides</description>
	<lastBuildDate>Fri, 10 Feb 2012 20:00:37 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<a href="http://www.if-not-true-then-false.com/hp/fitting.php" style="padding:0;margin:0;" rel="nofollow"><img src="http://www.if-not-true-then-false.com/wp-content/plugins/project-honey-pot-spam-trap/images/support.png" height="0" width="0" border="0" style="padding:0;margin:0;" /></a>	<item>
		<title>By: JR</title>
		<link>http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/comment-page-2/#comment-22783</link>
		<dc:creator>JR</dc:creator>
		<pubDate>Wed, 25 Jan 2012 19:00:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.if-not-true-then-false.com/?p=792#comment-22783</guid>
		<description>Hi Ben,

Could you echo your variables $setTime and $currentTime, just before deteDiff function and post output here, like:
&lt;pre lang=&quot;php&quot;&gt;
echo $setTime;
echo $currentTime;
echo dateDiff($setTime, $currentTime, 3);
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Hi Ben,</p>
<p>Could you echo your variables $setTime and $currentTime, just before deteDiff function and post output here, like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="color:#D8D8D8;"><span style="color: #FFF; font-weight:bold;">echo</span> <span style="color: #729fcf;">$setTime</span><span style=" color: #FFF;">;</span>
<span style="color: #FFF; font-weight:bold;">echo</span> <span style="color: #729fcf;">$currentTime</span><span style=" color: #FFF;">;</span>
<span style="color: #FFF; font-weight:bold;">echo</span> dateDiff<span style=" color: #FFF;">&#40;</span><span style="color: #729fcf;">$setTime</span><span style=" color: #FFF;">,</span> <span style="color: #729fcf;">$currentTime</span><span style=" color: #FFF;">,</span> <span style="color: #7FB347;">3</span><span style=" color: #FFF;">&#41;</span><span style=" color: #FFF;">;</span></pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: Ben Dane</title>
		<link>http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/comment-page-2/#comment-22772</link>
		<dc:creator>Ben Dane</dc:creator>
		<pubDate>Wed, 25 Jan 2012 16:57:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.if-not-true-then-false.com/?p=792#comment-22772</guid>
		<description>Forgot to add: it gives me 42 years, xx days, etc. I can&#039;t figure out why.</description>
		<content:encoded><![CDATA[<p>Forgot to add: it gives me 42 years, xx days, etc. I can&#8217;t figure out why.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben Dane</title>
		<link>http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/comment-page-2/#comment-22771</link>
		<dc:creator>Ben Dane</dc:creator>
		<pubDate>Wed, 25 Jan 2012 16:56:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.if-not-true-then-false.com/?p=792#comment-22771</guid>
		<description>Heej JR, your script has helped me loads. Thanks thus far! I have a quick question, a problem I can&#039;t simply solve by myself.

If I do: 
&lt;b&gt;echo dateDiff(1267074000, time(), 3);&lt;/b&gt;
it works fine.

But if I do: 
&lt;b&gt;echo dateDiff($setTime, $currentTime, 3);&lt;/b&gt;
it doesn&#039;t work, even though holding the same results. 
Is there anything I&#039;m doing wrong?

Cheers and much thanks in advance.</description>
		<content:encoded><![CDATA[<p>Heej JR, your script has helped me loads. Thanks thus far! I have a quick question, a problem I can&#8217;t simply solve by myself.</p>
<p>If I do:<br />
<b>echo dateDiff(1267074000, time(), 3);</b><br />
it works fine.</p>
<p>But if I do:<br />
<b>echo dateDiff($setTime, $currentTime, 3);</b><br />
it doesn&#8217;t work, even though holding the same results.<br />
Is there anything I&#8217;m doing wrong?</p>
<p>Cheers and much thanks in advance.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JR</title>
		<link>http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/comment-page-2/#comment-20739</link>
		<dc:creator>JR</dc:creator>
		<pubDate>Wed, 21 Dec 2011 12:55:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.if-not-true-then-false.com/?p=792#comment-20739</guid>
		<description>Hi abang,

Is those Operational days always from Mon to Fri?
And is those hours fixed 08:00 - 22:00?</description>
		<content:encoded><![CDATA[<p>Hi abang,</p>
<p>Is those Operational days always from Mon to Fri?<br />
And is those hours fixed 08:00 &#8211; 22:00?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: abang</title>
		<link>http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/comment-page-2/#comment-20720</link>
		<dc:creator>abang</dc:creator>
		<pubDate>Wed, 21 Dec 2011 03:53:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.if-not-true-then-false.com/?p=792#comment-20720</guid>
		<description>hi all,

I have a question to all, 
How to calculate minutes &quot;Operational Hours&quot; within a period.

case example :
Date range : 2011-11-01 to 2011-11-30, 
Non Operational Hours : between 22:00 to 08:00 week day (Mon to Frid)
Non Operational Days : Sat + Sun

Quest :
How to Calculate Total &quot;Minutes&quot; Operational Time ??

Please help, 
thanks for your advice ...

BP</description>
		<content:encoded><![CDATA[<p>hi all,</p>
<p>I have a question to all,<br />
How to calculate minutes &#8220;Operational Hours&#8221; within a period.</p>
<p>case example :<br />
Date range : 2011-11-01 to 2011-11-30,<br />
Non Operational Hours : between 22:00 to 08:00 week day (Mon to Frid)<br />
Non Operational Days : Sat + Sun</p>
<p>Quest :<br />
How to Calculate Total &#8220;Minutes&#8221; Operational Time ??</p>
<p>Please help,<br />
thanks for your advice &#8230;</p>
<p>BP</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JR</title>
		<link>http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/comment-page-2/#comment-20667</link>
		<dc:creator>JR</dc:creator>
		<pubDate>Mon, 19 Dec 2011 06:56:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.if-not-true-then-false.com/?p=792#comment-20667</guid>
		<description>Actually, I can&#039;t see the problem here, so let&#039;s look at this in more detail...

&lt;strong&gt;First if we compare just fixed value approach, with dateDiff:&lt;/strong&gt;
&lt;pre lang=&quot;php&quot;&gt;
$start =&#039;2011-12-18 23:19:00&#039;;
$end =&#039;2012-02-08 13:20:00&#039;;
$interval=3;

echo (strtotime($end)-strtotime($start))/(60*60) . &quot;\n&quot;;

echo dateDiff( $start , $end , &#039;hour&#039;) . &quot;\n&quot;;
&lt;/pre&gt;

Output:
&lt;pre&gt;
1238.0166666667
1238
&lt;/pre&gt;

So this looks good, exactly same, but dateDiff output without decimals, as it should be.


&lt;strong&gt;Then divide results with three (your interval):&lt;/strong&gt;
1238.0166666667 / 3 = 412.672222222
1238 / 3 = 412.666666667

&lt;strong&gt;Then add one (I don&#039;t know why, but if you just have to add one):&lt;/strong&gt;
412.672222222 + 1 = 413.672222222
412.666666667 + 1 = 413.666666667

&lt;strong&gt;Then you use floor to round first value fractions down:&lt;/strong&gt;
floor(413.672222222) = 413
413.666666667

So actually second value is 413.666666667 at this point, not 414...and first value fractions is rounded down and it&#039;s 413...</description>
		<content:encoded><![CDATA[<p>Actually, I can&#8217;t see the problem here, so let&#8217;s look at this in more detail&#8230;</p>
<p><strong>First if we compare just fixed value approach, with dateDiff:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="color:#D8D8D8;"><span style="color: #729fcf;">$start</span> <span style=" color: #FFF;">=</span><span style="color: #FFC600;">'2011-12-18 23:19:00'</span><span style=" color: #FFF;">;</span>
<span style="color: #729fcf;">$end</span> <span style=" color: #FFF;">=</span><span style="color: #FFC600;">'2012-02-08 13:20:00'</span><span style=" color: #FFF;">;</span>
<span style="color: #729fcf;">$interval</span><span style=" color: #FFF;">=</span><span style="color: #7FB347;">3</span><span style=" color: #FFF;">;</span>
&nbsp;
<span style="color: #FFF; font-weight:bold;">echo</span> <span style=" color: #FFF;">&#40;</span><span style="color: #d3d7cf">strtotime</span><span style=" color: #FFF;">&#40;</span><span style="color: #729fcf;">$end</span><span style=" color: #FFF;">&#41;</span><span style=" color: #FFF;">-</span><span style="color: #d3d7cf">strtotime</span><span style=" color: #FFF;">&#40;</span><span style="color: #729fcf;">$start</span><span style=" color: #FFF;">&#41;</span><span style=" color: #FFF;">&#41;</span><span style=" color: #FFF;">/</span><span style=" color: #FFF;">&#40;</span><span style="color: #7FB347;">60</span><span style=" color: #FFF;">*</span><span style="color: #7FB347;">60</span><span style=" color: #FFF;">&#41;</span> <span style=" color: #FFF;">.</span> <span style="color: #FFC600;">&quot;<span style="color: #ce5c00">\n</span>&quot;</span><span style=" color: #FFF;">;</span>
&nbsp;
<span style="color: #FFF; font-weight:bold;">echo</span> dateDiff<span style=" color: #FFF;">&#40;</span> <span style="color: #729fcf;">$start</span> <span style=" color: #FFF;">,</span> <span style="color: #729fcf;">$end</span> <span style=" color: #FFF;">,</span> <span style="color: #FFC600;">'hour'</span><span style=" color: #FFF;">&#41;</span> <span style=" color: #FFF;">.</span> <span style="color: #FFC600;">&quot;<span style="color: #ce5c00">\n</span>&quot;</span><span style=" color: #FFF;">;</span></pre></div></div>

<p>Output:</p>
<pre>
1238.0166666667
1238
</pre>
<p>So this looks good, exactly same, but dateDiff output without decimals, as it should be.</p>
<p><strong>Then divide results with three (your interval):</strong><br />
1238.0166666667 / 3 = 412.672222222<br />
1238 / 3 = 412.666666667</p>
<p><strong>Then add one (I don&#8217;t know why, but if you just have to add one):</strong><br />
412.672222222 + 1 = 413.672222222<br />
412.666666667 + 1 = 413.666666667</p>
<p><strong>Then you use floor to round first value fractions down:</strong><br />
floor(413.672222222) = 413<br />
413.666666667</p>
<p>So actually second value is 413.666666667 at this point, not 414&#8230;and first value fractions is rounded down and it&#8217;s 413&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sevag</title>
		<link>http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/comment-page-2/#comment-20666</link>
		<dc:creator>sevag</dc:creator>
		<pubDate>Mon, 19 Dec 2011 05:04:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.if-not-true-then-false.com/?p=792#comment-20666</guid>
		<description>First of all thank you for your code, it helped me alot. But i just realized something that got me puzzled.

Suppose I have the following two time-stamps:

$start =&#039;2011-12-18 23:19:00&#039;;	
$end   =&#039;2012-02-08 13:20:00&#039;;
$interval=3;

I tried to compute the difference in hours using two different approaches and I got a small difference.

1.
$howmany=floor($difference/($interval*60*60)+1);

output was 413

 
2.$howmany=(dateDiff( $start ,  $end , &#039;hour&#039;)/$interval)+1;

out was 414


I tried the same two approaches for minutes, days, and weeks and answers were IDENTICAL. but just never figured out why in this specific case answers weren&#039;t the same.  Any suggestions?

Thank you,
Sevag</description>
		<content:encoded><![CDATA[<p>First of all thank you for your code, it helped me alot. But i just realized something that got me puzzled.</p>
<p>Suppose I have the following two time-stamps:</p>
<p>$start =&#8217;2011-12-18 23:19:00&#8242;;<br />
$end   =&#8217;2012-02-08 13:20:00&#8242;;<br />
$interval=3;</p>
<p>I tried to compute the difference in hours using two different approaches and I got a small difference.</p>
<p>1.<br />
$howmany=floor($difference/($interval*60*60)+1);</p>
<p>output was 413</p>
<p>2.$howmany=(dateDiff( $start ,  $end , &#8216;hour&#8217;)/$interval)+1;</p>
<p>out was 414</p>
<p>I tried the same two approaches for minutes, days, and weeks and answers were IDENTICAL. but just never figured out why in this specific case answers weren&#8217;t the same.  Any suggestions?</p>
<p>Thank you,<br />
Sevag</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JR</title>
		<link>http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/comment-page-2/#comment-20662</link>
		<dc:creator>JR</dc:creator>
		<pubDate>Sun, 18 Dec 2011 15:52:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.if-not-true-then-false.com/?p=792#comment-20662</guid>
		<description>Hi Rilwis nice to see you here,

Yes, this is old post and &lt;em&gt;PHP&#039;s own Datetime:diff&lt;/em&gt; is useful on PHP 5.3, but you still need to format output manually. And of course here is many custom modifications from original version... ;)</description>
		<content:encoded><![CDATA[<p>Hi Rilwis nice to see you here,</p>
<p>Yes, this is old post and <em>PHP&#8217;s own Datetime:diff</em> is useful on PHP 5.3, but you still need to format output manually. And of course here is many custom modifications from original version&#8230; ;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rilwis</title>
		<link>http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/comment-page-2/#comment-20660</link>
		<dc:creator>Rilwis</dc:creator>
		<pubDate>Sun, 18 Dec 2011 13:21:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.if-not-true-then-false.com/?p=792#comment-20660</guid>
		<description>Very nice function. This is useful when the PHP version &lt; 5.3. Since version 5.3, PHP supports method Datetime::diff( $datetime1, $datetime2 ); that might be an easier solution.</description>
		<content:encoded><![CDATA[<p>Very nice function. This is useful when the PHP version &lt; 5.3. Since version 5.3, PHP supports method Datetime::diff( $datetime1, $datetime2 ); that might be an easier solution.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JR</title>
		<link>http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/comment-page-2/#comment-20652</link>
		<dc:creator>JR</dc:creator>
		<pubDate>Sun, 18 Dec 2011 00:21:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.if-not-true-then-false.com/?p=792#comment-20652</guid>
		<description>Okey :) Then check following code:
&lt;pre lang=&quot;php&quot;&gt;
&lt;?php
 
  // Set timezone
  date_default_timezone_set(&quot;UTC&quot;);
 
  // Time format is UNIX timestamp or
  // PHP strtotime compatible strings
  function dateDiff($time1, $time2, $interval) {
    // If not numeric then convert texts to unix timestamps
    if (!is_int($time1)) {
      $time1 = strtotime($time1);
    }
    if (!is_int($time2)) {
      $time2 = strtotime($time2);
    }
 
    // If time1 is bigger than time2
    // Then swap time1 and time2
    if ($time1 &gt; $time2) {
      $ttime = $time1;
      $time1 = $time2;
      $time2 = $ttime;
    }
 
    // Set up intervals and diffs arrays
    $intervals = array(&#039;year&#039;,&#039;month&#039;,&#039;day&#039;,&#039;hour&#039;,&#039;minute&#039;,&#039;second&#039;);
    if (!in_array($interval, $intervals)) {
      return false;
    }
      
    $diff = 0;
    // Create temp time from time1 and interval
    $ttime = strtotime(&quot;+1 &quot; . $interval, $time1);
    // Loop until temp time is smaller than time2
    while ($time2 &gt;= $ttime) {
      $time1 = $ttime;
      $diff++;
      // Create new temp time from time1 and interval
      $ttime = strtotime(&quot;+1 &quot; . $interval, $time1);
    }
 
    return $diff;
  }

  // Testing...
  echo &quot;Timestamps 2011-12-16 10:00 - 2011-12-17 10:25\n&quot;;
  echo &quot;Minute interval: &quot; . dateDiff(&#039;2011-12-16 10:00&#039;, &#039;2011-12-17 10:25&#039;, &#039;minute&#039;) . &quot;\n&quot;;
  echo &quot;Hour interval: &quot; . dateDiff(&#039;2011-12-16 10:00&#039;, &#039;2011-12-17 10:25&#039;, &#039;hour&#039;) . &quot;\n&quot;;
  echo &quot;Day interval: &quot; . dateDiff(&#039;2011-12-16 10:00&#039;, &#039;2011-12-17 10:25&#039;, &#039;day&#039;) . &quot;\n&quot;;

  echo &quot;\n\n&quot;;

  echo &quot;Timestamps 2011-12-16 10:00 - 2014-06-17 10:25\n&quot;;
  echo &quot;Hour interval: &quot; . dateDiff(&#039;2011-12-16 10:00&#039;, &#039;2014-06-17 10:25&#039;, &#039;hour&#039;) . &quot;\n&quot;;
  echo &quot;Day interval: &quot; . dateDiff(&#039;2011-12-16 10:00&#039;, &#039;2014-06-17 10:25&#039;, &#039;day&#039;) . &quot;\n&quot;;
  echo &quot;Month interval: &quot; . dateDiff(&#039;2011-12-16 10:00&#039;, &#039;2014-06-17 10:25&#039;, &#039;month&#039;) . &quot;\n&quot;;
  echo &quot;Year interval: &quot; . dateDiff(&#039;2011-12-16 10:00&#039;, &#039;2014-06-17 10:25&#039;, &#039;year&#039;) . &quot;\n&quot;;

?&gt;
&lt;/pre&gt;

Output:
&lt;pre&gt;
Timestamps 2011-12-16 10:00 - 2011-12-17 10:25
Minute interval: 1465
Hour interval: 24
Day interval: 1


Timestamps 2011-12-16 10:00 - 2014-06-17 10:25
Hour interval: 21936
Day interval: 914
Month interval: 30
Year interval: 2
&lt;/pre&gt;

Note. first examples use your timestamps and last examples use date 2014-06-17... :)

I just tested this quickly and seems to be working, please let me know, if you have any problems with that?</description>
		<content:encoded><![CDATA[<p>Okey :) Then check following code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="color:#D8D8D8;"><span style="color: #FFF; font-weight:bold;">&lt;?php</span>
&nbsp;
  <span style=" color: #C7DD0C">// Set timezone</span>
  <span style="color: #d3d7cf">date_default_timezone_set</span><span style=" color: #FFF;">&#40;</span><span style="color: #FFC600;">&quot;UTC&quot;</span><span style=" color: #FFF;">&#41;</span><span style=" color: #FFF;">;</span>
&nbsp;
  <span style=" color: #C7DD0C">// Time format is UNIX timestamp or</span>
  <span style=" color: #C7DD0C">// PHP strtotime compatible strings</span>
  <span style="color: #FFF; font-weight:bold;">function</span> dateDiff<span style=" color: #FFF;">&#40;</span><span style="color: #729fcf;">$time1</span><span style=" color: #FFF;">,</span> <span style="color: #729fcf;">$time2</span><span style=" color: #FFF;">,</span> <span style="color: #729fcf;">$interval</span><span style=" color: #FFF;">&#41;</span> <span style=" color: #FFF;">&#123;</span>
    <span style=" color: #C7DD0C">// If not numeric then convert texts to unix timestamps</span>
    <span style="color: #FFF; font-weight:bold;">if</span> <span style=" color: #FFF;">&#40;</span><span style=" color: #FFF;">!</span><span style="color: #d3d7cf">is_int</span><span style=" color: #FFF;">&#40;</span><span style="color: #729fcf;">$time1</span><span style=" color: #FFF;">&#41;</span><span style=" color: #FFF;">&#41;</span> <span style=" color: #FFF;">&#123;</span>
      <span style="color: #729fcf;">$time1</span> <span style=" color: #FFF;">=</span> <span style="color: #d3d7cf">strtotime</span><span style=" color: #FFF;">&#40;</span><span style="color: #729fcf;">$time1</span><span style=" color: #FFF;">&#41;</span><span style=" color: #FFF;">;</span>
    <span style=" color: #FFF;">&#125;</span>
    <span style="color: #FFF; font-weight:bold;">if</span> <span style=" color: #FFF;">&#40;</span><span style=" color: #FFF;">!</span><span style="color: #d3d7cf">is_int</span><span style=" color: #FFF;">&#40;</span><span style="color: #729fcf;">$time2</span><span style=" color: #FFF;">&#41;</span><span style=" color: #FFF;">&#41;</span> <span style=" color: #FFF;">&#123;</span>
      <span style="color: #729fcf;">$time2</span> <span style=" color: #FFF;">=</span> <span style="color: #d3d7cf">strtotime</span><span style=" color: #FFF;">&#40;</span><span style="color: #729fcf;">$time2</span><span style=" color: #FFF;">&#41;</span><span style=" color: #FFF;">;</span>
    <span style=" color: #FFF;">&#125;</span>
&nbsp;
    <span style=" color: #C7DD0C">// If time1 is bigger than time2</span>
    <span style=" color: #C7DD0C">// Then swap time1 and time2</span>
    <span style="color: #FFF; font-weight:bold;">if</span> <span style=" color: #FFF;">&#40;</span><span style="color: #729fcf;">$time1</span> <span style=" color: #FFF;">&gt;</span> <span style="color: #729fcf;">$time2</span><span style=" color: #FFF;">&#41;</span> <span style=" color: #FFF;">&#123;</span>
      <span style="color: #729fcf;">$ttime</span> <span style=" color: #FFF;">=</span> <span style="color: #729fcf;">$time1</span><span style=" color: #FFF;">;</span>
      <span style="color: #729fcf;">$time1</span> <span style=" color: #FFF;">=</span> <span style="color: #729fcf;">$time2</span><span style=" color: #FFF;">;</span>
      <span style="color: #729fcf;">$time2</span> <span style=" color: #FFF;">=</span> <span style="color: #729fcf;">$ttime</span><span style=" color: #FFF;">;</span>
    <span style=" color: #FFF;">&#125;</span>
&nbsp;
    <span style=" color: #C7DD0C">// Set up intervals and diffs arrays</span>
    <span style="color: #729fcf;">$intervals</span> <span style=" color: #FFF;">=</span> <span style="color: #d3d7cf">array</span><span style=" color: #FFF;">&#40;</span><span style="color: #FFC600;">'year'</span><span style=" color: #FFF;">,</span><span style="color: #FFC600;">'month'</span><span style=" color: #FFF;">,</span><span style="color: #FFC600;">'day'</span><span style=" color: #FFF;">,</span><span style="color: #FFC600;">'hour'</span><span style=" color: #FFF;">,</span><span style="color: #FFC600;">'minute'</span><span style=" color: #FFF;">,</span><span style="color: #FFC600;">'second'</span><span style=" color: #FFF;">&#41;</span><span style=" color: #FFF;">;</span>
    <span style="color: #FFF; font-weight:bold;">if</span> <span style=" color: #FFF;">&#40;</span><span style=" color: #FFF;">!</span><span style="color: #d3d7cf">in_array</span><span style=" color: #FFF;">&#40;</span><span style="color: #729fcf;">$interval</span><span style=" color: #FFF;">,</span> <span style="color: #729fcf;">$intervals</span><span style=" color: #FFF;">&#41;</span><span style=" color: #FFF;">&#41;</span> <span style=" color: #FFF;">&#123;</span>
      <span style="color: #FFF; font-weight:bold;">return</span> <span style="color: #FFF;">false</span><span style=" color: #FFF;">;</span>
    <span style=" color: #FFF;">&#125;</span>
&nbsp;
    <span style="color: #729fcf;">$diff</span> <span style=" color: #FFF;">=</span> <span style="color: #7FB347;">0</span><span style=" color: #FFF;">;</span>
    <span style=" color: #C7DD0C">// Create temp time from time1 and interval</span>
    <span style="color: #729fcf;">$ttime</span> <span style=" color: #FFF;">=</span> <span style="color: #d3d7cf">strtotime</span><span style=" color: #FFF;">&#40;</span><span style="color: #FFC600;">&quot;+1 &quot;</span> <span style=" color: #FFF;">.</span> <span style="color: #729fcf;">$interval</span><span style=" color: #FFF;">,</span> <span style="color: #729fcf;">$time1</span><span style=" color: #FFF;">&#41;</span><span style=" color: #FFF;">;</span>
    <span style=" color: #C7DD0C">// Loop until temp time is smaller than time2</span>
    <span style="color: #FFF; font-weight:bold;">while</span> <span style=" color: #FFF;">&#40;</span><span style="color: #729fcf;">$time2</span> <span style=" color: #FFF;">&gt;=</span> <span style="color: #729fcf;">$ttime</span><span style=" color: #FFF;">&#41;</span> <span style=" color: #FFF;">&#123;</span>
      <span style="color: #729fcf;">$time1</span> <span style=" color: #FFF;">=</span> <span style="color: #729fcf;">$ttime</span><span style=" color: #FFF;">;</span>
      <span style="color: #729fcf;">$diff</span><span style=" color: #FFF;">++;</span>
      <span style=" color: #C7DD0C">// Create new temp time from time1 and interval</span>
      <span style="color: #729fcf;">$ttime</span> <span style=" color: #FFF;">=</span> <span style="color: #d3d7cf">strtotime</span><span style=" color: #FFF;">&#40;</span><span style="color: #FFC600;">&quot;+1 &quot;</span> <span style=" color: #FFF;">.</span> <span style="color: #729fcf;">$interval</span><span style=" color: #FFF;">,</span> <span style="color: #729fcf;">$time1</span><span style=" color: #FFF;">&#41;</span><span style=" color: #FFF;">;</span>
    <span style=" color: #FFF;">&#125;</span>
&nbsp;
    <span style="color: #FFF; font-weight:bold;">return</span> <span style="color: #729fcf;">$diff</span><span style=" color: #FFF;">;</span>
  <span style=" color: #FFF;">&#125;</span>
&nbsp;
  <span style=" color: #C7DD0C">// Testing...</span>
  <span style="color: #FFF; font-weight:bold;">echo</span> <span style="color: #FFC600;">&quot;Timestamps 2011-12-16 10:00 - 2011-12-17 10:25<span style="color: #ce5c00">\n</span>&quot;</span><span style=" color: #FFF;">;</span>
  <span style="color: #FFF; font-weight:bold;">echo</span> <span style="color: #FFC600;">&quot;Minute interval: &quot;</span> <span style=" color: #FFF;">.</span> dateDiff<span style=" color: #FFF;">&#40;</span><span style="color: #FFC600;">'2011-12-16 10:00'</span><span style=" color: #FFF;">,</span> <span style="color: #FFC600;">'2011-12-17 10:25'</span><span style=" color: #FFF;">,</span> <span style="color: #FFC600;">'minute'</span><span style=" color: #FFF;">&#41;</span> <span style=" color: #FFF;">.</span> <span style="color: #FFC600;">&quot;<span style="color: #ce5c00">\n</span>&quot;</span><span style=" color: #FFF;">;</span>
  <span style="color: #FFF; font-weight:bold;">echo</span> <span style="color: #FFC600;">&quot;Hour interval: &quot;</span> <span style=" color: #FFF;">.</span> dateDiff<span style=" color: #FFF;">&#40;</span><span style="color: #FFC600;">'2011-12-16 10:00'</span><span style=" color: #FFF;">,</span> <span style="color: #FFC600;">'2011-12-17 10:25'</span><span style=" color: #FFF;">,</span> <span style="color: #FFC600;">'hour'</span><span style=" color: #FFF;">&#41;</span> <span style=" color: #FFF;">.</span> <span style="color: #FFC600;">&quot;<span style="color: #ce5c00">\n</span>&quot;</span><span style=" color: #FFF;">;</span>
  <span style="color: #FFF; font-weight:bold;">echo</span> <span style="color: #FFC600;">&quot;Day interval: &quot;</span> <span style=" color: #FFF;">.</span> dateDiff<span style=" color: #FFF;">&#40;</span><span style="color: #FFC600;">'2011-12-16 10:00'</span><span style=" color: #FFF;">,</span> <span style="color: #FFC600;">'2011-12-17 10:25'</span><span style=" color: #FFF;">,</span> <span style="color: #FFC600;">'day'</span><span style=" color: #FFF;">&#41;</span> <span style=" color: #FFF;">.</span> <span style="color: #FFC600;">&quot;<span style="color: #ce5c00">\n</span>&quot;</span><span style=" color: #FFF;">;</span>
&nbsp;
  <span style="color: #FFF; font-weight:bold;">echo</span> <span style="color: #FFC600;">&quot;<span style="color: #ce5c00">\n</span><span style="color: #ce5c00">\n</span>&quot;</span><span style=" color: #FFF;">;</span>
&nbsp;
  <span style="color: #FFF; font-weight:bold;">echo</span> <span style="color: #FFC600;">&quot;Timestamps 2011-12-16 10:00 - 2014-06-17 10:25<span style="color: #ce5c00">\n</span>&quot;</span><span style=" color: #FFF;">;</span>
  <span style="color: #FFF; font-weight:bold;">echo</span> <span style="color: #FFC600;">&quot;Hour interval: &quot;</span> <span style=" color: #FFF;">.</span> dateDiff<span style=" color: #FFF;">&#40;</span><span style="color: #FFC600;">'2011-12-16 10:00'</span><span style=" color: #FFF;">,</span> <span style="color: #FFC600;">'2014-06-17 10:25'</span><span style=" color: #FFF;">,</span> <span style="color: #FFC600;">'hour'</span><span style=" color: #FFF;">&#41;</span> <span style=" color: #FFF;">.</span> <span style="color: #FFC600;">&quot;<span style="color: #ce5c00">\n</span>&quot;</span><span style=" color: #FFF;">;</span>
  <span style="color: #FFF; font-weight:bold;">echo</span> <span style="color: #FFC600;">&quot;Day interval: &quot;</span> <span style=" color: #FFF;">.</span> dateDiff<span style=" color: #FFF;">&#40;</span><span style="color: #FFC600;">'2011-12-16 10:00'</span><span style=" color: #FFF;">,</span> <span style="color: #FFC600;">'2014-06-17 10:25'</span><span style=" color: #FFF;">,</span> <span style="color: #FFC600;">'day'</span><span style=" color: #FFF;">&#41;</span> <span style=" color: #FFF;">.</span> <span style="color: #FFC600;">&quot;<span style="color: #ce5c00">\n</span>&quot;</span><span style=" color: #FFF;">;</span>
  <span style="color: #FFF; font-weight:bold;">echo</span> <span style="color: #FFC600;">&quot;Month interval: &quot;</span> <span style=" color: #FFF;">.</span> dateDiff<span style=" color: #FFF;">&#40;</span><span style="color: #FFC600;">'2011-12-16 10:00'</span><span style=" color: #FFF;">,</span> <span style="color: #FFC600;">'2014-06-17 10:25'</span><span style=" color: #FFF;">,</span> <span style="color: #FFC600;">'month'</span><span style=" color: #FFF;">&#41;</span> <span style=" color: #FFF;">.</span> <span style="color: #FFC600;">&quot;<span style="color: #ce5c00">\n</span>&quot;</span><span style=" color: #FFF;">;</span>
  <span style="color: #FFF; font-weight:bold;">echo</span> <span style="color: #FFC600;">&quot;Year interval: &quot;</span> <span style=" color: #FFF;">.</span> dateDiff<span style=" color: #FFF;">&#40;</span><span style="color: #FFC600;">'2011-12-16 10:00'</span><span style=" color: #FFF;">,</span> <span style="color: #FFC600;">'2014-06-17 10:25'</span><span style=" color: #FFF;">,</span> <span style="color: #FFC600;">'year'</span><span style=" color: #FFF;">&#41;</span> <span style=" color: #FFF;">.</span> <span style="color: #FFC600;">&quot;<span style="color: #ce5c00">\n</span>&quot;</span><span style=" color: #FFF;">;</span>
&nbsp;
<span style=" color: #FFF;">?&gt;</span></pre></div></div>

<p>Output:</p>
<pre>
Timestamps 2011-12-16 10:00 - 2011-12-17 10:25
Minute interval: 1465
Hour interval: 24
Day interval: 1

Timestamps 2011-12-16 10:00 - 2014-06-17 10:25
Hour interval: 21936
Day interval: 914
Month interval: 30
Year interval: 2
</pre>
<p>Note. first examples use your timestamps and last examples use date 2014-06-17&#8230; :)</p>
<p>I just tested this quickly and seems to be working, please let me know, if you have any problems with that?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sevag</title>
		<link>http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/comment-page-2/#comment-20649</link>
		<dc:creator>sevag</dc:creator>
		<pubDate>Sat, 17 Dec 2011 22:23:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.if-not-true-then-false.com/?p=792#comment-20649</guid>
		<description>just the integer value is enough

but supposing minutes interval

instead of getting
1 day 3 minutes

I get
1443 minutes</description>
		<content:encoded><![CDATA[<p>just the integer value is enough</p>
<p>but supposing minutes interval</p>
<p>instead of getting<br />
1 day 3 minutes</p>
<p>I get<br />
1443 minutes</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JR</title>
		<link>http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/comment-page-2/#comment-20630</link>
		<dc:creator>JR</dc:creator>
		<pubDate>Sat, 17 Dec 2011 12:42:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.if-not-true-then-false.com/?p=792#comment-20630</guid>
		<description>Hi again Sevag,

Do you need output example as 24.2 hours or is just 24 hours integer value enough? This full double/floating point value with decimals is easy to get with minute, hour, day, week intervals, but it&#039;s very hard to get with month and year intervals, because we can&#039;t use fixed seconds for month and year. If you want to use this &lt;em&gt;date diff function&lt;/em&gt; logic and accuracy, then it&#039;s only possible to get integer values.</description>
		<content:encoded><![CDATA[<p>Hi again Sevag,</p>
<p>Do you need output example as 24.2 hours or is just 24 hours integer value enough? This full double/floating point value with decimals is easy to get with minute, hour, day, week intervals, but it&#8217;s very hard to get with month and year intervals, because we can&#8217;t use fixed seconds for month and year. If you want to use this <em>date diff function</em> logic and accuracy, then it&#8217;s only possible to get integer values.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sevag</title>
		<link>http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/comment-page-2/#comment-20615</link>
		<dc:creator>Sevag</dc:creator>
		<pubDate>Fri, 16 Dec 2011 17:42:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.if-not-true-then-false.com/?p=792#comment-20615</guid>
		<description>Sorry 1465/10 = 146.5 =&gt; 146 services</description>
		<content:encoded><![CDATA[<p>Sorry 1465/10 = 146.5 =&gt; 146 services</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sevag</title>
		<link>http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/comment-page-2/#comment-20614</link>
		<dc:creator>Sevag</dc:creator>
		<pubDate>Fri, 16 Dec 2011 17:37:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.if-not-true-then-false.com/?p=792#comment-20614</guid>
		<description>Years
Month
Week
Day
Hour 
Minutes

I need this for the following reason

If my client asked for service every 10 minutes between 2011-12-16 10:00 till 2011-12-17 10:25

Since he picked minutes as interval then i want something that gives me in this case 1465.
Then i will do simply 1465 / (10) = 14.65.  So this means 14 services.

Now if he were same time interval but he picked every 2 hours
Then the formula should give me 24.2
Then i do 24.2 / 2 = 12.1 . so this means 12 services.</description>
		<content:encoded><![CDATA[<p>Years<br />
Month<br />
Week<br />
Day<br />
Hour<br />
Minutes</p>
<p>I need this for the following reason</p>
<p>If my client asked for service every 10 minutes between 2011-12-16 10:00 till 2011-12-17 10:25</p>
<p>Since he picked minutes as interval then i want something that gives me in this case 1465.<br />
Then i will do simply 1465 / (10) = 14.65.  So this means 14 services.</p>
<p>Now if he were same time interval but he picked every 2 hours<br />
Then the formula should give me 24.2<br />
Then i do 24.2 / 2 = 12.1 . so this means 12 services.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rajeev Dhar</title>
		<link>http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/comment-page-2/#comment-20611</link>
		<dc:creator>Rajeev Dhar</dc:creator>
		<pubDate>Fri, 16 Dec 2011 13:35:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.if-not-true-then-false.com/?p=792#comment-20611</guid>
		<description>you are great , your code and function is very useful

Thanks to help all</description>
		<content:encoded><![CDATA[<p>you are great , your code and function is very useful</p>
<p>Thanks to help all</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JR</title>
		<link>http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/comment-page-2/#comment-20604</link>
		<dc:creator>JR</dc:creator>
		<pubDate>Fri, 16 Dec 2011 08:53:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.if-not-true-then-false.com/?p=792#comment-20604</guid>
		<description>Hi Sevag,

What intervals you want to use? Seconds, minutes, hours, days...?</description>
		<content:encoded><![CDATA[<p>Hi Sevag,</p>
<p>What intervals you want to use? Seconds, minutes, hours, days&#8230;?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sevag</title>
		<link>http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/comment-page-2/#comment-20573</link>
		<dc:creator>sevag</dc:creator>
		<pubDate>Thu, 15 Dec 2011 02:57:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.if-not-true-then-false.com/?p=792#comment-20573</guid>
		<description>I love you code JR it&#039;s really functional but is there a way I can get the difference ONLY in one of the time.

For example, i want the following:

difference between 2011-12-14 21:54 and 2011-12-16 20:45

--- if i need it in HOURS then it will give for example 22.8 Hours
--- if i need it in MINUTES for examples it will return 1368
and so on??

if we were able to to this it will HELP me alot.  

THANKS AGAIN
Sevag</description>
		<content:encoded><![CDATA[<p>I love you code JR it&#8217;s really functional but is there a way I can get the difference ONLY in one of the time.</p>
<p>For example, i want the following:</p>
<p>difference between 2011-12-14 21:54 and 2011-12-16 20:45</p>
<p>&#8212; if i need it in HOURS then it will give for example 22.8 Hours<br />
&#8212; if i need it in MINUTES for examples it will return 1368<br />
and so on??</p>
<p>if we were able to to this it will HELP me alot.  </p>
<p>THANKS AGAIN<br />
Sevag</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JR</title>
		<link>http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/comment-page-2/#comment-20041</link>
		<dc:creator>JR</dc:creator>
		<pubDate>Fri, 18 Nov 2011 23:27:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.if-not-true-then-false.com/?p=792#comment-20041</guid>
		<description>Hi Charlie,

Thanks, I actually post this style solution &lt;a href=&quot;http://forums.if-not-true-then-false.com/index.php/topic,29.0.html&quot; rel=&quot;nofollow&quot;&gt;here&lt;/a&gt; and also native MySQL example.  ;)</description>
		<content:encoded><![CDATA[<p>Hi Charlie,</p>
<p>Thanks, I actually post this style solution <a href="http://forums.if-not-true-then-false.com/index.php/topic,29.0.html" rel="nofollow">here</a> and also native MySQL example.  ;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Charlie H</title>
		<link>http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/comment-page-2/#comment-20040</link>
		<dc:creator>Charlie H</dc:creator>
		<pubDate>Fri, 18 Nov 2011 21:02:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.if-not-true-then-false.com/?p=792#comment-20040</guid>
		<description>Why not use the native functionality? The &lt;a href=&quot;http://www.php.net/manual/en/dateinterval.format.php&quot; rel=&quot;nofollow&quot;&gt;format function&lt;/a&gt; in the DateTime class can be used to output the same information with just a few lines of code.

&lt;code&gt;
$now  = new DateTime(&#039;now&#039;);
$then = new DateTime($date);
$diff = $now-&gt;diff($then);
echo $diff-&gt;format(&#039;%a days&#039;);
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Why not use the native functionality? The <a href="http://www.php.net/manual/en/dateinterval.format.php" rel="nofollow">format function</a> in the DateTime class can be used to output the same information with just a few lines of code.</p>
<p><code><br />
$now  = new DateTime('now');<br />
$then = new DateTime($date);<br />
$diff = $now-&gt;diff($then);<br />
echo $diff-&gt;format('%a days');<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JR</title>
		<link>http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/comment-page-2/#comment-20037</link>
		<dc:creator>JR</dc:creator>
		<pubDate>Fri, 18 Nov 2011 18:12:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.if-not-true-then-false.com/?p=792#comment-20037</guid>
		<description>Thanks Abu! :) I will check your problem soon...</description>
		<content:encoded><![CDATA[<p>Thanks Abu! :) I will check your problem soon&#8230;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
<!-- This Quick Cache file was built for (  www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/feed/ ) in 0.35522 seconds, on Feb 11th, 2012 at 7:51 am UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 11th, 2012 at 8:51 am UTC -->
