0
votes

I have a table, OperatingHours where 7 rows of data are recorded for the day of the week (Monday, Tuesday, Wednesday until Sunday). And there's a data column where user's can set true/false whether the clinic is open for the week.

I want to generate a list of date from current date till the end of the year, by the day of the week where the clinic is open. Currently below are my controller codes for the methods and I am getting the following error:

DateTime::modify(): Failed to parse time string (this [{"day":"Monday"},{"day":"Tuesday"},{"day":"Wednesday"},{"day":"Thursday"},{"day":"Friday"}]) at position 0 (t): The timezone could not be found in the database

public function index(){

$operatingDays = OperatingHour::where('clinic_open', true)->get('day');

$currentDate = Carbon::now()->toDateTimeString();
$endOfYear - Carbon::now()->endOfYear()->toDateTimeString();

$dt = $this->getWorkDayInRange($operatingDays, $currentDate, $endOfYear);

$data = [
'dt' = $dt,
];

return view('appointment', $data);
}

public function getWorkDayInRange($workday, $fromDate, $toDate){
$dates = [];
 $startDate = Carbon::parse($fromDate)->modify("this $workday");
 $endDate = Carbon::parse($toDate);

 //lte = less than or equal
 for ($date = $startDate; $date->lte($endDate); $date->addWeek()) {
   $dates[] = $date->toDateString();
 }

        return $dates;
}
2
Do you want to check day name also?Yasin Patel
@YasinPatel yap check the day name as well, and populate out the datesjunmingyeo98
getWorkDayInRange($operatingDays,... pass an array getWorkDayInRange($workday expect a workday string Failed to parse time string error message show you what you passed and so clearly there is now way to get any proper date from an array JSON.KyleK

2 Answers

0
votes

I have used something similar recently, check this out:

function generateDateRange($start, $end){
    $result = []
    $end = = strtotime($end);
    $current = = strtotime($start);

    while( $current <= $end ) {
         $result[] = date('Y-m-d', $current);
         $current = strtotime('+1 day', $current);
    }
    return $result;
}
0
votes

I have managed to retrieve out the lists of dates based on my operating hours details and holiday table.

Below are the changes to the codes I have made.

private function getHolidays()
    {
        $holidaysList = Holiday::where('clinic_id', '=', $_SESSION['clinic_ID'])->get();
        $holidayArray = json_decode($holidaysList, true);
        
        foreach($holidayArray as $holiday){
            //Convert each data from table to Y-m-d format to compare
            $holidays[] = date('Y-m-d', strtotime($holiday['date']));
            // $holidays[] = $holiday['date'];
        }
        return $holidays;
        
    }
    
    public function getWorkDayInRange($workday, $dateFrom, $dateTo)
    {
        $holidays = $this->getHolidays();
        $startDate = new DateTime($dateFrom);
        $endDate = new DateTime($dateTo);
        $interval = new DateInterval('P1D');
        $dateRange = new DatePeriod($startDate, $interval, $endDate);
        $results = [];
        foreach ($dateRange as $date) {
            $name = $date->format('l');
            if (in_array($name, $workday) && !in_array($date->format('Y-m-d'), $holidays)) {
                $results[] = $date->format('Y-m-d');
            }
        }
        return $results;

    }