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
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,396
    Thanks
    8
    Thanked 1,078 Times in 1,069 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.

  • Users who have thanked mlseim for this post:

    graham23s (08-27-2007)

  • #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

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 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.

  • Users who have thanked Inigoesdr for this post:

    graham23s (08-27-2007)

  • #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 06:21 PM.

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 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)); 

  • #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
    •