find missing date and then include in array Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Find missing dates in range (php)PHP: Delete an element from an arrayHow do I get the current date and time in PHP?Get the first element of an arrayFinding the number of days between two datesDifference between require, include, require_once and include_once?How to Sort Multi-dimensional Array by Value?PHP array delete by value (not key)How can I find overlapping dateperiods/date ranges in PHP?PHP two dates array differenceHow i can compare 2 arrays with dates and get period of unique

How to find the right literary agent in the USA?

How to get even lighting when using flash for group photos near wall?

Additive group of local rings

Is a 5 watt UHF/VHF handheld considered QRP?

Need of separate security plugins for both root and subfolder sites Wordpress?

All ASCII characters with a given bit count

Can you stand up from being prone using Skirmisher outside of your turn?

Raising a bilingual kid. When should we introduce the majority language?

Could moose/elk survive in the Amazon forest?

Password Generator in batch

Why does the Cisco show run command not show the full version, while the show version command does?

Will I lose my paid in full property

Do I need to protect SFP ports and optics from dust/contaminants? If so, how?

A Paper Record is What I Hamper

My admission is revoked after accepting the admission offer

Married in secret, can marital status in passport be changed at a later date?

Implementing 3DES algorithm in Java: is my code secure?

What is it called when you ride around on your front wheel?

Is accepting an invalid credit card number a security issue?

Did the Roman Empire have penal colonies?

Justification for leaving new position after a short time

My bank got bought out, am I now going to have to start filing tax returns in a different state?

Is it acceptable to use working hours to read general interest books?

Has a Nobel Peace laureate ever been accused of war crimes?



find missing date and then include in array



Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar Manara
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Find missing dates in range (php)PHP: Delete an element from an arrayHow do I get the current date and time in PHP?Get the first element of an arrayFinding the number of days between two datesDifference between require, include, require_once and include_once?How to Sort Multi-dimensional Array by Value?PHP array delete by value (not key)How can I find overlapping dateperiods/date ranges in PHP?PHP two dates array differenceHow i can compare 2 arrays with dates and get period of unique



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-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
)









share|improve this question






















  • What is the purpose of date_create("2019-04-".date("t", mktime(0, 0, 0, 0, 1, 2019)));?

    – axiac
    Mar 9 at 7:35











  • It does not work as expected if the current month has 30 days. Check this out: 3v4l.org/B7dWM

    – axiac
    Mar 9 at 7:45











  • Do not mix DateTime with mktime(), date() and other old date & time functions. Use only DateTime and the related classes; they are easier to use and they handle the timezones properly (what the old date & time functions do not).

    – axiac
    Mar 9 at 7:49

















-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
)









share|improve this question






















  • What is the purpose of date_create("2019-04-".date("t", mktime(0, 0, 0, 0, 1, 2019)));?

    – axiac
    Mar 9 at 7:35











  • It does not work as expected if the current month has 30 days. Check this out: 3v4l.org/B7dWM

    – axiac
    Mar 9 at 7:45











  • Do not mix DateTime with mktime(), date() and other old date & time functions. Use only DateTime and the related classes; they are easier to use and they handle the timezones properly (what the old date & time functions do not).

    – axiac
    Mar 9 at 7:49













-1












-1








-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
)









share|improve this question














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
)






php






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 9 at 6:02









Agusto NiceAgusto Nice

33




33












  • What is the purpose of date_create("2019-04-".date("t", mktime(0, 0, 0, 0, 1, 2019)));?

    – axiac
    Mar 9 at 7:35











  • It does not work as expected if the current month has 30 days. Check this out: 3v4l.org/B7dWM

    – axiac
    Mar 9 at 7:45











  • Do not mix DateTime with mktime(), date() and other old date & time functions. Use only DateTime and the related classes; they are easier to use and they handle the timezones properly (what the old date & time functions do not).

    – axiac
    Mar 9 at 7:49

















  • What is the purpose of date_create("2019-04-".date("t", mktime(0, 0, 0, 0, 1, 2019)));?

    – axiac
    Mar 9 at 7:35











  • It does not work as expected if the current month has 30 days. Check this out: 3v4l.org/B7dWM

    – axiac
    Mar 9 at 7:45











  • Do not mix DateTime with mktime(), date() and other old date & time functions. Use only DateTime and the related classes; they are easier to use and they handle the timezones properly (what the old date & time functions do not).

    – axiac
    Mar 9 at 7:49
















What is the purpose of date_create("2019-04-".date("t", mktime(0, 0, 0, 0, 1, 2019)));?

– axiac
Mar 9 at 7:35





What is the purpose of date_create("2019-04-".date("t", mktime(0, 0, 0, 0, 1, 2019)));?

– axiac
Mar 9 at 7:35













It does not work as expected if the current month has 30 days. Check this out: 3v4l.org/B7dWM

– axiac
Mar 9 at 7:45





It does not work as expected if the current month has 30 days. Check this out: 3v4l.org/B7dWM

– axiac
Mar 9 at 7:45













Do not mix DateTime with mktime(), date() and other old date & time functions. Use only DateTime and the related classes; they are easier to use and they handle the timezones properly (what the old date & time functions do not).

– axiac
Mar 9 at 7:49





Do not mix DateTime with mktime(), date() and other old date & time functions. Use only DateTime and the related classes; they are easier to use and they handle the timezones properly (what the old date & time functions do not).

– axiac
Mar 9 at 7:49












1 Answer
1






active

oldest

votes


















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






share|improve this answer

























  • 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 9 at 16:19












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55074496%2ffind-missing-date-and-then-include-in-array%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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






share|improve this answer

























  • 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 9 at 16:19
















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






share|improve this answer

























  • 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 9 at 16:19














0












0








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






share|improve this answer















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







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 9 at 6:47

























answered Mar 9 at 6:12









ArtisticPhoenixArtisticPhoenix

18.7k11226




18.7k11226












  • 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 9 at 16:19


















  • 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 9 at 16:19

















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 9 at 16:19






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 9 at 16:19




















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55074496%2ffind-missing-date-and-then-include-in-array%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко