# Thread: Problems calculating time difference

1. ## Problems calculating time difference

Can someone tell me what I'm doing wrong with this?
PHP Code:
``` \$temp = mktime(8, 0, 0);\$dayofweek = date('w', \$temp); \$start = \$temp + (7 - \$dayofweek) * 86400; \$end = \$start + 7 * 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. 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. 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. 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. Oh, I see. I assumed I wanted to use the endDate and not the startDate. But it works.

Thanks!

#### Posting Permissions

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