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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Working out days elapsed

    Hi Guys,

    in mysql i have 2 files (date files)

    they are:

    1)2007-08-27 13:20:03
    2)August 13, 2007, 10:02 pm

    they are displayed differently, is there a way i can work out the days elapsed between them like this? or do the time stamps need to be the same?

    thanks for any help guys

    Graham

  2. #2
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,080
    Thanks
    10
    Thanked 1,158 Times in 1,149 Posts
    They need to be the same format, but it's easy to do.
    The first date is easy: 2007-08-27 13:20:03 (format is - )
    The next one is a bit harder: August 13, 2007, 10:02 pm

    See this site as an example:

    http://www.developertutorials.com/tu...018/page1.html

    I'm wondering if MySQL can convert too.

  3. Users who have thanked mlseim for this post:

    graham23s (08-27-2007)

  4. #3
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi Mate,

    i have since made them both the same format for easiness (i should have done that in the first place i guess lol)

    they are now:

    2007-08-27 15:22:18
    2007-08-13 22:02:28

    i just need to work out the days elapsed but not sure how any help would be appreciated

    thanks mate

    Graham

  5. #4
    Senior Coder Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,651
    Thanks
    2
    Thanked 406 Times in 398 Posts
    PHP Code:
    $d1 strtotime('2007-08-27 15:22:18');
    $d2 strtotime('2007-08-13 22:02:28');
    $days ceil(($d1 $d2) / (60 60 24));
    echo 
    $days// 14 
    If this is in MySQL it has date functions that are very easy to use to compare dates and count days.

  6. Users who have thanked Inigoesdr for this post:

    graham23s (08-27-2007)

  7. #5
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts
    i'm stumped lol now code is:

    PHP Code:
                 $time_now time();
                 
    $d2 date('Y-m-d H:i:s'$time_now);
                 
    $d1 date('Y-m-d H:i:s'strtotime($ng_date));
                 
    $days ceil(($d1 $d2) / (60 60 24)); 
    when echoing out it produces:

    PHP Code:
    2007-06-02 16:53:24
    2007
    -08-27 18:00:51 
    which is right, but $days echoes out as 0.

    can anyone help lol

    cheers

    Graham
    Last edited by graham23s; 08-27-2007 at 07:21 PM.

  8. #6
    Senior Coder Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,651
    Thanks
    2
    Thanked 406 Times in 398 Posts
    $days uses timestamps. strtotime() generates them from the date/time string.
    If you're going to use those date formats then you need something like this:
    PHP Code:
    $days ceil((strtotime($d1) - strtotime($d2)) / (60 60 24)); 

  9. #7
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts
    thanks mate thats perfect.

    Graham


 

Posting Permissions

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