Total Time Elapased Calculation

tl0tr
9 years ago

0

I have start time 7:00 AM and end time 7:00 PM.For example: If I subtract 31st March 2015 7:30 AM with 30th March 2015 6:30 PM I want the result to display as 1 Hour and not 13 Hours. I want the start and end time to be used as I have defined and not the whole 24 Hours cycle. Please let me know if this can be done. If someone can share some code ir any links that would be great.

13replies
6voices
254views
? [bolofecal]
9 years ago

0

This script gets the date and return the years elapsed (like the age of someone).

$date=“2015-04-10”;
list($year,$month,$day)=explode(‘-’,$date);
$today=mktime(0,0,0,date(’m'), date(’d'),date(‘Y’));
$born=mktime(0,0,0,$month,$day,$year);
$years_old=floor(($today-$born)/60/60/24/365.25); // /60 -> minutes /60 -> hours /24 -> day /365.25 -> year
echo $years_old;

This script is PHP.
I don’t understand you asked, because I don’t speak english. But I hope that it helps you.

dloser
9 years ago

0

@tl0tr**, you’ll have to explain a bit better, because it’s not clear why(/how) you’d want the result to be 1 hour. The only way I can make sense of it is to do hours%12.

Mart
9 years ago

1

Ah, I think I understand. Suppose car park has fees only between 8am and 6pm, if you park at 5pm and leave at 10am the next day the time to be charged should be 3 hours. Is this what you mean?

If so, it looks simple enough to code. I’m sure dloser will do it for you.

tl0tr
9 years ago

0

@Mart : Yep that is exactly what I mean. Can it be done?

tehron
9 years ago

0

@dloser’s already on it…

dloser
9 years ago | edited 9 years ago

0

@tl0tr**: of course, it can. Just think about how you would do it manually.

tl0tr
9 years ago

0

@dloser

Manually I just subtract the hours that I don’t need. Actually I don’t really know. When it comes to time conversion and stuffs then I get all confused.

Mart
9 years ago

0

Things to consider: is the time period the same every day (i.e. 7am-7pm every day of the year)? Do you want to be able to compare between dates that are not consecutive? What result do you want if times outside those hours are used? (e.g. 2015-03-31 9pm - 2015-03-25 3am).

If the start of the time period is always the same (7am) a first step would be just to subtract 7 from the hours (and clamp negative results to 0). Oh and work with 24 hour clock.

Here, have some shitty pseudocode (with the limits hardcoded in):
```
getcrazyhours(time a, time b){ // a - b
// adjust hours
if (a.hours>19) a.hours= 19;
if (b.hours>19) b.hours= 19;
a.hours -= 7;
if (a.hours<0) a.hours= 0;
b.hours -= 7;
if (b.hours<0) b.hours= 0;

days= a.date - b.date; // date without hours
hours= days * 12 + a.hours - b.hours; // if a is earlier in the day than b the hours part will be negative but that is correct

return hours;
}
```

Now, if you want different time periods on different days then I think you’d have to step through the dates between a and b adding on the hours for each (unless it was in a very predictable format, like different on weekends).

tl0tr
9 years ago

0

Oh I completely forgot. Weekends needs to be excluded.

dloser
9 years ago

0

This is starting to sound a lot like “do my (home)work for me.”

[quote=tl0tr]Manually I just subtract the hours that I don’t need. Actually I don’t really know. When it comes to time conversion and stuffs then I get all confused.[/quote]
All the more reason to try and figure it out yourself. Take a structured approach, split it up in cases you can handle, get an actual calendar and figure out how you would do it manually, etc. Or at least ask help with figuring out how to approach this instead of just asking for the end result.

Also, try to be clear and complete when you ask questions. Put in that extra little bit of effort before hitting ‘send’ instead of expecting us to do that by figuring out what you mean (in addition to already helping you out by answering).

tl0tr
9 years ago | edited 9 years ago

0

Actually I need this done in Excel but I am not sure if it can be done in Excel. I have huge amount of data and I want the Time Elapsed for all the data. I won’t able to do it one at a time it would take me ages but anyways thanks for help and advise everyone.

I am googling to see if this can be done in Excel.

If I get the answer I’ll post it.

For the time being I am keeping the thread open.

dloser
9 years ago

0

Of course it can be done in Excel. I’m pretty sure it’s Turing-complete. And there are macros.

nakee
9 years ago

0

@tl0tr Isn’t it just checking the cycle and removing 12 when needed?
and like @dloser said of course it can be done in excel

You must be logged in to reply to this discussion. Login
1 of 14

This site only uses cookies that are essential for the functionality of this website. Cookies are not used for tracking or marketing purposes.

By using our site, you acknowledge that you have read and understand our Privacy Policy, and Terms of Service.

Dismiss