-3

I have a lot of values like this:

+---------------------+-------+
|        Date         | value |
+---------------------+-------+
| 2015-05-22 13:10:59 | 23,99 |
| 2015-05-22 13:20:19 | 23,8  |
| 2015-05-26 17:30:00 | 8     |
| 2015-05-26 19:00:00 | 125   |
| 2015-05-27 05:00:00 | -73   |
| 2015-05-27 06:00:00 | 30    |
| 2015-05-27 15:00:00 | 32    |
| 2015-05-27 16:00:00 | 33    |
| 2015-05-28 10:45:00 | 3     |
| 2015-05-28 10:50:00 | 3     |
+---------------------+-------+

And given a certain period (2015-05-20 -> 2015-05-30), in order to include all the dates for that period in the list, I need to fill the remaining dates. For better explain, I want something like this:

+---------------------+-------+
|        Date         | value |
+---------------------+-------+
| 2015-05-20 00:00:00 | null  |
| 2015-05-21 00:00:00 | null  |
| 2015-05-22 00:00:00 | null  |
| 2015-05-22 13:10:59 | 23,99 |
| 2015-05-22 13:20:19 | 23,8  |
| 2015-05-23 00:00:00 | null  |
| 2015-05-24 00:00:00 | null  |
| 2015-05-25 00:00:00 | null  |
| 2015-05-26 00:00:00 | null  |
| 2015-05-26 17:30:00 | 8     |
| 2015-05-26 19:00:00 | 125   |
| 2015-05-27 00:00:00 | null  |
| 2015-05-27 05:00:00 | -73   |
| 2015-05-27 06:00:00 | 30    |
| 2015-05-27 15:00:00 | 32    |
| 2015-05-27 16:00:00 | 33    |
| 2015-05-28 00:00:00 | null  |
| 2015-05-28 10:45:00 | 3     |
| 2015-05-28 10:50:00 | 3     |
| 2015-05-29 00:00:00 | null  |
| 2015-05-30 00:00:00 | null  |
+---------------------+-------+

I know how to do this by code, but it has a lot of drawbacks, so I'm looking for a library to do so.

Any idea about any PHP library to work with stuff like this?

Gab
  • 9
  • 3
  • I don't get the question, you represent a database table so why not ask the database to return the dates using `BETWEEN()`? – Xorifelse Sep 23 '16 at 09:23
  • You have to tell us .. what's your situation. – Emz Sep 23 '16 at 09:24
  • I'm doing that already, but I need to list the whole period (all the dates), even when a specific date doesn't have a value. – Gab Sep 23 '16 at 09:25
  • @victor the situation is the first part...that is what I currently have. – Gab Sep 23 '16 at 09:26
  • @KatsuMoto ... You need to use SQL Query IF THESE RECORDS are in a Table of a Database... – Emz Sep 23 '16 at 09:29
  • @KatsuMoto ... I can give you answer if you can tell me where did this data from... – Emz Sep 23 '16 at 09:30
  • I'm getting the data from a MySQL Database, but I don't have the complete period dates, I have some scattered dates only, and I have to complete the rest. – Gab Sep 23 '16 at 09:32

1 Answers1

1

Ok i can't get you an Library, but it is really simple in php to do so, just use a function which gives you an Array with all the Dates:

function getDatesFromRange($start, $end){
$dates = array($start);
while(end($dates) < $end){
    $dates[] = date('Y-m-d', strtotime(end($dates).' +1 day'));
}
return $dates;
}

use the result as keys:

$dateArray = array_fill_keys(getDatesFromRange($start, $end), null);

And then use the result from your mysql query to set the real values. So thats less then 10 lines of code, i think a library would be Overkill.. And if you use the getDatesFromRange Function only for this purpose, you can even shorten it more...

Doktor OSwaldo
  • 5,732
  • 20
  • 41
  • Thanks for the reply. I know how to do it, what I was looking for was something like a library to make the process easier. – Gab Sep 23 '16 at 09:34