0

I tried if condition if the date starts with 's' it's weekend but it's not working I don't know how to split date in to the day month and year.. Simply I need input two dates and get back array of workdays between that two dates.. No need to worry about holidays etc...

$begin = new DateTime('2020-10-29');
$end = new DateTime('2020-11-09');


$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);

foreach ($period as $dt) {

if(strpos($dt, 's') === 0) {
                                 echo "not workday";
}


else{
    echo $dt->format("l Y-m-d\n");
     }
}
  • Answer already here https://stackoverflow.com/questions/336127/calculate-business-days – Richard Housham Nov 03 '20 at 15:30
  • and here https://stackoverflow.com/questions/4312439/php-return-all-dates-between-two-dates-in-an-array – Richard Housham Nov 03 '20 at 15:31
  • 1
    Does this answer your question? [How to calculate the difference between two dates using PHP?](https://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – Isaac Nov 03 '20 at 15:54

1 Answers1

0

You forgot to ask for a format, so $dt is a object not a string and will not match any string positions. Replace:

if(strpos($dt, 's') === 0) {

With:

if(strpos($dt->format('l'), 'S') === 0) {

And to skip weekend days:

<?php 
$begin = new DateTime('2020-10-29');
$end = new DateTime('2020-11-09');


$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);

foreach ($period as $dt) {
    if(strpos($dt->format('l'), 'S') !== 0) {//!== to skip day with leading S.
        echo $dt->format("l Y-m-d\n");
     }
}
Thomas
  • 16
  • 4