Is there a way to use strtotime
to add working days (Monday to Friday) to a date? Or some other method? What I want to do is:
date ( 'Y-m-j' , strtotime ( '+3 working days' ) )
php function AddWorkDays(){ $i = 0; $d = 5; // Number of days to add while($i <= $d) { $i++; if(date('N', mktime(0, 0, 0, date(m), date(d)+$i, date(Y))) < 5) { $d++; } } return date(Y).
Try this. echo date('Y-m-d', strtotime('last monday', strtotime('next monday'))); It will return current date if today is monday, and will return last monday otherwise.
Use strtotime() function to get the first day of week using PHP. This function returns the default time variable timestamp and then use date() function to convert timestamp date into understandable date. strtotime() Function: The strtotime() function returns the result in timestamp by parsing the time string.
php $dt = "2008-02-23"; echo 'First day : '. date("Y-m-01", strtotime($dt)). ' - Last day : '. date("Y-m-t", strtotime($dt)); ?>
If you are limiting to weekdays use the string weekdays.
echo date ( 'Y-m-j' , strtotime ( '3 weekdays' ) );
This should jump you ahead by 3 weekdays, so if it is Thursday it will add the additional weekend time.
Source: http://www.php.net/manual/en/datetime.formats.relative.php
I have found this buggy when needing a larger amount of weekdays. I was looking for X amount of business days after the 1st of the current month.
Looked great at first until after adding > 5 business days (similar to what @zerkms found).
This has proved more accurate for me.
function _getBusinessDayOfMonth( $days ) { $time = strtotime(date("m/1/Y 00:00")); //finding # of business days after 1st of the month $i = 0; //start with zero while ($i < $days) { //loop through until reached the amount of weekdays $time = strtotime("+1 day", $time); //Increase day by 1 if (date("N", $time) < 6) { //test if M-F $i++; //Increase by 1 } } echo date("m/d/Y", $time); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With