-1

Here i am doing task allocation per employee wise, almost i completed i have to do Hours calculation in project wise

    <?php
include("dbconfig.php");
include("functions.php");
$sql = "SELECT DISTINCT date_add(date(d_started_on), interval i day) as allocation_date,t_project,t_assign_to,t_start_time,t_end_time FROM ( SELECT date(STR_TO_DATE(t_started_on, '%Y-%m-%d')) as d_started_on, datediff( date(STR_TO_DATE(t_due_on, '%Y-%m-%d')), date(STR_TO_DATE(t_started_on, '%Y-%m-%d')) ) as days,t_project,t_assign_to,t_start_time,t_end_time FROM task ) as base INNER JOIN nums ON i <= days ORDER BY 1";
$mysql = mysql_query($sql);
$productsArray = array();
while($rowRes = mysql_fetch_assoc($mysql)){
    $rowRes['allocated_day'] = $rowRes['allocation_date'];
    $productsArray[] = $rowRes;
}
$project_name = array();
foreach($productsArray as $key0 => $info) {
    $key1 = $info['t_assign_to'];
    $key2 = $info['t_project'];
    $key3 = $info['allocated_day'];
    $project_name[$key1][$key2][$key3] = $info['t_start_time'].' '.$info['t_end_time']; 
}

print_r($project_name);
?>

OUTPUT

    Array
(
    [G2E0357] => Array
        (
            [10001] => Array
                (
                    [2018-01-01] => 01:30 PM 11:30 AM
                    [2018-01-02] => 01:30 PM 11:30 AM
                    [2018-01-03] => 01:30 PM 11:30 AM
                )

            [10008] => Array
                (
                    [2018-01-25] => 10:30 AM 01:30 PM
                    [2018-01-26] => 10:30 AM 01:30 PM
                    [2018-01-27] => 10:30 AM 01:30 PM
                )

        )

    [XPL0315] => Array
        (
            [10008] => Array
                (
                    [2018-01-18] => 11:30 AM 07:30 PM
                    [2018-01-19] => 11:30 AM 07:30 PM
                    [2018-01-20] => 11:30 AM 07:30 PM
                )

        )

)
  1. G2E0357 and XPL0315 these are employee id's
  2. 10001 and 10008 these are project id's
  3. 2018-01-01(project start date ) 2018-01-03 (project end date)
  4. 01:30 PM(start time) 11:30 AM(end time)

Explnation

employee id XPL0315 having one project allocation (10008) .timeline is 2018-01-18 11:30 AM INTO 2018-01-20 07:30 PM,

Instead of displaying t_start_time and t_end_time. i have to calculate per day how many Hours employee has to work

My Office timing is 9.30 AM into 7.30 PM (Totally 10 Hrs)

employee id XPL0315 and project id 10008 having 3 days allocation but working hours it will be different based allocation ,because

  1. 2018-01-18 date he is going to start 11.30 PM(7 Hrs Only)
  2. 2018-01-19 employee has to start the work 9.30 AM into 7.30 PM because allocation ending date 2018-01-18 07:30 PM (10 Hrs)
  3. 2018-01-20 also employee has to start the work 9.30 AM into 7.30 PM because allocation ending date 2018-01-18 07:30 PM (10 Hrs) suppose ending date is 2018-01-20 06:30 PM (9 Hrs)

Expected output should come like this

    Array
(
    [G2E0357] => Array
        (
            [10001] => Array
                (
                    [2018-01-01] => 6 Hrs
                    [2018-01-02] => 10 Hrs
                    [2018-01-03] => 2 Hrs
                )

            [10008] => Array
                (
                    [2018-01-25] => 9 Hrs
                    [2018-01-26] => 10 Hrs
                    [2018-01-27] => 4 Hrs
                )

        )

    [XPL0315] => Array
        (
            [10008] => Array
                (
                    [2018-01-18] => 7 Hrs
                    [2018-01-19] => 10 Hrs
                    [2018-01-20] => 10 Hrs
                )

        )

)

I think we have to take first index and last index,

  1. first index means we have to take t_start_time and do the calculation
  2. last index means we have to take t_end_time and do the calculation
Mithun M
  • 117
  • 2
  • 11
  • I've read the entire thing, and I'm not sure what your question is. Also, please reserve quoteblocks for quotes, that's what they're for; instead, use headings – William Perron Jan 22 '18 at 15:59
  • 1
    You want the number of hours between your time strings? – Kisaragi Jan 22 '18 at 15:59
  • 1
    https://stackoverflow.com/questions/3108591/calculate-number-of-hours-between-2-dates-in-php –  Jan 22 '18 at 15:59
  • Hello all ,in my case project id `10001` having 3 days allocation`2018-01-01 01:30 PM` into `2018-01-03 11:30 AM`, in this allocation i want to know per day how may hrs , i have work ? for that 1st day he should start work on 1:30 PM upto 7:30 Pm bcz my office timing already i given so first day (`1:30 PM into 7:30PM = 6 Hrs`) second day (`9:30 AM into 7:30 PM= 10 Hrs`) third day (`9:30 AM into 11:30 PM= 2 Hrs`) – Mithun M Jan 22 '18 at 16:12
  • @William Perron,are you getting my question ? – Mithun M Jan 22 '18 at 16:27
  • @MithunM There is still no question statement in your post. Please narrow down our question to a **specific** problem you are having in a **clear** and **concise** way – William Perron Jan 22 '18 at 16:53

1 Answers1

1

try this

    $project_name = array();
    foreach($productsArray as $key0 => $info) {
        $key1 = $info['t_assign_to'];
        $key2 = $info['t_project'];
        $key3 = $info['allocated_day'];
    /* Add this code */
    $start = new DateTime(date("H:i:s", strtotime($info['t_start_time']))); //'10:35:20'
    $end = new DateTime(date("H:i:s", strtotime($info['t_end_time'])));//'02:00:45'

    $diff = $start->diff( $end );
    $hrs = $diff->format( '%H'.' Hrs'  ); //'%H:%I:%S' => 02:25:25

    $project_name[$key1][$key2][$key3] = $hrs;
   /* Add this code */
    }
Prashant
  • 347
  • 1
  • 2
  • 12
  • Mr Prashant,i am not getting my expected results, i am getting **Array ( [G2E0357] => Array ( [10001] => Array ( [2018-01-01] => 02 Hrs [2018-01-02] => 02 Hrs [2018-01-03] => 02 Hrs ) [10008] => Array ( [2018-01-25] => 03 Hrs [2018-01-26] => 03 Hrs [2018-01-27] => 03 Hrs ) ) [XPL0315] => Array ( [10008] => Array ( [2018-01-18] => 08 Hrs [2018-01-19] => 08 Hrs [2018-01-20] => 08 Hrs ) ) )** – Mithun M Jan 22 '18 at 16:26
  • Hello Prashant ,in my case project id `10001` having 3 days allocation `2018-01-01 01:30 PM` into `2018-01-03 11:30 AM`, in this allocation i want to know per day how many hrs , i have to work ? for that 1st day he should start work on 11:30 PM upto 7:30 PM` bcz my office timing already i given so first day (`1:30 PM into 7:30PM = 6 Hrs`) second day (`9:30 AM into 7:30 PM= 10 Hrs`) third day (`9:30 AM into 11:30 PM= 2 Hrs`) – Mithun M Jan 22 '18 at 16:30
  • I tried lot Mr Prashant,Please update my code, i am not able to do i spent so much time for this – Mithun M Jan 22 '18 at 16:32
  • time formate in DateTime($info['t_start_time']); should be H:i:s data will be 11:00:00 then you will calculate perfect result, this format is wrong 11:00 PM, i had already write in comment. you can see that – Prashant Jan 22 '18 at 16:39
  • Task allocation time time format is `H:i:s A` i can't change this format , not possible to get the expected results from this format Mr @ Prashant – Mithun M Jan 22 '18 at 16:43
  • You telling 24 hrs format i have to make ?,can you please update your code include `print_r($project_name);` – Mithun M Jan 22 '18 at 16:46
  • To echo 16:30:00 from 4, 30 and PM, echo date("H:i:s", strtotime("$hour:$minute $period")); – Prashant Jan 23 '18 at 18:13
  • try this $start = new DateTime( date("H:i:s", strtotime($info['t_start_time']))); $end = new DateTime( date("H:i:s", strtotime($info['t_end_time']))); – Prashant Jan 23 '18 at 18:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/163740/discussion-between-prashant-and-mithun-m). – Prashant Jan 23 '18 at 18:23