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
)
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
Related
I just want to skip the given week from a date range(month range).
For example, I have a date range between 2020-04-01 to 2020-04-30. I just have to skip the alternate week from this date range which is 2020-04-08 to 2020-04-14 and 2020-04-22 to 2020-04-28. So my final output will be
[0] => 2020-04-01
[1] => 2020-04-02
[2] => 2020-04-03
[3] => 2020-04-04
[4] => 2020-04-05
[5] => 2020-04-06
[6] => 2020-04-07
[7] => 2020-04-15
[8] => 2020-04-16
[9] => 2020-04-17
[10] => 2020-04-18
[11] => 2020-04-19
[12] => 2020-04-20
[13] => 2020-04-21
[14] => 2020-04-29
[15] => 2020-04-30
These skipping should be dynamic like skip every second week, third week and so on...
I hope you understand what I want to say
Maybe something like this:
You only need to pass in date for first of month and array of weeks to exclude.
function excludeWeeks($dateStart, $excludeWeeks) {
$finalDates = [];
$date = new DateTime($dateStart);
$daysInMonth = date("t", strtotime($dateStart));;
$firstOfMonth = strtotime(date("Y-m-01", strtotime($date->format('Y-m-d'))));
$modifiedDate = $date->format('Y-m-d');
for ($i = 0; $i <= $daysInMonth; $i++) {
if($i > 0) {
$modifiedDate = $date->modify('+ 1 day')->format('Y-m-d');
}
$weekNumber = intval(date("W", strtotime($modifiedDate))) - intval(date("W", $firstOfMonth)) + 1;
if(!in_array($weekNumber, $excludeWeeks)) {
$finalDates[] = $modifiedDate;
}
}
print_r($finalDates);
}
excludeWeeks('2020-04-1', ["3"]);
Will print our:
Array ( [0] => 2020-04-01 [1] => 2020-04-02 [2] => 2020-04-03 [3] => 2020-04-04 [4] => 2020-04-05 [5] => 2020-04-06 [6] => 2020-04-07 [7] => 2020-04-08 [8] => 2020-04-09 [9] => 2020-04-10 [10] => 2020-04-11 [11] => 2020-04-12 [12] => 2020-04-20 [13] => 2020-04-21 [14] => 2020-04-22 [15] => 2020-04-23 [16] => 2020-04-24 [17] => 2020-04-25 [18] => 2020-04-26 [19] => 2020-04-27 [20] => 2020-04-28 [21] => 2020-04-29 [22] => 2020-04-30 [23] => 2020-05-01 )
Based on your comment, this is what you can do:
function excludeWeeks($dateStart, $dateEnd, $excludeWeeks) {
$totalDays = strtotime($dateEnd) - strtotime($dateStart);
$totalDays = round($totalDays / (60 * 60 * 24));
$finalDates = [];
$dateStart = new DateTime($dateStart);
$modifiedDate = $dateStart->format('Y-m-d');
for ($i = 0; $i < $totalDays; $i++) {
if($i > 0) {
$modifiedDate = $dateStart->modify('+ 1 day')->format('Y-m-d');
}
$weekNumber = (int)$i/7 + 1;
if(!in_array( (int)$weekNumber, $excludeWeeks)) {
$finalDates[] = $modifiedDate;
}
}
print("<pre>".print_r($finalDates,true)."</pre>");
}
excludeWeeks('2020-04-1','2020-06-18', ["2","4","6","8","10"]);
This will exclude all weeks (not accoring to calendar, but as you said, 7 days) and print something like:
Array
(
[0] => 2020-04-01
[1] => 2020-04-02
[2] => 2020-04-03
[3] => 2020-04-04
[4] => 2020-04-05
[5] => 2020-04-06
[6] => 2020-04-07
[7] => 2020-04-15
[8] => 2020-04-16
[9] => 2020-04-17
[10] => 2020-04-18
[11] => 2020-04-19
[12] => 2020-04-20
[13] => 2020-04-21
[14] => 2020-04-29
[15] => 2020-04-30
[16] => 2020-05-01
[17] => 2020-05-02
[18] => 2020-05-03
[19] => 2020-05-04
[20] => 2020-05-05
[21] => 2020-05-13
[22] => 2020-05-14
[23] => 2020-05-15
[24] => 2020-05-16
[25] => 2020-05-17
[26] => 2020-05-18
[27] => 2020-05-19
[28] => 2020-05-27
[29] => 2020-05-28
[30] => 2020-05-29
[31] => 2020-05-30
[32] => 2020-05-31
[33] => 2020-06-01
[34] => 2020-06-02
[35] => 2020-06-10
[36] => 2020-06-11
[37] => 2020-06-12
[38] => 2020-06-13
[39] => 2020-06-14
[40] => 2020-06-15
[41] => 2020-06-16
[42] => 2020-06-17
)
You can use whatever startTime and endTime.
The starting parameters are at beggining of code
$startData = "2020-04-01";
$endData = "2020-04-30";
$skipData = "2020-04-08"; // skip one week starting from this data
$everyNumberWeeks = 1;
$dateTimestampStartData = strtotime($startData);
$dateTimestampEndData = strtotime($endData);
$dataTimestampSkipData = strtotime($skipData);
$data = $startData;
$dateTimestampData = strtotime($data);
$skiping = 0;$show=true;
while ($dateTimestampData <= $dateTimestampEndData)
{
$data = date("Y-m-d", $dateTimestampData);
$dateTimestampData = strtotime($data) + 60*60*24;
if ($dateTimestampData > $dataTimestampSkipData)
{
if (($skiping % (7*$everyNumberWeeks))==0)
{
$show = ! $show;
$skiping = 0;
}
$skiping++;
}
if ($show) echo "$data<BR>";
}
I have an array of dates
$dates = array("2019-04-09","2019-04-17","2019-04-18","2019-04-23","2019-04-24","2019-04-26","2019-04-29","2019-04-30");
what i would like is to pass the $date array through a function and that function would group consecutive working days together to produce the following format.
$newarray = array(
[0]=>array('start' => '2019-04-09','end' => '2019-04-09'),
[1]=>array('start' => '2019-04-17','end' => '2019-04-24'),
[2]=>array('start' => '2019-04-26','end' => '2019-04-30'),
);
Working days are non weekend and non uk bank holidays. I have bank holidays already in another array)
$bankholidays = array(
[0] => 2018-01-01
[1] => 2018-03-30
[2] => 2018-04-02
[3] => 2018-05-07
[4] => 2018-05-28
[5] => 2018-08-27
[6] => 2018-12-25
[7] => 2018-12-26
[8] => 2019-01-01
[9] => 2019-04-19
[10] => 2019-04-22
[11] => 2019-05-06
[12] => 2019-05-27
[13] => 2019-08-26
[14] => 2019-12-25
[15] => 2019-12-26
[16] => 2020-01-01
[17] => 2020-04-10
[18] => 2020-04-13
[19] => 2020-05-04
[20] => 2020-05-25
[21] => 2020-08-31
[22] => 2020-12-25
[23] => 2020-12-28
)
the following function gives me date ranges in the format i require but doesn't take into consideration weekends and bank holidays
function genarray($dates) {
$i=0;
$j=1;
$diff=86400;
$period = $diff;
$nrInterval=0;
$intervals[$nrInterval]['start'] = $dates[$i];
$intervals[$nrInterval]['end'] = $dates[$i];
while($j<count($dates)){
if(strtotime($dates[$j])-strtotime($dates[$i]) == $period){
$intervals[$nrInterval]['end'] = $dates[$j];
$j++;
$period+=$diff;
} else{
$i=$j;
$j++;
$nrInterval++;
$intervals[$nrInterval]['start'] = $dates[$i];
$intervals[$nrInterval]['end'] = $dates[$i];
$period = $diff;
}
}
return $intervals;
}
$dates = array("2019-04-09","2019-04-17","2019-04-18","2019-04-19","2019-04-23","2019-04-24","2019-04-26","2019-04-29","2019-04-30");
$tmp = genarray($dates);
print_r($tmp);
gives me
Array
(
[0] => Array
(
[start] => 2019-04-09
[end] => 2019-04-09
)
[1] => Array
(
[start] => 2019-04-17
[end] => 2019-04-19
)
[2] => Array
(
[start] => 2019-04-23
[end] => 2019-04-24
)
[3] => Array
(
[start] => 2019-04-26
[end] => 2019-04-26
)
[4] => Array
(
[start] => 2019-04-29
[end] => 2019-04-30
)
)
Can anyone help please? I just cant get my head around where to go from here.
How can I get month Interval in php using DateInterval.
If I chose 05 Aug 2015 as start date and 17 Oct 2015 as end date, then my array output should be divided in 3 arrays:
1st array for 05 Aug 2015 - 31 Aug 2015
2nd array for 01 Sept 2015 - 30 Sept 2015
3rd array for 01 Oct 2015 - 17 Oct 2015
We can achieve this thing by php DateInterval function?
you can try the below code. I am not sure but you might get what you are looking for
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($d1, $interval, $d2);
foreach ($period as $dt) {
if($dt->format("Y-m") == date("Y-m", strtotime($date1))){
$date_array[$dt->format("Y-m")] = array(
'start_date' => $startDate,
'end_date' => $dt->format("Y-m-t"),
);
}else if($dt->format("Y-m") == date("Y-m", strtotime($date2))){
$date_array[$dt->format("Y-m")] = array(
'start_date' => $dt->format("Y-m-").'01',
'end_date' => $endDate,
);
}else{
$date_array[$dt->format("Y-m")] = array(
'start_date' => $dt->format("Y-m-").'01',
'end_date' => $dt->format("Y-m-t"),
);
}
}
foreach( $date_array as $d_key=>$da ){
/* your stuff */
}
This is pretty straightforward if you familiarise yourself with some of PHP's helpful built-in Date/Time classes.
You can use DateTime, DateInterval and DatePeriod to achieve your results. For example:
<?php
// create start/end dates and interval
$start = new DateTime('05 Aug 2015');
$end = new DateTime('17 Oct 2015');
$interval = new DateInterval('P1D');
// DatePeriod excludes the last day
// so bump the end date by one
$end->modify('+1 day');
// create a DatePeriod for each day in range
$period = new DatePeriod($start, $interval, $end);
$months = [];
foreach ($period as $date) {
$months[$date->format('F')][] = $date->format('m/d/Y');
}
print_r($months);
Yields:
Array
(
[August] => Array
(
[0] => 08/05/2015
[1] => 08/06/2015
[2] => 08/07/2015
[3] => 08/08/2015
[4] => 08/09/2015
[5] => 08/10/2015
[6] => 08/11/2015
[7] => 08/12/2015
[8] => 08/13/2015
[9] => 08/14/2015
[10] => 08/15/2015
[11] => 08/16/2015
[12] => 08/17/2015
[13] => 08/18/2015
[14] => 08/19/2015
[15] => 08/20/2015
[16] => 08/21/2015
[17] => 08/22/2015
[18] => 08/23/2015
[19] => 08/24/2015
[20] => 08/25/2015
[21] => 08/26/2015
[22] => 08/27/2015
[23] => 08/28/2015
[24] => 08/29/2015
[25] => 08/30/2015
[26] => 08/31/2015
)
[September] => Array
(
[0] => 09/01/2015
[1] => 09/02/2015
[2] => 09/03/2015
[3] => 09/04/2015
[4] => 09/05/2015
[5] => 09/06/2015
[6] => 09/07/2015
[7] => 09/08/2015
[8] => 09/09/2015
[9] => 09/10/2015
[10] => 09/11/2015
[11] => 09/12/2015
[12] => 09/13/2015
[13] => 09/14/2015
[14] => 09/15/2015
[15] => 09/16/2015
[16] => 09/17/2015
[17] => 09/18/2015
[18] => 09/19/2015
[19] => 09/20/2015
[20] => 09/21/2015
[21] => 09/22/2015
[22] => 09/23/2015
[23] => 09/24/2015
[24] => 09/25/2015
[25] => 09/26/2015
[26] => 09/27/2015
[27] => 09/28/2015
[28] => 09/29/2015
[29] => 09/30/2015
)
[October] => Array
(
[0] => 10/01/2015
[1] => 10/02/2015
[2] => 10/03/2015
[3] => 10/04/2015
[4] => 10/05/2015
[5] => 10/06/2015
[6] => 10/07/2015
[7] => 10/08/2015
[8] => 10/09/2015
[9] => 10/10/2015
[10] => 10/11/2015
[11] => 10/12/2015
[12] => 10/13/2015
[13] => 10/14/2015
[14] => 10/15/2015
[15] => 10/16/2015
[16] => 10/17/2015
)
)
Just to note: you specified that you wanted to break the result into three separate arrays. I assume the results will be dynamic - in which the number of months could vary - so I think you are better generating a multidimensional array of results.
In the example above I used the month names as keys. $date->format('F') returns a full textual representation of a month, such as January or March.
If you want to get a list of the months contained in the $months array you can just do:
print_r(array_keys($months));
Which returns the keys, and should give you enough information to access the data for your needs:
Array
(
[0] => August
[1] => September
[2] => October
)
Hope this helps :)
This question already has answers here:
Reverse date order in script
(6 answers)
Closed 9 years ago.
I build a php page in which i want to get dates in reverse order how i do this?
Here is my code:
$dates = array();
$timestamp = time();
for ($i = 0 ; $i <=30 ; $i++) {
$dates[$i]= date('m-d-Y', $timestamp);
$timestamp -= 24 * 3600;
}
and here is my output
Array ( [0] => 02-01-2014 [1] => 01-31-2014 [2] => 01-30-2014 [3] => 01-29-2014 [4] => 01-28-2014 [5] => 01-27-2014 [6] => 01-26-2014 [7] => 01-25-2014 [8] => 01-24-2014 [9] => 01-23-2014 [10] => 01-22-2014 [11] => 01-21-2014 [12] => 01-20-2014 [13] => 01-19-2014 [14] => 01-18-2014 [15] => 01-17-2014 [16] => 01-16-2014 [17] => 01-15-2014 [18] => 01-14-2014 [19] => 01-13-2014 [20] => 01-12-2014 [21] => 01-11-2014 [22] => 01-10-2014 [23] => 01-09-2014 [24] => 01-08-2014 [25] => 01-07-2014 [26] => 01-06-2014 [27] => 01-05-2014 [28] => 01-04-2014 [29] => 01-03-2014 [30] => 01-02-2014 )
How to store dates in reverse order?
Reverse you can use strtotime function
<?php
$dates = array();
//get the last day and go from that day
$timestamp = strtotime('-30 days');
for ($i = 0 ; $i <=30 ; $i++) {
//insert the date
$dates[$i]= date('m-d-Y', $timestamp);
//increase the day
$timestamp += 24 * 3600;
}
//display the output
print_r($dates);
Array
(
[0] => 01-02-2014
[1] => 01-03-2014
[2] => 01-04-2014
[3] => 01-05-2014
[4] => 01-06-2014
[5] => 01-07-2014
[6] => 01-08-2014
[7] => 01-09-2014
[8] => 01-10-2014
[9] => 01-11-2014
[10] => 01-12-2014
[11] => 01-13-2014
[12] => 01-14-2014
[13] => 01-15-2014
[14] => 01-16-2014
[15] => 01-17-2014
[16] => 01-18-2014
[17] => 01-19-2014
[18] => 01-20-2014
[19] => 01-21-2014
[20] => 01-22-2014
[21] => 01-23-2014
[22] => 01-24-2014
[23] => 01-25-2014
[24] => 01-26-2014
[25] => 01-27-2014
[26] => 01-28-2014
[27] => 01-29-2014
[28] => 01-30-2014
[29] => 01-31-2014
[30] => 02-01-2014
)
I would like to identify the missing dates in a PHP array
for example
this range
2013-06-12
2013-06-13
2013-06-26
2013-06-27
2013-06-29
has the following dates missing:
2013-06-14
2013-06-15
2013-06-16
...
2013-06-24
2013-06-25
2013-06-28
How can I identify the missing dates?
Here's a good way to get all missing dates in an array:
<?php
$myDates = array("2013-06-12", "2013-06-13", "2013-06-26", "2013-06-27", "2013-06-29");
$missingDates = array();
$dateStart = date_create("2013-06-01");
$dateEnd = date_create("2013-06-".date("t", mktime(0, 0, 0, 6, 1, 2013)));
$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;
}
print_r($missingDates);
?>
This will result in:
Array
(
[0] => 2013-06-01
[1] => 2013-06-02
[2] => 2013-06-03
[3] => 2013-06-04
[4] => 2013-06-05
[5] => 2013-06-06
[6] => 2013-06-07
[7] => 2013-06-08
[8] => 2013-06-09
[9] => 2013-06-10
[10] => 2013-06-11
[11] => 2013-06-14
[12] => 2013-06-15
[13] => 2013-06-16
[14] => 2013-06-17
[15] => 2013-06-18
[16] => 2013-06-19
[17] => 2013-06-20
[18] => 2013-06-21
[19] => 2013-06-22
[20] => 2013-06-23
[21] => 2013-06-24
[22] => 2013-06-25
[23] => 2013-06-28
[24] => 2013-06-30
)