-1

how to add $mydates (2019-04-01", "2019-04-08) include too in array ?

Give some suggestion please.

thanks

I referrer from here , but only find missing date Find missing dates in range (php)

$myDates = array("2019-04-01", "2019-04-08");
$missingDates = array();

$dateStart = date_create("2019-04-01");
$dateEnd   = date_create("2019-04-".date("t", mktime(0, 0, 0, 0, 1, 2019)));
$interval  = new DateInterval('P1D');
$period    = new DatePeriod($dateStart, $interval, $dateEnd);
foreach($period as $day) {
  $formatted = $day->format("Y-m-d");
  if(!in_array($formatted, $myDates)) $missingDates[] = $formatted;
}

echo '<pre>';print_r($missingDates);echo '</pre>';

result

Array
(
    [0] => 2019-04-02
    [1] => 2019-04-03
    [2] => 2019-04-04
    [3] => 2019-04-05
    [4] => 2019-04-06
    [5] => 2019-04-07
    [6] => 2019-04-09
    [7] => 2019-04-10
    [8] => 2019-04-11
    [9] => 2019-04-12
    [10] => 2019-04-13
    [11] => 2019-04-14
    [12] => 2019-04-15
    [13] => 2019-04-16
    [14] => 2019-04-17
    [15] => 2019-04-18
    [16] => 2019-04-19
    [17] => 2019-04-20
    [18] => 2019-04-21
    [19] => 2019-04-22
    [20] => 2019-04-23
    [21] => 2019-04-24
    [22] => 2019-04-25
    [23] => 2019-04-26
    [24] => 2019-04-27
    [25] => 2019-04-28
    [26] => 2019-04-29
    [27] => 2019-04-30
)
  • What is the purpose of `date_create("2019-04-".date("t", mktime(0, 0, 0, 0, 1, 2019)));`? – axiac Mar 09 '19 at 07:35
  • It does not work as expected if the current month has 30 days. Check this out: https://3v4l.org/B7dWM – axiac Mar 09 '19 at 07:45
  • Do not mix `DateTime` with `mktime()`, `date()` and other old [date & time functions](http://php.net/manual/en/ref.datetime.php). Use only [`DateTime` and the related classes](http://php.net/manual/en/book.datetime.php); they are easier to use and they handle the timezones properly (what the old date & time functions do not). – axiac Mar 09 '19 at 07:49

1 Answers1

0

It's not clear exactly what you want as the output, but maybe some of these will give you some Ideas.

Like this for your code:

$missingDates = array("2019-04-01", "2019-04-08");

$dateStart = date_create("2019-04-01");
$dateEnd   = date_create("2019-04-".date("t", mktime(0, 0, 0, 0, 1, 2019)));
$interval  = new DateInterval('P1D');
$period    = new DatePeriod($dateStart, $interval, $dateEnd);
foreach($period as $day) {
  $formatted = $day->format("Y-m-d");
  if(!in_array($formatted, $missingDates)) $missingDates[] = $formatted;
}

sort($missingDates);

echo '<pre>';print_r($missingDates);echo '</pre>';

Output

Array
(
    [0] => 2019-04-01
    [1] => 2019-04-02
    [2] => 2019-04-03
    [3] => 2019-04-04
    [4] => 2019-04-05
    [5] => 2019-04-06
    [6] => 2019-04-07
    [7] => 2019-04-08
    [8] => 2019-04-09
    [9] => 2019-04-10
    [10] => 2019-04-11
    [11] => 2019-04-12
    [12] => 2019-04-13
    [13] => 2019-04-14
    [14] => 2019-04-15
    [15] => 2019-04-16
    [16] => 2019-04-17
    [17] => 2019-04-18
    [18] => 2019-04-19
    [19] => 2019-04-20
    [20] => 2019-04-21
    [21] => 2019-04-22
    [22] => 2019-04-23
    [23] => 2019-04-24
    [24] => 2019-04-25
    [25] => 2019-04-26
    [26] => 2019-04-27
    [27] => 2019-04-28
    [28] => 2019-04-29
    [29] => 2019-04-30
)

Sandbox

One note here is this 2019 is probably going to cause you some issues in 2020

$dateEnd   = date_create("2019-04-".date("t", mktime(0, 0, 0, 0, 1, 2019)));

You can't really fix this, because by the time you do it's preferable to use a date time for that, as I do below. You'll wind up turning the end date into a date time object, so you can get the year (new DateTime("2019-04-08"))->format('Y') at which point you might as well just use one of the options below. You cannot simply use the $dateStart object because the $dateEnd could be in next year depending on what you actually want.

All the days of this month

The above basically just gives you all the days of the month which you could do this way:

function getDaysOfMonth($date){
    $dateStart = (new DateTime($date))->modify('first day of this month'); 
    $dateEnd = (new DateTime($date))->modify('first day of next month');
    $interval  = new DateInterval('P1D');
    $period    = new DatePeriod($dateStart, $interval, $dateEnd);
    $formatted = [];

    foreach($period as $day) $formatted[] = $day->format("Y-m-d");
    return $formatted;
}

print_r(getDaysOfMonth('2019-04-10'));

Output

Array
(
    [0] => 2019-04-01
    [1] => 2019-04-02
    [2] => 2019-04-03
    [3] => 2019-04-04
    [4] => 2019-04-05
    [5] => 2019-04-06
    [6] => 2019-04-07
    [7] => 2019-04-08
    [8] => 2019-04-09
    [9] => 2019-04-10
    [10] => 2019-04-11
    [11] => 2019-04-12
    [12] => 2019-04-13
    [13] => 2019-04-14
    [14] => 2019-04-15
    [15] => 2019-04-16
    [16] => 2019-04-17
    [17] => 2019-04-18
    [18] => 2019-04-19
    [19] => 2019-04-20
    [20] => 2019-04-21
    [21] => 2019-04-22
    [22] => 2019-04-23
    [23] => 2019-04-24
    [24] => 2019-04-25
    [25] => 2019-04-26
    [26] => 2019-04-27
    [27] => 2019-04-28
    [28] => 2019-04-29
    [29] => 2019-04-30
)

Sandbox

Remaining days of the month

Just change this line in the above:

 $dateStart = (new DateTime($date))->modify('first day of this month'); 

To

 $dateStart = new DateTime($date); 

Output

Array
(
    [0] => 2019-04-10
    [1] => 2019-04-11
    [2] => 2019-04-12
    [3] => 2019-04-13
    [4] => 2019-04-14
    [5] => 2019-04-15
    [6] => 2019-04-16
    [7] => 2019-04-17
    [8] => 2019-04-18
    [9] => 2019-04-19
    [10] => 2019-04-20
    [11] => 2019-04-21
    [12] => 2019-04-22
    [13] => 2019-04-23
    [14] => 2019-04-24
    [15] => 2019-04-25
    [16] => 2019-04-26
    [17] => 2019-04-27
    [18] => 2019-04-28
    [19] => 2019-04-29
    [20] => 2019-04-30
)

Sandbox

Start to End (inclusive)

This gives you all the days, but if you only want from your first to your second you can use this:

function getDays($dateStart,$dateEnd){

    $dateStart = new DateTime($dateStart); 
    $dateEnd = (new DateTime($dateEnd))->modify('+1 day'); 
    $interval  = new DateInterval('P1D');
    $period    = new DatePeriod($dateStart, $interval, $dateEnd);
    $formatted = [];

    foreach($period as $day) $formatted[] = $day->format("Y-m-d");



    return $formatted;
}

print_r(getDays('2019-04-01', '2019-04-08'));

Output

Array
(
    [0] => 2019-04-01
    [1] => 2019-04-02
    [2] => 2019-04-03
    [3] => 2019-04-04
    [4] => 2019-04-05
    [5] => 2019-04-06
    [6] => 2019-04-07
    [7] => 2019-04-08
)

Sandbox

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
  • thank you for the input, which I want in the first point, I want to get the missing date in the specified month based on the dropdown menu. – Agusto Nice Mar 09 '19 at 16:19