florianv / business

:date: DateTime calculations in business hours
http://florianv.github.io/business
MIT License
361 stars 25 forks source link

HALF BUSINESS DAY ADDITION #23

Open legreco opened 7 years ago

legreco commented 7 years ago

Hello, How to add 1/2 business day to a date? I give you an example: In my work worked hours start from 7h30AM to 3h30PM in regular business day. In half worked business day hours start from 7h30AM to11h3AM. I am programming a leave request system. Employee should be able to take 1/2 business days.

Sorry if my english is not good. :)

florianv commented 7 years ago

Hello,

you can use a SpecialDay to change the working hours dynamically:

$days = [
    // Special day with dynamic opening hours depending on the date
    new SpecialDay(Days::FRIDAY, function (\DateTime $date) {
        if ('2015-05-29' === $date->format('Y-m-d')) {
            return [['9 AM', '12:00']];
        }

        return [['9 AM', '5 PM']];
    }),
];
legreco commented 7 years ago

Thanks. I solved it this way. But i do a little trick. Because employees can request half day leave, i split the business day in two equal part: time

` if(ctype_digit($leave_duration)) { //business day = 8 hours $hours=$leave_duration*8;

            while($hours>0)
            {
                $leave_start_date_time->add(new \DateInterval("PT4H"));      // I add 1/2 of business day
                $leave_start_date_time = $business_c->closest($leave_start_date_time);  // I get the closest business date time
                $hours=$hours-4;

            }
            // Finally  we get the employee return date time
            $employee_return_date_time=$business->closest($leave_start_date_time);

        }
        else
        {

//leave duration is decimal // we get the int part $leave_duration_int=intval($leave_duration);

            if($leave_duration_int>0)
            {
                $hours=$leave_duration_int*8;
                while($hours>0)
                {
                    $leave_start_date_time->add(new \DateInterval("PT4H"));
                    $leave_start_date_time = $business_c->closest($leave_start_date_time);
                    $hours=$hours-4;

                }

            }

//we add the intervall equal to the 1/2 business day remaining $leave_start_date_time->add(new \DateInterval("PT4H")); $leave_start_date_time = $business_c->closest($leave_start_date_time);

           // Finally  we get the employee return date time
            $employee_return_date_time=$business->closest($leave_start_date_time);
        }`