Jump to content

Php 2 Dates Strip Outside Business Hours


morrisjohnny

Recommended Posts

Hello All.I'm currently working on a custom reporting function.What i'm looking todo is extract the total time elapsed ($start) for each ticket during business hours the business hours are:mon-fri: 8:30-17:30 So if a ticket was loggedMonday 8:00 and closed: Monday 9 it's 0.5 (half an hour because the SLA/Business hour states from 8:30)Monday 13:00 and closed: Tuesday 12:00 it's 9 (Because 23hours (actual time between create_time and date_completed) - 14(number of hours the SLA/Business hours does not run for = ))Friday 17:00 and closed: Monday 09:00 its 1 (again because our buiness hour stops on friday at 17:30 and re-starts from monday 08:30. Note some tickets are logged outside of business hours and some tickets may be closed outside business hours.If they are opened they need to be taken from when the business re-opens (as you'd expect)If they are closed they need to be taken from when the business closed. I'm currently calling a function called

strip_out_of_ours($start,$end){	    $start_month=date_format($start,'m');	    $end_month=date_format($start,'m');	    if(date_format($start,'H')>'17'&&date_format($start,'i')>'30')// if this has been logged after 17:30 but before midnight assign the date for tomorrow at 8:30		    $start=date('y-m-d H:i:s',strtotime("+1 day",strtotime(date_format($start,'y-m-d').' 08:30:00')));	    if(date_format($start,'H')<'08'&&date_format($start,'i')<'30')// if this has been logged before8:30 make it 8:30		    $start=date('y-m-d H:i:s',strtotime(date_format($start,'y-m-d').' 08:30:00'));	    if(date_format($end,'H')>'17'&&date_format($end,'i')>'30')		    $end=date('y-m-d H:i:s',strtotime(date_format($end,'y-m-d').' 08:30:00'));	    if(date_format($start,'H')<'08'&&date_format($start,'i')<'30')		    $end=date('y-m-d H:i:s',strtotime("-1 day",strtotime(date_format($end,'y-m-d').' 08:30:00')));	    return var_dump($start);    }

Link to comment
Share on other sites

sounds like you know what your individual use cases are, so I'm not sure what the exact question/problem is. Personally though, I would be doing all this with timestamps instead of a bunch of date conversions. It will be that much easier on you and less cluttered when you write your if/else statements. It sounds like you have a few normal cases and then lots of specific cases within each one. I would try and start by first defining whether the hours logged fall in or out of business hours, and then define the specific edge cases within each block. I think it will take some trial and error to get the logic going, and then as you step through it, you will find betters ways to refine your algorithms. But start big, and try and be as broad as possible and then slowly narrow it down based on common logic and use cases.

Link to comment
Share on other sites

sounds like you know what your individual use cases are, so I'm not sure what the exact question/problem is. Personally though, I would be doing all this with timestamps instead of a bunch of date conversions. It will be that much easier on you and less cluttered when you write your if/else statements. It sounds like you have a few normal cases and then lots of specific cases within each one. I would try and start by first defining whether the hours logged fall in or out of business hours, and then define the specific edge cases within each block. I think it will take some trial and error to get the logic going, and then as you step through it, you will find betters ways to refine your algorithms. But start big, and try and be as broad as possible and then slowly narrow it down based on common logic and use cases.
Thanks theScientist yeah the cases above are what i'm trying to program. Whats the difference between timestamps and dates? the data has to be stored as a date. Working with dates/timestamps are probably my weakest point hence why i'm really struggeling with this. I've managed to almost get it working (so i think) however when i tried to convert date_format($start,'u') (start is a new DateTime object) it sets the unix time to be ZERO ?at the moment i'm putting in static data such as business opening hours but at point this will need to be from a database value. Here's my current code which is having problems convert from $start to unix stamp so i can remove out of hours time
function strip_out_of_hours($start,$end){ 		if(date_format($start,'H')>'17'&&date_format($start,'i')>'30')			$start=new DateTime(date('y-m-d H:i:s',strtotime("+1 day",strtotime(date_format($start,'y-m-d').' 08:30:00'))));		if(date_format($start,'H')<'08'&&date_format($start,'i')<'30')			$start=new DateTime(date('y-m-d H:i:s',strtotime(date_format($start,'y-m-d').' 08:30:00')));		if(date_format($end,'H')>'17'&&date_format($end,'i')>'30')			$end=new DateTime(date('y-m-d H:i:s',strtotime(date_format($end,'y-m-d').' 17:30:00')));		if(date_format($end,'H')<'08'&&date_format($end,'i')<'30')			$end=new DateTime(date('y-m-d H:i:s',strtotime("-1 day",strtotime(date_format($end,'y-m-d').' 17:30:00')))); 		$date_diff=(date_format($end,'d')-date_format($start,'d'));		$time_diff=(date_format($end,'u')-date_format($start,'u'));		echo 'Time Diff Begin'.$time_diff;				for($i=$date_diff;$i!=0;$i--){			echo '<br />-'.$i.'--'.$time_diff;			$test=getdate(date_format($start,'u'));						if(getdate(date_format($start,'u'))==0||getdate(date_format($start,'u'))==6)//day is Sunday Remove 24 hours (full hours on sunday's date				$time_diff=($time_diff-86400);//Removes 24 hours from the timestamp Because we are closed on Sundays			else if(getdate(date_format($start,'u'))==1||getdate(date_format($start,'u'))==2||getdate(date_format($start,'u'))==3||getdate(date_format($start,'u'))==4||getdate(date_format($start,'u'))==5) //Get Mondays,Tuesdays,Wednesdays,Thursday and Fridays				$time_diff=($time_diff-54000);//Removes 24 hours from the timestamp Because we are closed on Sundays					} 		return '<br />'.date_format($start,'Y-m-d H:i:s').'|'.date_format($end,'Y-m-d H:i:s').'--------Hours Elapsed Inside Business Hours'.$time_diff;	}

Link to comment
Share on other sites

timestamps are an integer value representing the number of seconds from the epoch (1/1/1970). It provides a more absolute way of representing the date in such a way that adding, subtracting, comparing the values can be simplified. Date format is a formatted representation of a timestamp, i.e. MM/DD/YYYY. I can't vouch for the methods and classes you are using, I don't think they are native to PHP. PHP has it's own date functions and methods for getting timestamps, converting dates to readable formats, etc.

Link to comment
Share on other sites

so using timestamps how would i be able to identifiy midnight to 8:30 and 5:30 till midnight monday to friday? i now understand how the dates methods i'm using (thank you PHP) is converting everything from all the different values. I know this code is the worst i have ever seen and coded i just don't know the best way to do this. It's due for tomorrow hence why i'm 'just carrying on with the scruffy way' i will be going back and look to tidy this up. (that's why i'm interested in timestamps)

Link to comment
Share on other sites

Well the reasoning behind saving timestamps is that you aren't locked into a specific display. Using date(), you can format the date hundreds of ways with just one number, rather than having to do conversions if your formatting changes, or if you need to do math with it. but, you can use a combination of functions, like mktime, to generate a specific timestamp for a particular date/time. Having numbers (in this case timestamps) can certainly simplify basic arithmetic and comparisons (<,>, etc).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...