0

i have case where there are a timestamp date contains of date format. Then i wanted to build a chart that show the number of "clicked" items "per day" ,

//array declaration
$array1 = array("Date" => 0);
$array2 = array("Date" => 0);
$array3 = array("Date" => 0);
$array4 = array("Date" => 0);
$array5 = array("Date" => 0);
$array6 = array("Date" => 0);
$array7 = array("Date" => 0);
$array8 = array("Date" => 0);
//var_dump($array);

foreach ($_sql as $result1) {
    $timestamp = $result1['timestamp'];
    $itemType = $result1['itemType'];

    //separate the time and date
    $explodeTime = explode(" ", $timestamp);
    $date = $explodeTime[0];
    //$arrDate = array($date);

    //var_dump($Date);

    //if the item type is 1;
    if($itemType == 1 ){



        //check the existence
        if(array_key_exists($date,$array1)){

            //if exist increment the click by 1

            foreach ($array1 as $key => $value) {

                $array1[$key]=$value + 1;


                //var_dump($array1);


        }           
            }


                //else add new record and set default value as 1
                else{ 



                    //echo "Insert new Key";

                    //$array1 = array($date => 1);
                    //var_dump($array1);


                    //var_dump($array1);
                    //exit();


                }
    }

i wanted to get my array result with this format

array(1) {
["2009-04-17"]=> 211
int(1)
}
array(1) {
["2009-04-18"]=> 1213
int(1)
}
array(1) {
["2009-04-19"]=> 1232
int(1)
}
array(1) {
["2009-04-20"]=> 32312
int(1)
}

so i can get the value of date, then it is easily to convert the data into json, then insert into the Chartjs.

sorry if my question is not clear, because i have just started learn php.

2
  • What is your problem exactly ? Is there any error message ? Commented Aug 9, 2016 at 7:41
  • Are you getting information from a database initially? Commented Aug 9, 2016 at 7:42

2 Answers 2

1

I think this is what you are trying to do:

//array declaration
$array = array();

foreach ($_sql as $result1) {
    //separate the time and date
    $dateTime = new DateTime($result1['timestamp']);
    $date = $dateTime->format('Y-m-d');

    //if the item type is 1;
    if (1 == $result1['itemType']) {
        //check the existence
        if (array_key_exists($date, $array)) {
            //if exist increment the click by 1
            $array[$date]++;
        } else {
            $array[$date] = 1;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

What do you need exactly? if you need to convert timestamp to date, use format function.

$timestamp=$result1['timestamp'];
$timestamp=new DateTime($timestamp);
$date=$timestamp->format("n.j.Y");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.