vapourmike Posted July 12, 2011 Share Posted July 12, 2011 Hi, I'm working on a script which will loop through all emails that have come in and then it tries to work out how long in hours it was until a reply was sent, this was fairly straight forward using the function I have below where I just pump in two dates and it spits out a value of how many hours it took. However, this isn't good enough for the needs of the script. We work in the office from 9 till 5.30 from monday to friday, what I now need to do is work out how many WORKING HOURS there was between the two dates and times I give it. Can someone help please? Thanks Mike function timeDiff($firstTime,$lastTime) { // convert to unix timestamps $firstTime=strtotime($firstTime); $lastTime=strtotime($lastTime); // perform subtraction to get the difference (in seconds) between times $timeDiff=$lastTime-$firstTime; // return the difference return $timeDiff; } Quote Link to comment https://forums.phpfreaks.com/topic/241796-php-date-difference-in-hours-with-a-working-twist/ Share on other sites More sharing options...
premiso Posted July 12, 2011 Share Posted July 12, 2011 Even though I didn't see any real effort you put into it, I was intrigued. I wrote it up, this does not take into account weekends or holidays. You will have to figure that part out. But for the most part should give you an accurate account of the number of working hours. <?php function workingHours($stDate, $enDate) { $busHours = 8.5; $interval = $stDate->diff($enDate); $totalHours = ($interval->d * 24) + $interval->h; $days = $interval->d; // If the total hours is exactly 24, we do not want to add 1. if (($totalHours % 24) != 0 && $interval->format('%R') == "+") $days += 1; return $totalHours - ($days * (24 - $busHours)); } // Time stamps can be used instead - // $start = new datetime('@'. time()); // where time() is the timestamp $start = new datetime('2011-07-12 10:00:00'); $end = new datetime('2011-07-13 10:00:00'); echo workingHours($start, $end); Not perfect, but at least you have something to work off of. This will only work on PHP 5.3, given I like the DateTime function. If you need it without that, well you will just have to work that out, but at least you have the logic more or less. Quote Link to comment https://forums.phpfreaks.com/topic/241796-php-date-difference-in-hours-with-a-working-twist/#findComment-1241861 Share on other sites More sharing options...
vapourmike Posted July 15, 2011 Author Share Posted July 15, 2011 Hi, Thank you for the code, however it doesn't make sense to me as it doesn't anywhere know when the office opens and when it shuts? (its 9:00 to 5:30 every day) ... My situation is we are monitoring on how quick we turn around a new proposal, if for example a proposal is loaded at 4:30pm, in order to fit our 2 hour turnaround, it would need to be completed by 10:00am the following day (2 WORKING hours) ... Currently my function just works out the hours between the two dates which doesn't help ... and in this scenario would give me a figure nearer 18 hours! Mike Quote Link to comment https://forums.phpfreaks.com/topic/241796-php-date-difference-in-hours-with-a-working-twist/#findComment-1243022 Share on other sites More sharing options...
vapourmike Posted July 15, 2011 Author Share Posted July 15, 2011 Becomming quite an urgent need now, google returns no help at all *cries* Quote Link to comment https://forums.phpfreaks.com/topic/241796-php-date-difference-in-hours-with-a-working-twist/#findComment-1243053 Share on other sites More sharing options...
TeNDoLLA Posted July 15, 2011 Share Posted July 15, 2011 Could be also that people don't get what exactly you want. Atleast I don't. Quote Link to comment https://forums.phpfreaks.com/topic/241796-php-date-difference-in-hours-with-a-working-twist/#findComment-1243054 Share on other sites More sharing options...
premiso Posted July 15, 2011 Share Posted July 15, 2011 Well if it is getting urgent, you better up and confess that you have no clue how to do it, higher someone, or take some math courses. The function above is very generic and yea, does not account for much. But it should be a good starting point for most programmers to go off of. You are going to need some if statements in there checking the hour time, how long it spans, if it spams a holiday or a weekend, and be able to add it up from there. It is a really complex situation, don't kid your self and will take a bit of brain power. But you are not really showing us that you are doing trial and error and you seem to just want us to code it up for you, sorry, that is why this has had so low of posts. Yea. Best of luck with your urgent need. Quote Link to comment https://forums.phpfreaks.com/topic/241796-php-date-difference-in-hours-with-a-working-twist/#findComment-1243065 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.