public function test_number_of_weekdays() { $dateFrom = '2011-01-01 00:00:00'; $dateTo = '2012-10-23 11:45:00'; //convert strings to date objects $dateFrom = new DateTime($dateFrom); $dateTo = new DateTime($dateTo); $this->assertEquals(472, number_of_weekdays($dateFrom, $dateTo)); }
public function index_json() { $dateFrom = $this->input->post('dateFrom'); $dateTo = $this->input->post('dateTo'); // do we have valid dates? is_date($dateFrom); is_date($dateTo); // include timezone adjustment $dateFromTimeZone = $this->input->post('dateFromTimeZone'); $dateToTimeZone = $this->input->post('dateToTimeZone'); // convert strings to date objects including the time zone $dateFrom = new DateTime($dateFrom . ' ' . $dateFromTimeZone); $dateTo = new DateTime($dateTo . ' ' . $dateToTimeZone); // check if dateFrom is earlier than dateTo, if not then swap them over, we are caldulating // the difference so this should be fine. if ($dateFrom->getTimestamp() > $dateTo->getTimestamp()) { $tempDate2 = $dateFrom; $dateFrom = $dateTo; $dateTo = $tempDate2; } // build up an objct to return as json $numberOfDays = number_of_days($dateFrom, $dateTo); $numberOfDaysResults = new stdClass(); $numberOfDaysResults->days = $numberOfDays; $numberOfDaysResults->seconds = convert_time($numberOfDays, "days", "seconds"); $numberOfDaysResults->minutes = convert_time($numberOfDays, "days", "minutes"); $numberOfDaysResults->hours = convert_time($numberOfDays, "days", "hours"); $numberOfDaysResults->years = convert_time($numberOfDays, "days", "years"); $numberOfWeekDays = number_of_weekdays($dateFrom, $dateTo); $numberOfWeekDaysResults = new stdClass(); $numberOfWeekDaysResults->days = $numberOfWeekDays; $numberOfWeekDaysResults->seconds = convert_time($numberOfWeekDays, "days", "seconds"); $numberOfWeekDaysResults->minutes = convert_time($numberOfWeekDays, "days", "minutes"); $numberOfWeekDaysResults->hours = convert_time($numberOfWeekDays, "days", "hours"); $numberOfWeekDaysResults->years = convert_time($numberOfWeekDays, "days", "years"); $numberOfCompleteWeeks = number_of_complete_weeks($dateFrom, $dateTo); $numberOfCompleteWeeksResults = new stdClass(); $numberOfCompleteWeeksResults->weeks = $numberOfCompleteWeeks; $numberOfCompleteWeeksResults->seconds = convert_time($numberOfCompleteWeeks, "weeks", "seconds"); $numberOfCompleteWeeksResults->minutes = convert_time($numberOfCompleteWeeks, "weeks", "minutes"); $numberOfCompleteWeeksResults->hours = convert_time($numberOfCompleteWeeks, "weeks", "hours"); $numberOfCompleteWeeksResults->years = convert_time($numberOfCompleteWeeks, "weeks", "years"); $dateCalcResults = new stdClass(); $dateCalcResults->numberOfDays = $numberOfDaysResults; $dateCalcResults->numberOfWeekDays = $numberOfWeekDaysResults; $dateCalcResults->numberOfCompleteWeeks = $numberOfCompleteWeeksResults; // output as json $this->output->set_content_type('application/json')->set_output(json_encode($dateCalcResults)); }