| Home => PHP Projects => Dates/Times |
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 Warning: fopen(../mikescounterfiles/.txt) [function.fopen]: failed to open stream: Permission denied in /home/michael/public_html/php/footer1.php on line 28 error opening file for read/write access. Return code: |