3

I have this array

$date = array{"2013-09-17 00:21:00",
              "2013-09-23 00:12:00",
              "2013-09-23 00:41:00",
              "2013-09-20 00:13:00",
              "2013-09-19 00:34:00",
              "2013-09-17 00:38:00"}

I'm trying to sum the time inside the array that have the same date.

This is my expected output:

 $date = array{"2013-09-17 00:59:00",
               "2013-09-23 00:53:00", 
               "2013-09-20 00:13:00",
               "2013-09-19 00:34:00"}

Now this is what I have tried so far

foreach($date as $key => $value)
        {
            $lf_date[] = date("Y-m-d",strtotime($value));
            $lf_time[] = date("H:i:s",strtotime($value));

            if(isset($lf_date[$key]))
            {
                 $output[] += $lf_time[$key]; 
            }
            else
            {
                 $output[] = $lf_time[$key]; 
            }

        }

This gives me a 0 output T_T.. I already tried searching on google and it says that I have to use isset and array_key_exists but I can't get it to work. :(. Thanks for anyone who could help me.

bot
  • 4,841
  • 3
  • 42
  • 67
  • Do not covert times to dates, but to seconds. – Voitcus Oct 07 '13 at 09:45
  • 1
    Simple question, what do you expect from `"2013-01-01 23:00:00" + "2013-01-01 23:00:00"`? Your data model is incorrect. You are adding dates and timespans. – Bart Friederichs Oct 07 '13 at 09:45
  • I think you want get only distinct value? – Md. Sahadat Hossain Oct 07 '13 at 09:47
  • Hi you need only date unique else time also required? – Nes Oct 07 '13 at 09:52
  • @Voitcus and Bart thanks for the formatting suggestions. I know I have to work on formatting the value for the computation. But I'm after for the condition, if how I'll be going to know if that date has a duplicate or already exists. then after that the formatting thing will now come for the computation. – bot Oct 08 '13 at 01:29
  • @Md.SahadatHossain yes I want to get only the distinct date and if it has duplicate, sum their time. – bot Oct 08 '13 at 01:30
  • @Nes yes I need unique date and if their is a duplicate, sum their time. – bot Oct 08 '13 at 01:31

1 Answers1

1

Use:

<?php
$date = array("2013-09-17 00:21:00",
              "2013-09-23 00:12:00",
              "2013-09-23 00:41:00",
              "2013-09-20 00:13:00",
              "2013-09-19 00:34:00",
              "2013-09-17 00:38:00");

$array = array();             
foreach($date as $key => $value)
{
    $lf_date = date("Y-m-d",strtotime($value));
    $lf_time = date("H:i:s",strtotime($value));

    $midnight = strtotime("0:00");

    if(!isset($array[$lf_date])) 
           $array[$lf_date] = 0;//check is array index exists

    $array[$lf_date] += strtotime($lf_time) - $midnight;
}

foreach($array as $key => $value)
{
    $midnight = strtotime("0:00");
    $array[$key] = $key." ".date("G:i:s", $midnight + $value);
}

$result = array_values($array);

print_r($result);

?>

Output:

Array
(
    [0] => 2013-09-17 0:59:00
    [1] => 2013-09-23 0:53:00
    [2] => 2013-09-20 0:13:00
    [3] => 2013-09-19 0:34:00
)
MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89