0

I'm trying to plot an array, but if one date is NULL, the value go to 1/1/1970.

I have the code:

    $date1 = $row['date_initial'];
    $date2 = $row['date_end'];
    $value = $row['value'];


    $data1 = array(strtotime($date1)*1000,$value);
    $data2 = array(strtotime($date2)*1000,$value);

    $data8[] = array($data1,$data2);

    echo json_encode($data8);

I get this array:

[[[1456531200000,"-12"],[1456704000000,"-12"]],[[1456531200000,"-16"],[0,"-16"]],[[1456617600000,"-13"],[1456790400000,"-13"]],[[1456704000000,"-14"],[0,"-14"]]]

It would be possible to change the null value date and to put the current date until the date is not empty? or remove this pair of array?

I proved:

    $data8 = array_map('array_filter', $data8);
    $data8 = array_filter($data8);  

but it doesn't work in this case...

1
  • Unset($array[0]); not sure it works though Commented May 8, 2016 at 8:30

2 Answers 2

3

Test it while init

if( ! ($data1 = strtotime($date1))) $data1 = time();
$data1 = array($date1*1000,$value);
Sign up to request clarification or add additional context in comments.

4 Comments

I saw it as a comment first, then it got removed and I was a bit surprised as it's a simple and good solution.
i was mistaken in my comment. The solution require two statements :)
Either way, it's still a solution worthy of an upvote
Glad to help. Good luck!
0

You could replace 0 timestamps with today's (mid-night) timestamp as follows:

$today = strtotime(date('Y-m-d')) * 1000; // convert to milliseconds
foreach($data8 as &$period) {
    if(!$period[0][0]) $period[0][0] = $today;
    if(!$period[1][0]) $period[1][0] = $today;
}

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.