Jump to content

Recommended Posts

Hi Guys,

I have a strange issue and can't get it to work properly.

Here is the code that calculate the time in minutes between two dates with a 5 days work week and 8 hours a day.
The script runs properly and calculate the right time in minutes.

Here is the code:

$begintime = '2013-17-06  12:00';
$endtime = '2013-19-06  10:00';

$start = new DateTime($begintime);
$end = new DateTime($endtime);

    $oneday = new DateInterval('P1D');
    $hours = 0; // start at 0

    foreach(new DatePeriod($start, $oneday, $end->add($oneday)) as $day)
    {
        if($day->format('N') < 5) // weekday, 'N' number days 1 (mon) to 7 (sun)
        {
            $hours += 8; // add 8-hr workday
        }
    }

   echo $hours;
  

    date_default_timezone_set("Europe/Amsterdam");
    $startDate=strtotime($begintime);
    $endDate = strtotime($endtime);

   seconds2human(work_hours_diff($startDate,$endDate));

    function seconds2human($ss) {
        $s = $ss%60;
        $m = floor(($ss%3600)/60);
        $h = floor(($ss)/3600);
        $h_total = floor(($ss)/60);

mysql_query("UPDATE table SET minutes = '$h_total' WHERE id = '$id'");
echo mysql_error();


        return "$h hours, $m minutes, $s seconds, totaal: $h_total Minuten";

    }

    function work_hours_diff($date1,$date2) {
        if ($date1>$date2) {
            $tmp=$date1;
            $date1=$date2;
            $date2=$tmp;
            unset($tmp);
            $sign=-1;
        } else $sign = 1;
        if ($date1==$date2) return 0;

        $days = 0;
        $working_days = array(1,2,3,4,5,6,7); // Monday-->Sunday
        $working_hours = array(9.5, 18); // from 9:30 to 18:00
        $current_date = $date1;

        $beg_h = floor($working_hours[0]);
        $beg_m = ($working_hours[0]*60)%60;
        $end_h = floor($working_hours[1]);
        $end_m = ($working_hours[1]*60)%60;

        //In case date1 is on same day of date2
        if (mktime(0,0,0,date('n', $date1), date('j', $date1), date('Y', $date1))==mktime(0,0,0,date('n', $date2), date('j', $date2), date('Y', $date2))) {
            //If its not working day, then return 0
            if (!in_array(date('w', $date1), $working_days)) return 0;

            $date0 = mktime($beg_h, $beg_m, 0, date('n', $date1), date('j', $date1), date('Y', $date1));
            $date3 = mktime($end_h, $end_m, 0, date('n', $date1), date('j', $date1), date('Y', $date1));

            if ($date1<$date0) {
                if ($date2<$date0) return 0;
                $date1 = $date0;
                if ($date2>$date3) $date2=$date3;
                return $date2-$date1;
            }
            if ($date1>$date3) return 0;
            if ($date2>$date3) $date2=$date3;
            return $date2-$date1;
        }

        //setup the very next first working time stamp
        if (!in_array(date('w',$current_date) , $working_days)) {
            // the current day is not a working day

            // the current time stamp is set at the beginning of the working day
            $current_date = mktime( $beg_h, $beg_m, 0, date('n',$current_date), date('j',$current_date), date('Y',$current_date) );

            // search for the next working day
            while ( !in_array(date('w',$current_date) , $working_days) ) {
                $current_date += 24*3600; // next day
            }
        } else {
            // check if the current timestamp is inside working hours
            $date0 = mktime( $beg_h, $beg_m, 0, date('n',$current_date), date('j',$current_date), date('Y',$current_date) );
            // it's before working hours, let's update it
            if ($current_date<$date0) $current_date = $date0;

            $date3 = mktime( $end_h, $end_m, 0, date('n',$current_date), date('j',$current_date), date('Y',$current_date) );

            if ($date3<$current_date) {
                // outch ! it's after working hours, let's find the next working day
                $current_date += 24*3600; // the day after
                // and set timestamp as the beginning of the working day
                $current_date = mktime( $beg_h, $beg_m, 0, date('n',$current_date), date('j',$current_date), date('Y',$current_date) );
                while ( !in_array(date('w',$current_date) , $working_days) ) {
                    $current_date += 24*3600; // next day
                }
            }
        }

        // so, $current_date is now the first working timestamp available...

        // calculate the number of seconds from current timestamp to the end of the working day
        $date0 = mktime( $end_h, $end_m, 0, date('n',$current_date), date('j',$current_date), date('Y',$current_date) );
        $seconds = $date0-$current_date;

        // calculate the number of days from the current day to the end day

        $date3 = mktime( $beg_h, $beg_m, 0, date('n',$date2), date('j',$date2), date('Y',$date2) );
        while ( $current_date < $date3 ) {
            $current_date += 24*3600; // next day
            if (in_array(date('w',$current_date) , $working_days) ) $days++; // it's a working day
        }
        if ($days>0) $days--; //because we've already count the first day (in $seconds)

        // check if end's timestamp is inside working hours
        $date0 = mktime( $beg_h, $beg_m, 0, date('n',$date2), date('j',$date2), date('Y',$date2) );
        if ($date2<$date0) {
            // it's before, so nothing more !
        } else {
            // is it after ?
            $date3 = mktime( $end_h, $end_m, 0, date('n',$date2), date('j',$date2), date('Y',$date2) );
            if ($date2>$date3) $date2=$date3;
            // calculate the number of seconds from current timestamp to the final timestamp
            $tmp = $date2-$date0;
            $seconds += $tmp;
         }

        // calculate the working days in seconds
        $seconds += 3600*($working_hours[1]-$working_hours[0])*$days;

        return $sign * $seconds;


 }  

But when I add this:

$query=mysql_query("SELECT * FROM table WHERE closed='yes'");
while($row=mysql_fetch_array($query)){

//The whole code above

}  

I want to do this to change it like this to get all rows together

$start = new DateTime($begintime);
to 
$start = new DateTime($row[begin_time]);  

It give a error:

Fatal error: Call to undefined function seconds2human() 

It only give this error when i place the { and the } above and after the code.
But I need it otherwise the $row values doesn't work.

The problem isn't the $row[begin_time] value because when I echo it, it works fine. Its just the { } thats buggin me frown.gif

Hope its something simple, but Im busy with it for last 8 hours and still doesn't work.

Any idea's to get it fixed?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.