Difference Between Dates in P H P
PHP does not provide functions to calculate the difference between two
timestamps. Here are some ways to do it. As of this writing, I have only
accumulated these from the net, but I have not yet evaluated them.
if( $date2 > $date1 )
{ die( "error: date1 has to be >= date2 in calcDateDiff($date1, $date2)" ); }
$diff = $date1-$date2;
$seconds = 0;
$hours = 0;
$minutes = 0;
if($diff % 86400 <= 0) //there are 86,400 seconds in a day
{$days = $diff / 86400;}
if($diff % 86400 > 0)
{ $rest = ($diff % 86400);
$days = ($diff - $rest) / 86400;
if( $rest % 3600 > 0 )
{ $rest1 = ($rest % 3600);
$hours = ($rest - $rest1) / 3600;
if( $rest1 % 60 > 0 )
{ $rest2 = ($rest1 % 60);
$minutes = ($rest1 - $rest2) / 60;
$seconds = $rest2;
}else
$minutes = $rest1 / 60;
}else
$hours = $rest / 3600;}
$month1 = date('n',$timestamp1);
$month2 = date('n',$timestamp2);
$diff = $month2-$month1;
echo(($diff > 0 ? $diff-1 : $diff+1));
First date: 920264400 Second date: 974240831
$differecne = $first - $second;
echo $difference;
Returns: Difference: 53976431
$diffday = $difference/86400; echo "$diffday (days)";
Returns this: Difference: 624.73377314815 (days)
How can I further format this figure to display like: Difference: 624 days, X hours, X minutes, and X seconds
$string_difference = date("H:m:s",mktime(0,0,$diff,0,0,0)) // I think that'll work.
$filemod = filemtime( 'headlines.txt' );
$now = time();
$seconds = $now - $filemod;
/****************************
Make Minutes
****************************/
$minutes = floor( $seconds / ( int )60 );
/****************************
Get Left Over Seconds
****************************/
$left = $minutes * ( int )60;
$a_seconds = $seconds - $left;
/****************************
Output Message
****************************/
echo( 'File last modifed ' . $minutes . ' minute(s) and ' . $a_seconds . ' second(s) ago.' );
/* return the difference in days between two PHP timestamps */
$var_days = ($ts_1 - $ts_2) / 86400; /// 60 / 60 / 24;
|
Viewed 3860 times since May 26,2010 (59 per day over 65.2 days) |
Home
=> PHP Projects |
| Page: www.michaelhorowitz.com |
Last Updated:
May 24, 2005 5 PM |
|
| Prior to you, the last person to look at this web page did so on | Friday, July 30, 2010 8:13:16 PM |
| Current time on the server is | Friday, July 30, 2010 8:19:29 PM |
|
There have been 115248 page views of these PHP pages since February 10, 2004 for an average of 49 per day over 2,362.0 days
The last 5 people to view this web site were from these IP addresses:
| 38.107.191.86 | no name found |
| 195.62.15.206 | no name found |
| 64.8.98.179 | no name found |
| 178.107.220.58 | no name found |
| 92.224.25.228 | g224025228.adsl.alicedsl.de |