Jump to content

how to skip long weekend


ahmedmii

Recommended Posts

Hello ,

I've this code its skip the weekend ,and I've a table for special events has field datetime.

What I need when its come across this datetime skip it like it does with the weekend.

there's the code

<?php$database_date = '2011-08-12, 07:00:00'; //this comes from your database, $updated_time = strtotime('+' . hoursLeft($database_date,7,16,24) . ' hours',strtotime($database_date)); //Handle the date:$new_date = date('m-d-Y, H:i:s',$updated_time); //this is when the email should be sent.echo date('m-d-Y, H:i:s',strtotime($database_date)) . '<br />' . $new_date . '<-- you should be emailing at this time!'; //just echo's it to the screen.function hoursLeft($time,$work_starts,$work_ends,$time_to_email,$skip_weekends = true) { //this function handles the count of how many hours you need to fulfill your task.	$parts = explode(' ',$time); //split the time off of the date.	list($hour, $minute, $second) = explode(':',$parts[1]); //get the hours, minutes, seconds.	if($minute == 0) { //if the minutes is over 0, then return a full hour for it		$hours = $work_ends - $hour;	}	else { //otherwise, take into account that this isn't a full hour.		$hours = ($work_ends - 1) - $hour;	}	$day_count = 0; //original day count is 0;	for($i = $work_ends,$count = $hours; $count <= $time_to_email; $i++) { //Start the increment at 16, the count at the current hours, keep the count below 25, increment on each loop.		if($i > 24) { //if the increment is over 24, reset it to 0.			$i = 0;			++$day_count; //increment day_count when i goes over 24 hour limit.					if($skip_weekends == true) { //if you want to skip weekends				$day = date('l',strtotime("+ {$day_count} days",strtotime($time))); //get the full text of the current day the hours are pointing to.						if(in_array($day, array('Saturday','Sunday'))) { //and current day is in the weekend.										$hours += 24; //add 24 hours to the count.														continue; //restart loop, before any counting takes place.				}			}		}				if($i > $work_starts && $i <= $work_ends) { //if the increment is between 7 and 16, add to the count, which will break the loop at 24.					$count += 1;		}					$hours += 1; //add to the hours, the loop breaks at a count of 24, which will give us the total hours to add to the updated_time above.	}	return $hours; //return hours}?>

 

OUTPUT:

 


08-12-2011, 07:00:0008-16-2011, 14:00:00<-- you should be emailing at this time!9 hours for Friday:9 hours for Monday:7 hours for Tuesday:= 24 hours.

 

I'm waiting for your replys..

Link to comment
Share on other sites

I Apologize for this .

This code calculate 24 hours workday .The work starts at 7 and finishes at 16.This code skip the weekend,what I have is another table for special event has dates if it comes across it skiped like the weekend.

 


<?php

$database_date = '2011-08-12, 07:00:00'; //this comes from your database, 
$updated_time = strtotime('+' . hoursLeft($database_date,7,16,24) . ' hours',strtotime($database_date)); //Handle the date:
$new_date = date('m-d-Y, H:i:s',$updated_time); //this is when the email should be sent.

echo date('m-d-Y, H:i:s',strtotime($database_date)) . '<br />' . $new_date . '<-- you should be emailing at this time!'; //just echo's it to the screen.

function hoursLeft($time,$work_starts,$work_ends,$time_to_email,$skip_weekends = true) { //this function handles the count of how many hours you need to fulfill your task.
$parts = explode(' ',$time); //split the time off of the date.
list($hour, $minute, $second) = explode(':',$parts[1]); //get the hours, minutes, seconds.
if($minute == 0) { //if the minutes is over 0, then return a full hour for it
	$hours = $work_ends - $hour;
}
else { //otherwise, take into account that this isn't a full hour.
	$hours = ($work_ends - 1) - $hour;
}
$day_count = 0; //original day count is 0;
for($i = $work_ends,$count = $hours; $count <= $time_to_email; $i++) { //Start the increment at 16, the count at the current hours, keep the count below 25, increment on each loop.
	if($i > 24) { //if the increment is over 24, reset it to 0.
		$i = 0;
		++$day_count; //increment day_count when i goes over 24 hour limit.		
		if($skip_weekends == true) { //if you want to skip weekends
			$day = date('l',strtotime("+ {$day_count} days",strtotime($time))); //get the full text of the current day the hours are pointing to.		
			if(in_array($day, array('Saturday','Sunday'))) { //and current day is in the weekend.					
				$hours += 24; //add 24 hours to the count.									
				continue; //restart loop, before any counting takes place.
			}
		}
	}

	if($i > $work_starts && $i <= $work_ends) { //if the increment is between 7 and 16, add to the count, which will break the loop at 24.		
		$count += 1;
	}

	$hours += 1; //add to the hours, the loop breaks at a count of 24, which will give us the total hours to add to the updated_time above.
}
return $hours; //return hours
}
?>

 

 

OUTPUT:

 


08-12-2011, 07:00:000
8-16-2011, 14:00:00<-- you should be emailing at this time!
9 hours for Friday:
9 hours for Monday:
7 hours for Tuesday:
= 24 hours.


Link to comment
Share on other sites

Well, the user's gonna enter the special date saved in table of special event .

for example suppose it's friday 8-12-2011 7:00:00this code will send email on tusday ,but suppose the use enter Monday as

a special event it'll skip it like on weekend .So, the email will be sent on Wednesday.

Link to comment
Share on other sites

So why can you not just send the info to the function?

 

In the above script, the function is called here:

$updated_time = strtotime('+' . hoursLeft($database_date,7,16,24) . ' hours',strtotime($database_date)); //Handle the date:

 

Where the function is set, the arguments are very explicit on what arguments it expects.

function hoursLeft($time,$work_starts,$work_ends,$time_to_email,$skip_weekends = true) { //this function handles the count of how many hours you need to fulfill your task.

 

The first parameter is the current time, in the form of a timestamp from the database.

The second parameter is when the work day starts.

The third parameter is when the work day ends.

The fourth parameter is how many hours must elapse before the email goes out.

The fifth parameter is an optional parameter, true skips weekends, false includes them.  Default is true(skipped).

Link to comment
Share on other sites

Like this

 


function hoursLeft($time,$work_starts,$work_ends,$time_to_email,$skip_weekends = true,$skip_long_weekends = true) 
{
if($skip_long_weekends == true)	
 {
	$day = date('l',strtotime("+ {$day_count} days",strtotime($time)));
	$result=mysql_query('select `date` from `specialevent`');
	while($row=mysql_fetch_array)
	{	
		$database_date1 = $row["Date"];
		if(in_array($day,$database_date1)//$day==$day in table specialevents skip it
		$hours+=24;
		continue;
	}


}
}


Link to comment
Share on other sites

This the finall thing I get with it

if($skip_long_weekends == true) { //if you want to skip weekends
$day = date('l',strtotime("+ {$day_count} days",strtotime($time))); //get the full text of the current day the hours are pointing to.		
			$result10 = mysql_query('SELECT `Date` FROM special');
					while($row = mysql_fetch_array($result10))
				{
			$database_date1=$result10["Date"];
					if(in_array($day,$database_date1))
					{
					$hours += 24; //add 24 hours to the count.									
					continue; //restart loop, before any counting takes place.
		}
				}}

The problem is it checks the frist date in the table specail and the rests are ignored even I after I put the while nothing changed

 

Link to comment
Share on other sites

Simple problems, need simple changes, but over-analyzing leads to headaches. This should address your issues, and I hope it works 100%.  I've tested it, but not in every way possible.

 

Changes MARKED

<?php
function hoursLeft($time,$work_starts,$work_ends,$time_to_email,$skip_days = array()) { //ARGUMENTS CHANGED!!!
$parts = explode(' ',$time); //split the time off of the date.
list($hour, $minute, $second) = explode(':',$parts[1]); //get the hours, minutes, seconds.
if($minute == 0) { //if the minutes is over 0, then return a full hour for it
	$hours = $work_ends - $hour;
}
else { //otherwise, take into account that this isn't a full hour.
	$hours = ($work_ends - 1) - $hour;
}
$day_count = 0; //original day count is 0;
for($i = $work_ends,$count = $hours; $count <= $time_to_email; $i++) { //Start the increment at 16, the count at the current hours, keep the count below 25, increment on each loop.
	if($i > 24) { //if the increment is over 24, reset it to 0.
		$i = 1;
		++$day_count; //increment day_count when i goes over 24 hour limit.	
		if(!empty($skip_days)) { //if you want to skip weekends CHANGED FROM BOOLEAN, TO IS NOT EMPTY.
			$day = date('l',strtotime("+ {$day_count} days",strtotime($time))); //get the full text of the current day the hours are pointing to.				
			if(in_array($day, $skip_days)) { //and current day is in the weekend.	CHANGED ARGUMENTS TO in_array() FUNCTION.
				$hours += 24; //add 24 hours to the count.
				$i = 24;
				continue; //restart loop, before any counting takes place.
			}
		}
	}

	if($i > $work_starts && $i <= $work_ends) { //if the increment is between 7 and 16, add to the count, which will break the loop at 24.		
		$count += 1;			
	}

	$hours += 1; //add to the hours, the loop breaks at a count of 24, which will give us the total hours to add to the updated_time above.
}	
return $hours; //return hours
}
?>

 

How to use

This function returns an integer.  This integer represents hours based on the information you feed the function.

Arguments to the function are as follows.

1. A database styled timestamp. (YYYY-mm-dd H:i:s)

2. Time of day to start counting (integer 24 hour clock)

3. Time of day to stop counting (integer 24 hour clock)

4. Amount of time you wish to count (integer hours)

5. Days you wish to skip (array OPTIONAL)

 

Uses

<?php
$database_date = '2011-08-12, 07:00:00'; //this comes from your database,
//Start counting at 7am, count till 5pm, for 20 hours, no skip days.
$updated_time = strtotime('+' . hoursLeft($database_date,7,17,20) . ' hours',strtotime($database_date)); 
echo date('m-d-Y, H:i:s',$updated_time);

//Start counting at 7am, count till 5pm, for 20 hours, skip monday.
$updated_time = strtotime('+' . hoursLeft($database_date,7,17,20,array('Monday')) . ' hours',strtotime($database_date)); 
echo date('m-d-Y, H:i:s',$updated_time);

//Start counting at 7am, count till 5pm, for 20 hours, skip monday and wednesday.
$updated_time = strtotime('+' . hoursLeft($database_date,7,17,20,array('Monday','Wednesday') . ' hours',strtotime($database_date)); 
echo date('m-d-Y, H:i:s',$updated_time);
?>

 

Hope this helps.

Link to comment
Share on other sites

To put it simply

if the time is getting from table specail it checks the date from this table and compare it to the day this function calcaulate?

what I think is gonna work is


function hoursLeft($time,$work_starts,$work_ends,$time_to_email,$skip_weekends = true,$skip_long_weekends = true) {
if($skip_long_weekends == true)
{
$day = date('l',strtotime("+ {$day_count} days",strtotime($time)));
$result=mysql_query('select `date` from `specialevent`');	
while($row=mysql_fetch_array)
{
$database_date1 = $row["Date"];
if(in_array($day,$database_date1)//This if i can make his compare the timestamp for specailtable with the var $date I think it done
$hours+=24;
continue;
}
}
}

Link to comment
Share on other sites

You may do it how you wish, but I coded a function to be used as a function should.  Doing it your way, would defeat the purpose of making it a function.

 

<?php
$result=mysql_query('select `date` from `specialevent`');	
while($row=mysql_fetch_assoc($result))
{
$days[] = date('l',strtotime($row['date']));
}

//Start counting at 7am, count till 5pm, for 20 hours, skip monday.
$updated_time = strtotime('+' . hoursLeft($database_date,7,17,20,$days) . ' hours',strtotime($database_date)); 
echo date('m-d-Y, H:i:s',$updated_time);
?>

 

Link to comment
Share on other sites

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.