calculating date diff
Moderator: General Moderators
calculating date diff
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?
Any help would be much appreciated?
i need the date day, month, year. its taking it javascript calendar day month year.
here is the code i tried:
i know the format for mktime is m-d-y. but it still didnt work.
feyd | Please review how to post code using
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);
?>feyd | Please review how to post code using
Code: Select all
andCode: Select all
tags. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]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.
that or i'm sure you could find a script on hotscripts that would do this for you.
have you looked into a combination of date() and strtotime()?
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?
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 
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];
Last edited by infolock on Thu Mar 31, 2005 3:50 pm, edited 1 time in total.
that wont' take into account days across months though will it? If not, why not just explode the dates and subtract the days ie:
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
Code: Select all
$date1 = "30-3-2005";
$date2 = "3-3-2005";
$date1 = explode("-",$date1);
$date2 = explode("-",$date2);
$daydiff = ($date1[0] - $date2[0]);
echo $daydiff;if I get a chance later this afternoon, I'll try to work through it for you.
Burr
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..
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..
here you go:
This should take into account leapyears etc and no need to create a large array.
let me know if that works for you.
This should take into account leapyears etc and no need to create a large array.
Code: Select all
<?
$date1 = "e;20/2/2005"e;; // dates in d/m/Y format
$date2 = "e;5/3/2005"e;; // dates in d/m/Y format
$date1 = explode("e;/"e;,$date1);
$date2 = explode("e;/"e;,$date2);
$date1 = date("e;m/d/Y"e;, strtotime($date1ї1]."e;/"e;.$date1ї0]."e;/"e;.$date1ї2]));
$date2 = date("e;m/d/Y"e;, strtotime($date2ї1]."e;/"e;.$date2ї0]."e;/"e;.$date2ї2]));
echo ((strtotime($date2) - strtotime($date1))/86400);
?>- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
My solution. 
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( '1-3-2005', '05-03-2005' );
?>Code: Select all
4i 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
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
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' ); ?>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
andCode: Select all
tags. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]