ahmedmii Posted August 16, 2011 Share Posted August 16, 2011 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.. Quote Link to comment https://forums.phpfreaks.com/topic/244894-how-to-skip-long-weekend/ Share on other sites More sharing options...
Nodral Posted August 16, 2011 Share Posted August 16, 2011 I think before anyone even thinks about replying to this you'll need to tidy your post up and make your code readable. Put some linebreaks and some whitespace in so we can see what's going on. Quote Link to comment https://forums.phpfreaks.com/topic/244894-how-to-skip-long-weekend/#findComment-1258029 Share on other sites More sharing options...
ahmedmii Posted August 16, 2011 Author Share Posted August 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/244894-how-to-skip-long-weekend/#findComment-1258033 Share on other sites More sharing options...
jcbones Posted August 16, 2011 Share Posted August 16, 2011 Gonna need more clarification. 1. You have special events. 2. Why do you need to make special events skip weekends (should be a hard date). 3. How does the special events interact with this script. Quote Link to comment https://forums.phpfreaks.com/topic/244894-how-to-skip-long-weekend/#findComment-1258307 Share on other sites More sharing options...
ahmedmii Posted August 17, 2011 Author Share Posted August 17, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/244894-how-to-skip-long-weekend/#findComment-1258407 Share on other sites More sharing options...
ahmedmii Posted August 17, 2011 Author Share Posted August 17, 2011 I hope this explanation is clear. Quote Link to comment https://forums.phpfreaks.com/topic/244894-how-to-skip-long-weekend/#findComment-1258647 Share on other sites More sharing options...
jcbones Posted August 17, 2011 Share Posted August 17, 2011 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). Quote Link to comment https://forums.phpfreaks.com/topic/244894-how-to-skip-long-weekend/#findComment-1258763 Share on other sites More sharing options...
ahmedmii Posted August 18, 2011 Author Share Posted August 18, 2011 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; } } } Quote Link to comment https://forums.phpfreaks.com/topic/244894-how-to-skip-long-weekend/#findComment-1258915 Share on other sites More sharing options...
ahmedmii Posted August 18, 2011 Author Share Posted August 18, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/244894-how-to-skip-long-weekend/#findComment-1259077 Share on other sites More sharing options...
jcbones Posted August 18, 2011 Share Posted August 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/244894-how-to-skip-long-weekend/#findComment-1259193 Share on other sites More sharing options...
ahmedmii Posted August 19, 2011 Author Share Posted August 19, 2011 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; } } } Quote Link to comment https://forums.phpfreaks.com/topic/244894-how-to-skip-long-weekend/#findComment-1259340 Share on other sites More sharing options...
jcbones Posted August 19, 2011 Share Posted August 19, 2011 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/244894-how-to-skip-long-weekend/#findComment-1259463 Share on other sites More sharing options...
ahmedmii Posted August 19, 2011 Author Share Posted August 19, 2011 What do you think the best way implement it .So, it can skip the date in table specail ? Quote Link to comment https://forums.phpfreaks.com/topic/244894-how-to-skip-long-weekend/#findComment-1259531 Share on other sites More sharing options...
TeNDoLLA Posted August 19, 2011 Share Posted August 19, 2011 Sorry I couldn't resists. What is a long weekend? Longer than normal weekends? I wish i had these... Quote Link to comment https://forums.phpfreaks.com/topic/244894-how-to-skip-long-weekend/#findComment-1259540 Share on other sites More sharing options...
ahmedmii Posted August 20, 2011 Author Share Posted August 20, 2011 In some companies they have Monday as a long weekend in some months. Quote Link to comment https://forums.phpfreaks.com/topic/244894-how-to-skip-long-weekend/#findComment-1259699 Share on other sites More sharing options...
ahmedmii Posted August 21, 2011 Author Share Posted August 21, 2011 How can I implement it without changing the function? Quote Link to comment https://forums.phpfreaks.com/topic/244894-how-to-skip-long-weekend/#findComment-1260034 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.