Strahan Posted June 10, 2014 Share Posted June 10, 2014 Hi. I am writing software to choose employees for drug testing. Originally it was choosing 5 per day, Mon-Thu. No problem there, that's easy. Now, they want to ensure everyone gets tested each calendar year. So now I need the program to look at the date and calculate the days between now and 12/31 then divide the people that haven't been tested by days to get number per day that need testing so it gets all done by the end of the year. The snag I hit is how to calculate it. I can easily get days to 12/31, but I need to drop the Fridays, Saturdays and Sundays of each week. That has me a little perplexed. Is there a way to make that kind of calculation? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/289093-odd-date-calculation/ Share on other sites More sharing options...
Strahan Posted June 10, 2014 Author Share Posted June 10, 2014 I found some code while Googling: $workdays = explode(",", "1,1,1,1,0,0,0"); // turn string to array (Monday to Sunday) $days = 0; $time = strtotime("2014-06-01"); $end_time = strtotime("2014-06-30"); while ($time < $end_time) { $day_of_week = date("N", $time) - 1; // 0 = Monday, 6 = Sunday if ($workdays[$day_of_week] == 1) { $days++; } $time = strtotime("+1 day", $time); // add one day to time } echo $days; Quote Link to comment https://forums.phpfreaks.com/topic/289093-odd-date-calculation/#findComment-1482357 Share on other sites More sharing options...
Strahan Posted June 10, 2014 Author Share Posted June 10, 2014 Odd, it cut off my text after the code. Anyway, it works fine for months that have whole, unbroken weeks. Like this month, June 14. 16 days matching my criteria (Mon-Thu). However, if I try it with a month like July where the first is on Tuesday and the thirty-first is on Thursday, there are 19 days but the code returns 18. In September, there are 18 days but the code returns 17. I could add $days++, but for unbroken months it will be wrong and I'm not sure how to programatically tell that a month is broken up or not. Then again, it's after midnight so maybe I should try tomorrow when I am not sleepy Quote Link to comment https://forums.phpfreaks.com/topic/289093-odd-date-calculation/#findComment-1482358 Share on other sites More sharing options...
boompa Posted June 10, 2014 Share Posted June 10, 2014 (edited) How about getting the number of days until the end of the year instead of looking at months? Then do some arithmetic. Edited June 10, 2014 by boompa Quote Link to comment https://forums.phpfreaks.com/topic/289093-odd-date-calculation/#findComment-1482373 Share on other sites More sharing options...
Barand Posted June 10, 2014 Share Posted June 10, 2014 try $dt1 = new DateTime('tomorrow'); $dt2 = new DateTime('2015-01-01'); $di = DateInterval::createFromDateString('next weekday'); $dp = new DatePeriod($dt1, $di, $dt2); $count = 0; foreach ($dp as $d) { $dt = $d->format('D M j'); $wd = $d->format('w'); if ($wd != 5) { // not Friday ++$count; echo "$dt<br>"; // optional } } echo "<br>Total days - $count"; Quote Link to comment https://forums.phpfreaks.com/topic/289093-odd-date-calculation/#findComment-1482415 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.