Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    May 2009
    Location
    Bangalore
    Posts
    75
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Difference between two datetime

    Hi All ,


    I need to find out the difference between two date times like
    $logged_time='2009-06-01 01:38:35';
    $current_time='2009-06-01 07:48:45';

    I need the differnce between $logged_time and $current_time

    can any one help me ...

    Thanx

  • #2
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Are you sure this is a Javascript question? The format of your variables suggest PHP.

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    If you do need to handle ISO time formats with javascript, just remember that they refer to GMT time to seconds, and months are 1-12.

    Javascript Dates are local and to milliseconds, and months range from 0-11.

    A bulletproof method to convert ISO to a javascript date would have to include a switch for the
    short versions that use year, week in year and day in week, and the year and day in year formats,
    but this will handle the common long format you show:

    Code:
    Date.fromISO= function(s){
        var i= 0, A= s.split(/\D+/);
        while(i++<7){
            if(!A[i]) A[i]= 0;
            else A[i]= parseInt(A[i], 10);
        }
        --A[1];
        return new Date(Date.UTC(A[0], A[1], A[2], A[3], A[4], A[5]));      
    }
    var s1='2009-06-01 01:38:35';
    var s2='2009-06-01 07:48:45';
    alert((Date.fromISO(s2)-Date.fromISO(s1))/1000+' seconds');
    Last edited by mrhoo; 06-01-2009 at 02:21 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •