Jump to content

Recommended Posts

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;
}

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.

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.