...

View Full Version : Calculating the difference between two dates.



Gez
05-18-2008, 06:53 PM
I have two user imputted dates in the format (YYYY-MM-DD) and i need to work out the number of days between the two dates. I found this code but i cant seem to get it to work! Does anone have any alternaivs or suggestions?


function dateDiff($dformat, $endDate, $beginDate)
{
$date_parts1=explode($dformat, $beginDate);
$date_parts2=explode($dformat, $endDate);
$start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
return $end_date - $start_date;
}

This is my version of the code:

Function staylength(){

$checkin=$_REQUEST['checkin'];
$checkout=$_REQUEST['checkout'];

$date_parts1=explode("-", $checkin);
$date_parts2=explode("-", $checkout);
$checkin=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
$checkout=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);

$staylength = $checkout - $checkin;

echo $staylength;
}

mlseim
05-18-2008, 07:53 PM
So many different ways to do this ... just one example below.
I didn't try your code, but I think you need to bring in the variables
with the function, so they can be used "inside" the function.



$days = staylength($_REQUEST['checkin'],$_REQUEST['checkout']);

Function staylength($checkin,$checkout){
$date_parts1=explode("-", $checkin);
$date_parts2=explode("-", $checkout);
$checkin=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
$checkout=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);

$staylength = $checkout - $checkin;

echo $staylength;
}





<?php

// $days is the number of days between the dates

$init_day = "5";
$init_mth = "6";
$init_yr = "2008";

$dst_day = "15";
$dst_mth = "6";
$dst_yr = "2008";

//first convert to unix timestamp
$init_date = mktime(0,0,0,$init_mth,$init_day,$init_yr);
$dst_date = mktime(0,0,0,$dst_mth,$dst_day,$dst_yr);

$offset = $dst_date-$init_date; //Depending on which offset type you want, switch order

$days = floor($offset/60/60/24);

echo $days;
?>

Fou-Lu
05-19-2008, 03:04 AM
srttotime combined with strftime. Three lines of code :)
PHP has the greatest time handing of any language of used.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum