0

I need to get an array with days missing from a month.

Get year and month: to Between:

$dateS //have the current given date to make the filter

$year   = mb_substr($dateS, 0, 4);
$month  = mb_substr($dateS, 5, 7);
$tday   = cal_days_in_month(CAL_GREGORIAN, (int)$month, (int)$year); 
$cmonthS    = $year."-".$month."-01 00:00:00";
$cmonthE    = $year."-".$month."-".$tday." 23:59:59";

I am getting the array with this statement:

$stmtpre        =   "SELECT days FROM registry WHERE days BETWEEN '2016-12-01 00:00:00' AND '2016-12-31 00:00:00';";

All data have the current format as date time in MySQL:

XXXX-XX-XX 00:00:00

But it returns the current day, and I need to get in PHP the missing days.

Does anyone know how can i do it?

Sotiris Kiritsis
  • 3,178
  • 3
  • 23
  • 31
  • Sorry, I don't understand. Do you want to get the number of days between two dates? – Daniel Apr 15 '16 at 17:29
  • So you want to return every day in the month, even when a database row for that day does not exist? Are you wanting mysql to return those rows, or do you want to do it in php, filling in the gaps where mysql does not have a row? – Sean Apr 15 '16 at 17:32
  • @Sean I get mysql records; in an array. in array represent the days of the month with record (in datetime format: XXXX-XX-XX 00:00:00) I need is to take this array and have the reverse, the days that have no registration. if you see I have the month of the year to do this operation, but not function or operation that I implement an array with this information. –  Apr 15 '16 at 17:56
  • So if mysql only returns `2016-04-15 00:00:00`, you want in php to get an array of `2016-04-01 00:00:00`-`2016-04-14 00:00:00` & `2016-04-16 00:00:00`-`2016-04-30 00:00:00`? – Sean Apr 15 '16 at 17:59
  • yea but all days : 1,2,3... 14; except day 15, continue 16,17,18,19 in format datetime in and array.. have any idea? –  Apr 15 '16 at 18:01
  • here is a way to loop over a date range - http://stackoverflow.com/a/8755803/689579. You could then loop over those dates, checking if that value is in your mysql array. – Sean Apr 15 '16 at 18:07
  • ok but this: $period = new DatePeriod( $startDate, $periodInterval, $endDate ); which it is what returns ?? and array with all day in the mont ?? but i need day in the month not registry... –  Apr 15 '16 at 18:25

1 Answers1

0

i create and array with Total days of the month and other array with de Event day i run this function to get the diference into this 2 array:

http://php.net/manual/en/function.array-diff.php

and get finaly my No Event Array