Location: PHPKode > scripts > PHP Pretty Date > Date_Difference.php
<html><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"></head><body><code><span style="color: rgb(0, 0, 0);">
<span style="color: rgb(0, 0, 187);">&lt;?php
<br></span><span style="color: rgb(255, 128, 0);">/*
<br>&nbsp;*&nbsp;JavaScript&nbsp;Pretty&nbsp;Date
<br>&nbsp;*&nbsp;Copyright&nbsp;(c)&nbsp;2008&nbsp;John&nbsp;Resig&nbsp;(jquery.com)
<br>&nbsp;*&nbsp;Licensed&nbsp;under&nbsp;the&nbsp;MIT&nbsp;license.
<br>&nbsp;*/
<br>
<br>//&nbsp;Ported&nbsp;to&nbsp;PHP&nbsp;&gt;=&nbsp;5.1&nbsp;by&nbsp;Zach&nbsp;Leatherman&nbsp;(zachleat.com)
<br>//&nbsp;Slight&nbsp;modification&nbsp;denoted&nbsp;below&nbsp;to&nbsp;handle&nbsp;months&nbsp;and&nbsp;years.
<br></span><span style="color: rgb(0, 119, 0);">class&nbsp;</span><span style="color: rgb(0, 0, 187);">Date_Difference
<br></span><span style="color: rgb(0, 119, 0);">{
<br>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;function&nbsp;</span><span style="color: rgb(0, 0, 187);">getStringResolved</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$date</span><span style="color: rgb(0, 119, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 187);">$compareTo&nbsp;</span><span style="color: rgb(0, 119, 0);">=&nbsp;</span><span style="color: rgb(0, 0, 187);">NULL</span><span style="color: rgb(0, 119, 0);">)
<br>&nbsp;&nbsp;&nbsp;&nbsp;{
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(!</span><span style="color: rgb(0, 0, 187);">is_null</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$compareTo</span><span style="color: rgb(0, 119, 0);">))&nbsp;{
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 187);">$compareTo&nbsp;</span><span style="color: rgb(0, 119, 0);">=&nbsp;new&nbsp;</span><span style="color: rgb(0, 0, 187);">DateTime</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$compareTo</span><span style="color: rgb(0, 119, 0);">);
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: rgb(0, 0, 187);">self</span><span style="color: rgb(0, 119, 0);">::</span><span style="color: rgb(0, 0, 187);">getString</span><span style="color: rgb(0, 119, 0);">(new&nbsp;</span><span style="color: rgb(0, 0, 187);">DateTime</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$date</span><span style="color: rgb(0, 119, 0);">),&nbsp;</span><span style="color: rgb(0, 0, 187);">$compareTo</span><span style="color: rgb(0, 119, 0);">);
<br>&nbsp;&nbsp;&nbsp;&nbsp;}
<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;function&nbsp;</span><span style="color: rgb(0, 0, 187);">getString</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">DateTime&nbsp;$date</span><span style="color: rgb(0, 119, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 187);">DateTime&nbsp;$compareTo&nbsp;</span><span style="color: rgb(0, 119, 0);">=&nbsp;</span><span style="color: rgb(0, 0, 187);">NULL</span><span style="color: rgb(0, 119, 0);">)
<br>&nbsp;&nbsp;&nbsp;&nbsp;{
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: rgb(0, 0, 187);">is_null</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$compareTo</span><span style="color: rgb(0, 119, 0);">))&nbsp;{
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 187);">$compareTo&nbsp;</span><span style="color: rgb(0, 119, 0);">=&nbsp;new&nbsp;</span><span style="color: rgb(0, 0, 187);">DateTime</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(221, 0, 0);">'now'</span><span style="color: rgb(0, 119, 0);">);
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 187);">$diff&nbsp;</span><span style="color: rgb(0, 119, 0);">=&nbsp;</span><span style="color: rgb(0, 0, 187);">$compareTo</span><span style="color: rgb(0, 119, 0);">-&gt;</span><span style="color: rgb(0, 0, 187);">format</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(221, 0, 0);">'U'</span><span style="color: rgb(0, 119, 0);">)&nbsp;-&nbsp;</span><span style="color: rgb(0, 0, 187);">$date</span><span style="color: rgb(0, 119, 0);">-&gt;</span><span style="color: rgb(0, 0, 187);">format</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(221, 0, 0);">'U'</span><span style="color: rgb(0, 119, 0);">);
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 187);">$dayDiff&nbsp;</span><span style="color: rgb(0, 119, 0);">=&nbsp;</span><span style="color: rgb(0, 0, 187);">floor</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$diff&nbsp;</span><span style="color: rgb(0, 119, 0);">/&nbsp;</span><span style="color: rgb(0, 0, 187);">86400</span><span style="color: rgb(0, 119, 0);">);
<br>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: rgb(0, 0, 187);">is_nan</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$dayDiff</span><span style="color: rgb(0, 119, 0);">)&nbsp;||&nbsp;</span><span style="color: rgb(0, 0, 187);">$dayDiff&nbsp;</span><span style="color: rgb(0, 119, 0);">&lt;&nbsp;</span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">)&nbsp;{
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: rgb(221, 0, 0);">''</span><span style="color: rgb(0, 119, 0);">;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: rgb(0, 0, 187);">$dayDiff&nbsp;</span><span style="color: rgb(0, 119, 0);">==&nbsp;</span><span style="color: rgb(0, 0, 187);">0</span><span style="color: rgb(0, 119, 0);">)&nbsp;{
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: rgb(0, 0, 187);">$diff&nbsp;</span><span style="color: rgb(0, 119, 0);">&lt;&nbsp;</span><span style="color: rgb(0, 0, 187);">60</span><span style="color: rgb(0, 119, 0);">)&nbsp;{
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: rgb(221, 0, 0);">'Just&nbsp;now'</span><span style="color: rgb(0, 119, 0);">;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;elseif(</span><span style="color: rgb(0, 0, 187);">$diff&nbsp;</span><span style="color: rgb(0, 119, 0);">&lt;&nbsp;</span><span style="color: rgb(0, 0, 187);">120</span><span style="color: rgb(0, 119, 0);">)&nbsp;{
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: rgb(221, 0, 0);">'1&nbsp;minute&nbsp;ago'</span><span style="color: rgb(0, 119, 0);">;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;elseif(</span><span style="color: rgb(0, 0, 187);">$diff&nbsp;</span><span style="color: rgb(0, 119, 0);">&lt;&nbsp;</span><span style="color: rgb(0, 0, 187);">3600</span><span style="color: rgb(0, 119, 0);">)&nbsp;{
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: rgb(0, 0, 187);">floor</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$diff</span><span style="color: rgb(0, 119, 0);">/</span><span style="color: rgb(0, 0, 187);">60</span><span style="color: rgb(0, 119, 0);">)&nbsp;.&nbsp;</span><span style="color: rgb(221, 0, 0);">'&nbsp;minutes&nbsp;ago'</span><span style="color: rgb(0, 119, 0);">;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;elseif(</span><span style="color: rgb(0, 0, 187);">$diff&nbsp;</span><span style="color: rgb(0, 119, 0);">&lt;&nbsp;</span><span style="color: rgb(0, 0, 187);">7200</span><span style="color: rgb(0, 119, 0);">)&nbsp;{
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: rgb(221, 0, 0);">'1&nbsp;hour&nbsp;ago'</span><span style="color: rgb(0, 119, 0);">;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;elseif(</span><span style="color: rgb(0, 0, 187);">$diff&nbsp;</span><span style="color: rgb(0, 119, 0);">&lt;&nbsp;</span><span style="color: rgb(0, 0, 187);">86400</span><span style="color: rgb(0, 119, 0);">)&nbsp;{
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: rgb(0, 0, 187);">floor</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$diff</span><span style="color: rgb(0, 119, 0);">/</span><span style="color: rgb(0, 0, 187);">3600</span><span style="color: rgb(0, 119, 0);">)&nbsp;.&nbsp;</span><span style="color: rgb(221, 0, 0);">'&nbsp;hours&nbsp;ago'</span><span style="color: rgb(0, 119, 0);">;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;elseif(</span><span style="color: rgb(0, 0, 187);">$dayDiff&nbsp;</span><span style="color: rgb(0, 119, 0);">==&nbsp;</span><span style="color: rgb(0, 0, 187);">1</span><span style="color: rgb(0, 119, 0);">)&nbsp;{
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: rgb(221, 0, 0);">'Yesterday'</span><span style="color: rgb(0, 119, 0);">;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;elseif(</span><span style="color: rgb(0, 0, 187);">$dayDiff&nbsp;</span><span style="color: rgb(0, 119, 0);">&lt;&nbsp;</span><span style="color: rgb(0, 0, 187);">7</span><span style="color: rgb(0, 119, 0);">)&nbsp;{
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: rgb(0, 0, 187);">$dayDiff&nbsp;</span><span style="color: rgb(0, 119, 0);">.&nbsp;</span><span style="color: rgb(221, 0, 0);">'&nbsp;days&nbsp;ago'</span><span style="color: rgb(0, 119, 0);">;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;elseif(</span><span style="color: rgb(0, 0, 187);">$dayDiff&nbsp;</span><span style="color: rgb(0, 119, 0);">==&nbsp;</span><span style="color: rgb(0, 0, 187);">7</span><span style="color: rgb(0, 119, 0);">)&nbsp;{
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: rgb(221, 0, 0);">'1&nbsp;week&nbsp;ago'</span><span style="color: rgb(0, 119, 0);">;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;elseif(</span><span style="color: rgb(0, 0, 187);">$dayDiff&nbsp;</span><span style="color: rgb(0, 119, 0);">&lt;&nbsp;(</span><span style="color: rgb(0, 0, 187);">7</span><span style="color: rgb(0, 119, 0);">*</span><span style="color: rgb(0, 0, 187);">6</span><span style="color: rgb(0, 119, 0);">))&nbsp;{&nbsp;</span><span style="color: rgb(255, 128, 0);">//&nbsp;Modifications&nbsp;Start&nbsp;Here
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;6&nbsp;weeks&nbsp;at&nbsp;most
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 119, 0);">return&nbsp;</span><span style="color: rgb(0, 0, 187);">ceil</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$dayDiff</span><span style="color: rgb(0, 119, 0);">/</span><span style="color: rgb(0, 0, 187);">7</span><span style="color: rgb(0, 119, 0);">)&nbsp;.&nbsp;</span><span style="color: rgb(221, 0, 0);">'&nbsp;weeks&nbsp;ago'</span><span style="color: rgb(0, 119, 0);">;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;elseif(</span><span style="color: rgb(0, 0, 187);">$dayDiff&nbsp;</span><span style="color: rgb(0, 119, 0);">&lt;&nbsp;</span><span style="color: rgb(0, 0, 187);">365</span><span style="color: rgb(0, 119, 0);">)&nbsp;{
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: rgb(0, 0, 187);">ceil</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$dayDiff</span><span style="color: rgb(0, 119, 0);">/(</span><span style="color: rgb(0, 0, 187);">365</span><span style="color: rgb(0, 119, 0);">/</span><span style="color: rgb(0, 0, 187);">12</span><span style="color: rgb(0, 119, 0);">))&nbsp;.&nbsp;</span><span style="color: rgb(221, 0, 0);">'&nbsp;months&nbsp;ago'</span><span style="color: rgb(0, 119, 0);">;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 187);">$years&nbsp;</span><span style="color: rgb(0, 119, 0);">=&nbsp;</span><span style="color: rgb(0, 0, 187);">round</span><span style="color: rgb(0, 119, 0);">(</span><span style="color: rgb(0, 0, 187);">$dayDiff</span><span style="color: rgb(0, 119, 0);">/</span><span style="color: rgb(0, 0, 187);">365</span><span style="color: rgb(0, 119, 0);">);
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: rgb(0, 0, 187);">$years&nbsp;</span><span style="color: rgb(0, 119, 0);">.&nbsp;</span><span style="color: rgb(221, 0, 0);">'&nbsp;year'&nbsp;</span><span style="color: rgb(0, 119, 0);">.&nbsp;(</span><span style="color: rgb(0, 0, 187);">$years&nbsp;</span><span style="color: rgb(0, 119, 0);">!=&nbsp;</span><span style="color: rgb(0, 0, 187);">1&nbsp;</span><span style="color: rgb(0, 119, 0);">?&nbsp;</span><span style="color: rgb(221, 0, 0);">'s'&nbsp;</span><span style="color: rgb(0, 119, 0);">:&nbsp;</span><span style="color: rgb(221, 0, 0);">''</span><span style="color: rgb(0, 119, 0);">)&nbsp;.&nbsp;</span><span style="color: rgb(221, 0, 0);">'&nbsp;ago'</span><span style="color: rgb(0, 119, 0);">;
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
<br>&nbsp;&nbsp;&nbsp;&nbsp;}
<br>}</span>
</span>
</code></body></html>
Return current item: PHP Pretty Date