How to calculate time elapsed between two dates

KamBhai

Elite Member
Joined
Aug 13, 2010
Messages
1,538
Reaction score
1,498
Hello guys,
Can anyone help me with this?
If you can't write the program then it's fine just give me the logic and I will do myself.

I need to calculate the duration between 2 given dates.

Suppose, I choose 11/1/1992 and 8/6/2001
It should give the output as X year X month X days.

I know it's simple, but I am not getting the logic yet. Also don't forget the leap years.

Thanks in advance !
 
I think you will need flux capacitor to run this program on:)
 
It's easy in PHP :
Code:
<?php

function dateDiff($d1,$d2){
    $date1=strtotime($d1);
    $date2=strtotime($d2);
    $seconds = $date1 - $date2;
    $weeks = floor($seconds/604800);
    $seconds -= $weeks * 604800;
    $days = floor($seconds/86400);
    $seconds -= $days * 86400;
    $hours = floor($seconds/3600);
    $seconds -= $hours * 3600;
    $minutes = floor($seconds/60);
    $seconds -= $minutes * 60;
    $months=round(($date1-$date2) / 60 / 60 / 24 / 30);
    $years=round(($date1-$date2) /(60*60*24*365));
    $diffArr=array("Seconds"=>$seconds,
                  "minutes"=>$minutes,
                  "Hours"=>$hours,
                  "Days"=>$days,
                  "Weeks"=>$weeks,
                  "Months"=>$months,
                  "Years"=>$years
                 ) ;
   return $diffArr;
}

print_r(dateDiff("2012-02-21 11:11:11","2011-01-01 10:09:11"));
?>
 
Oh sorry I just googled and got this snippset lol :P
On a serious note, it's almost impossible to memorize all the functions. So, I don't care at all! It doesn't harm coz I have netbeans :)

EDIT: That won't support for PHP < 5.3 :(
@g0g0l

Someone needs to start going through the PHP manual - we have a function for that :p

See here: http://php.net/manual/en/datetime.diff.php
 
Last edited:
On a serious note, it's almost impossible to memorize all the functions.

I don't remember them either, it 's silly to remember them all. What you should remember though is what PHP functions are available in general. In this case for example, what I had in mind is that the DateTime class had a diff functionality so I opened up the manual to link to it. Modern IDEs do the job of remembering the syntax for us :)
 
Thanks for the tip man. I will try them out for sure :-)
EDIT:: N yeah, it really looks like PHP has become a graduate. I am discovering so many static methods these days. I really think I should go back to the school lol.
I don't remember them either, it 's silly to remember them all. What you should remember though is what PHP functions are available in general. In this case for example, what I had in mind is that the DateTime class had a diff functionality so I opened up the manual to link to it. Modern IDEs do the job of remembering the syntax for us :)
 
Last edited:
Code:
        Dim from As Date = DateTimePicker1.Value        Dim toDays As Date = DateTimePicker2.Value
        Dim years As Integer = toDays.Year - from.Year
        Dim mm As Integer = toDays.Month - from.Month
        If mm < 0 Then
            years = years - 1
            mm = 12 + mm
        End If
        Dim days As Integer = toDays.Day - from.Day
        If days < 0 Then
            mm = mm - 1
            days = days + Date.DaysInMonth(from.Year, from.Month)
        End If
        MsgBox("Y: " & years & " M: " & mm & " D: " & days)

Is this what you need?

Regards
 
Back
Top