SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict Eric1776's Avatar
    Join Date
    May 2002
    Location
    Atlanta, GA
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problems calculating time difference

    Can someone tell me what I'm doing wrong with this?
    PHP Code:
    $temp mktime(800);
    $dayofweek date('w'$temp); 
    $start $temp + ($dayofweek) * 86400
    $end $start 86400 50400;
    $startDate  date('l, m-d-Y h:i A'$start);
    $endDate date('l, m-d-Y h:i A'$end);
    $timeDifference $end time();
    $timeLeft sprintf('%d Days %d Hours %d Minutes'$timeDifference 86400$timeDifference 3600 24$timeDifference 60 60); 
    It should calculate the start date as the very next Sunday at 8am and the end date as the following Sunday at 10pm and it does. The problem I'm having is getting the time difference between the end date/time and the current date/time.

    For example right now I'm getting these results:
    Start Date: Sunday, 05-18-2003 08:00 AM
    End Date: Sunday, 05-25-2003 10:00 PM
    Currently: Monday, 05-19-2003 12:20 PM
    Time Left: 13 Days 9 Hours 39 Minutes

    Time left is obviously wrong. It should be something like 6 days, 9 hours...

  2. #2
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You calculated the time difference between the end date and the current date. This means your result was correct

    Anyways, change:

    PHP Code:
    $timeDifference $end time(); 
    to

    PHP Code:
    $timeDifference $start time(); 
    to calculate the difference between the start date and now.

  3. #3
    SitePoint Addict Eric1776's Avatar
    Join Date
    May 2002
    Location
    Atlanta, GA
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe I missed something.

    If my results are correct why does it show my time left to be 13 days... when its only 6 days ... between the current date and the end date.

  4. #4
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you change the $timeDifference as I said above?
    If you use that the difference will be calculated between the start date and now.

    In your original code you used the end date of the event and the difference between that and the current date was 13 days (at the time of posting).
    Using the code above the value of $startdate was 25.05.2003 and the value of $enddate was 01.06.2003.
    Thus the difference was correct, although not as expected.

  5. #5
    SitePoint Addict Eric1776's Avatar
    Join Date
    May 2002
    Location
    Atlanta, GA
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, I see. I assumed I wanted to use the endDate and not the startDate. But it works.

    Thanks!


Bookmarks

Posting Permissions

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