Page 1 of 2

calculating date diff

Posted: Thu Mar 31, 2005 8:49 am
by juice
i am trying to calculate the difference between two dates. But any code i have tried gives back the wrong day difference. I have two fields - pickupdate and dropoffdate which are taken in from a javascript calendar. The format is just 12-02-2004 (d-m-y) going into the form. Maybe its the way the date is going through the form.

Any help would be much appreciated?

Posted: Thu Mar 31, 2005 9:06 am
by hawleyjr
Are you sure the format is (d-m-y) and not (m-d-Y)?

Try a date like: 30-01-2005 and see if it is a valid date.

Posted: Thu Mar 31, 2005 9:20 am
by juice
i need the date day, month, year. its taking it javascript calendar day month year.
here is the code i tried:

Code: Select all

<? $datediff = mktime(0,0,0,$dropoffdate) - mktime(0,0,0,$pickupdate);
	echo '<br /> Days difference = '. floor($datediff/60/60/24);
	?>
i know the format for mktime is m-d-y. but it still didnt work.


feyd | Please review how to post code using

Code: Select all

and

Code: Select all

tags. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Thu Mar 31, 2005 9:44 am
by infolock
the way i hanlded this was splitting hte date up into segments using Preg_Split. I then dumped each into a array and calculated the differences. for the months, you could incorporate a multidimentional array that would say Jan=>01, Dec=12, etc... it's the easiest way i know of how to do it.

that or i'm sure you could find a script on hotscripts that would do this for you.

Posted: Thu Mar 31, 2005 10:08 am
by juice
i have tried it out but cudn't get it to work. you cudn't post an example of the function preg_split with my code or post how you did it.

help be really grateful

Posted: Thu Mar 31, 2005 11:27 am
by Burrito
have you looked into a combination of date() and strtotime()?

Posted: Thu Mar 31, 2005 3:41 pm
by infolock
can't find the one with date, but i found another that follows the same principal with time (you just gotta change the split to look for - instead of : ..) and do whatever comparison you want.. and yeah, i could write it, but where is the learning experience in that? :twisted:

Code: Select all

$start_time = $_POST['first'];
$end_time = $_POST['second'];

$bob = preg_split("/[:]/",$start_time, PREG_SPLIT_OFFSET_CAPTURE);
$bob2 = preg_split("/[:]/",$end_time, PREG_SPLIT_OFFSET_CAPTURE);
$different = $bob2[0] - $bob[0];
edit: btw, this one just finds the difference between the hours.. although that should be self-evident if you do a print_r on either one of my $bob's ;)

Posted: Thu Mar 31, 2005 3:50 pm
by Burrito
that wont' take into account days across months though will it? If not, why not just explode the dates and subtract the days ie:

Code: Select all

$date1 = "30-3-2005";
$date2 = "3-3-2005";
$date1 = explode("-",$date1);
$date2 = explode("-",$date2);

$daydiff = ($date1[0] - $date2[0]);
echo $daydiff;
the problem with that is what if $date1 is 1-4-2005 and $date2 is 3-3-2005...that obviously won't give you an accurate number of days. I honestly think the best solution is going to be some combination of date() and strtotime().

if I get a chance later this afternoon, I'll try to work through it for you.

Burr

Posted: Thu Mar 31, 2005 3:54 pm
by infolock
sure it would, if you just do what i said.. i kinda mislead by saying Jan=>01 when i meant 01=>31
then you can just add $date[0] and $date[1], compare to $date[0]+$date[1]. could also do a check to see about year differences in there as well, but either way, you are still doing the same thing that is happening in your explode. the only difference is your explode will only compare the differences between days in 1 month.. but you could still achieve what he wants to do with either explode or preg_split..

Posted: Thu Mar 31, 2005 4:11 pm
by Burrito
here you go:
This should take into account leapyears etc and no need to create a large array.

Code: Select all

&lt;?
$date1 = &quote;20/2/2005&quote;; // dates in d/m/Y format
$date2 = &quote;5/3/2005&quote;; // dates in d/m/Y format
$date1 = explode(&quote;/&quote;,$date1);
$date2 = explode(&quote;/&quote;,$date2);
$date1 = date(&quote;m/d/Y&quote;, strtotime($date1&#1111;1].&quote;/&quote;.$date1&#1111;0].&quote;/&quote;.$date1&#1111;2]));
$date2 = date(&quote;m/d/Y&quote;, strtotime($date2&#1111;1].&quote;/&quote;.$date2&#1111;0].&quote;/&quote;.$date2&#1111;2]));
echo ((strtotime($date2) - strtotime($date1))/86400);
?&gt;
let me know if that works for you.

Posted: Thu Mar 31, 2005 4:19 pm
by feyd
My solution. :)

Code: Select all

&lt;?php

function makeTimestamp( $date )
{
	static $months = array( 1 =&gt; 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' );
	$fixed = preg_replace('#^\s*(\d{1,2})\s*(&#1111;-/\.])\s*(\d{1,2})\s*\\2\s*(\d{2,4})\s*$#e', 'sprintf(\'%02d-%s-%04d\', intval(\'\\1\'), $months&#1111;intval(\'\\3\')], (strlen(\'\\4\') &lt; 4 ? 1900 : 0) + intval(\'\\4\') )', $date);
	return strtotime($fixed);
}


function toDays( $timestamp )
{
	return intval(intval($timestamp) / (24 * 60 * 60));
}


function dateDiff( $date1, $date2 )
{
	$date1 = toDays(makeTimestamp( $date1 ));
	$date2 = toDays(makeTimestamp( $date2 ));
	
	return abs($date1 - $date2);
}

echo dateDiff( '1-3-2005', '05-03-2005' );

?&gt;

Code: Select all

4

Posted: Thu Mar 31, 2005 6:42 pm
by infolock
8O

"We're not worthy! We're not worthy!"

feyd, whatever books it is you read, i want like 10 of em.

Posted: Thu Mar 31, 2005 7:09 pm
by feyd
only "book" I read: php.net :)

Although having the regex bible is quite helpful with those things :)

Posted: Fri Apr 01, 2005 6:37 am
by juice
thanks every1 i will try them out later when get chance and let u know if they work or not

Posted: Fri Apr 01, 2005 5:31 pm
by juice
i tried out the last example but didnt work just getting 0 coming up. The dates are coming through because i did
echo $pickupdate;
echo $dropoffdate;
The two dates showed up in the format 05-03-05 and 07-03-05

The code i used was as follows: (same as yours jus puting in variables

Code: Select all

<?php function makeTimestamp( $date )
{   
 static $months = array( 1 => 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' );    $fixed = preg_replace('#^\s*(\d{1,2})\s*([-/\.])\s*(\d{1,2})\s*\\2\s*(\d{2,4})\s*$#e', 'sprintf(\'%02d-%s-%04d\', intval(\'\\1\'), $months[intval(\'\\3\')], (strlen(\'\\4\') < 4 ? 1900 : 0) + intval(\'\\4\') )', $date);    
return strtotime($fixed);
} 
 function toDays( $timestamp )
{    return intval(intval($timestamp) / (24 * 60 * 60));
} 
 function dateDiff( $date1, $date2 )
{    $date1 = toDays(makeTimestamp( $date1 ));  
     $date2 = toDays(makeTimestamp( $date2 )); 
     return abs($date1 - $date2);
} 
 echo dateDiff( '$pickupdate', '$dropoffdate' ); ?>
just getting printout of 0.
Your code does work. i tried it with manually entered dates instead of variables and it worked


feyd | Please review how to post code using

Code: Select all

and

Code: Select all

tags. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]