Jump to content
MakeWebGames

Function to calculate days between two dates?


Sim
 Share

Recommended Posts

Heres what I got so far:

I am not sure if its correct or not either... According to that site, my days should be correct:

My hours can change by changing dates but my minutes never change...

//vars
$current_date_stamp = time();
$cruise_date_stamp = mktime($_REQUEST['hour'], $_REQUEST['min'], 0, $_REQUEST['month'], $_REQUEST['day'], $_REQUEST['YYYY']);
$cruise_date = date("F j, Y", $cruise_date_stamp);
$date_diff = $cruise_date_stamp - $current_date_stamp;
$days = floor($date_diff/(60*60*24));  
$hours = floor($date_diff/(60*60*24*60));
$mins = floor($date_diff/(60*60*24*60*60));

echo "Cruise Date Stamps: " . $cruise_date_stamp;
echo "<BR>Current Date Stamp: " . $current_date_stamp;
echo "<BR>Stamp Difference: " . $date_diff;
echo "<br>Cruise Date: " .  $cruise_date;
echo "<br># of days: " . $days;
echo "<br># of hours: " . $hours;
echo "<br># of minutes: " . $mins . "<BR>";
Link to comment
Share on other sites

Assuming you mean you want to find the exact time between 2 dates. This is what i made up.

 

[noparse]function timeDiff($ts1,$ts2)
{
$ts1=(int) $ts1;
$ts2=(int) $ts2;
$time=max($ts1,$ts2)-min($ts1,$ts2);
$years=floor($time/60/60/24/364.25);
$time=(float) ($time-($years*364.25*24*60*60));
$days=floor($time/60/60/24);
$time=(float) ($time-($days*24*60*60));
$hours=floor($time/60/60);
$time=(float) ($time-($hours*60*60));
$minutes=floor($time/60);
$time=(float) ($time-($minutes*60));
$seconds=(float) $time;

$return=$years>0 ? $years.'y' : '';
$return.=$days>0 ? (empty($return) ? '' : ' ').$days.'d' : '';
$return.=$hours>0 ? (empty($return) ? '' : ' ').$hours.'h' : '';
$return.=$minutes>0 ? (empty($return) ? '' : ' ').$minutes.'m' : '';
$return.=$seconds>0 ? (empty($return) ? '' : ' ').$seconds.'s' : '';
$return=empty($return) ? 'NOW!' : $return;

return $return;
}[/noparse]
Edited by bluegman991
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...