I have the following array with different mixed dates from different years
Array
(
[0] => 2016-05-18
[1] => 2016-06-18
[2] => 2016-08-13
[3] => 2016-09-03
[4] => 2016-10-08
[5] => 2016-08-06
[6] => 2016-09-30
[7] => 2016-09-10
[8] => 2016-07-09
[9] => 2016-06-13
[10] => 2016-06-15
[11] => 2016-07-30
[12] => 2016-08-27
[13] => 2016-07-02
[14] => 2016-11-01
[15] => 2016-09-18
[16] => 2016-11-06
[17] => 2016-11-07
[18] => 2017-06-17
[19] => 2017-06-22
[20] => 2017-06-21
[21] => 2017-10-01
[22] => 2017-07-08
[23] => 2017-05-27
[24] => 2017-06-06
[25] => 2017-09-09
[26] => 2017-04-16
[27] => 2017-09-16
[28] => 2017-07-29
[29] => 2017-08-05
[30] => 2017-09-03
[31] => 2017-06-24
[32] => 2017-08-26
[33] => 2017-07-22
[34] => 2018-05-28
[35] => 2018-06-09
[36] => 2017-10-16
[37] => 2017-10-28
[38] => 2017-10-08
[39] => 2017-11-04
[40] => 2018-06-20
[41] => 2018-08-05
[42] => 2018-09-03
[43] => 2018-06-16
[44] => 2018-03-31
[45] => 2019-05-25
[46] => 2021-05-25
[47] => 2021-05-26
[48] => 2021-05-27
)
I want to remove all dates earlier than 2021 so my new array will have only the dates belonging to the current year of 2021. How can I filter the new array so all dates before 2021 will disappear?
Array
(
[0] => 2021-05-25
[1] => 2021-05-26
[2] => 2021-05-27
)
You can create a function that loops through the array and check if the string stars with 2021 if not, delete the element.
For php 8 you can use function:
str_starts_with(string $haystack, string $needle): bool
For example:
foreach($array as $key => $val){
if(!str_starts_with('2021', $val)){
unset($array[$key]);
}
}
For previous php versions you can use strpos
Example:
foreach($array as $key => $val){
if (!strpos($val, '2021') == 0) {
unset($array[$key]);
}
}
You have hashtag array-filter and PHP has array_filter function:
$dates = [
'2016-05-18',
'2018-03-31',
'2019-05-25',
'2021-05-25',
'2021-05-26',
'2021-05-27',
];
function myFilter($dt) {
$year = date('Y',strtotime($dt));
return $year > 2020;
}
print_r(array_values(array_filter($dates, 'myFilter')));
You can loop over each date and use the dateTime object to compare the times to see if the date has passed a set date like the begining of a year and if it hasn't then add it to the new array. See below code:
<?php
//array with mixed date data
$dateArray = array("2021-01-01","2016-05-18","2017-09-03","2018-06-16","2019-05-25","2021-05-25","2021-05-26","2021-05-27");
//the day you want to have everyday after (in this case the start of 2021)
$setDate = "2021-01-01";
//array where the dates greater than then the set date will be stored.
$newTimesArray = array();
$setDateObj = new \DateTime($setDate); // moved outside of loop
foreach ($dateArray as $date) {
$given_date = new \DateTime($date); //convert string to a date time object
if ($given_date >= $setDateObj) { //if the date is greater than or equal to "2021-01-01" add it to the new array
$newTimesArray[]=$date;
}
}
//print the results
echo print_r($newTimesArray,true);
?>
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>";
}
Now, I'm try to change date format from year-month-day to month/day/year form array.
I have an array that contains multiple dates in array i want to change each date format to year-month-day to month/day/year. Please check below array that contains multiple dates. Please help me if know something about that.
Array
(
[0] => 2018-05-19
[1] => 2018-05-20
[2] => 2018-05-21
[3] => 2018-05-22
[4] => 2018-05-23
[5] => 2018-05-24
[6] => 2018-05-25
[7] => 2018-05-26
[8] => 2018-05-27
[9] => 2018-05-28
[10] => 2018-05-29
[11] => 2018-05-30
[12] => 2018-05-31
[13] => 2018-06-04
[14] => 2018-06-05
[15] => 2018-06-08
[16] => 2018-06-09
[17] => 2018-06-10
[18] => 2018-06-13
[19] => 2018-06-14
[20] => 2018-06-15
[21] => 2018-06-17
[22] => 2018-06-20
[23] => 2018-06-21
[24] => 2018-06-24
[25] => 2018-06-28
[26] => 2018-06-29
[27] => 2018-07-02
[28] => 2018-07-05
[29] => 2018-07-11
[30] => 2018-07-12
[31] => 2018-07-15
[32] => 2018-07-16
[33] => 2018-07-17
[34] => 2018-07-20
[35] => 2018-07-23
[36] => 2018-07-24
[37] => 2018-07-25
[38] => 2018-07-26
[39] => 2018-07-27
[40] => 2018-07-29
[41] => 2018-08-07
[42] => 2018-08-08
[43] => 2018-08-10
[44] => 2018-08-11
[45] => 2018-08-12
[46] => 2018-08-13
[47] => 2018-08-15
[48] => 2018-08-17
[49] => 2018-08-18
[50] => 2018-08-19
[51] => 2018-08-20
[52] => 2018-08-21
[53] => 2018-08-25
[54] => 2018-08-26
[55] => 2018-08-27
[56] => 2018-08-28
[57] => 2018-08-29
[58] => 2018-08-30
)
I have an array that contains multiple dates in array i want to change each date format to year-month-day to month/day/year. Please check below array that contains multiple dates. Please help me if know something about that.
Here you go:
<?php
$currentDates = [
'2018-05-19',
'2018-05-20',
'2018-05-21',
'2018-05-22',
'2018-05-23',
'2018-05-24',
'2018-05-25',
'2018-05-26',
'2018-05-27',
'2018-05-28',
'2018-05-29',
'2018-05-30',
'2018-05-31',
'2018-06-04',
'2018-06-05',
'2018-06-08',
'2018-06-09',
'2018-06-10',
'2018-06-13',
'2018-06-14',
'2018-06-15',
'2018-06-17',
'2018-06-20',
'2018-06-21',
'2018-06-24',
'2018-06-28',
'2018-06-29',
'2018-07-02',
'2018-07-05',
'2018-07-11',
'2018-07-12',
'2018-07-15',
'2018-07-16',
'2018-07-17',
'2018-07-20',
'2018-07-23',
'2018-07-24',
'2018-07-25',
'2018-07-26',
'2018-07-27',
'2018-07-29',
'2018-08-07',
'2018-08-08',
'2018-08-10',
'2018-08-11',
'2018-08-12',
'2018-08-13',
'2018-08-15',
'2018-08-17',
'2018-08-18',
'2018-08-19',
'2018-08-20',
'2018-08-21',
'2018-08-25',
'2018-08-26',
'2018-08-27',
'2018-08-28',
'2018-08-29',
'2018-08-30',
];
$changedDates = array_map(function ($date) {
return date('m/d/Y', strtotime($date));
}, $currentDates);
var_dump($changedDates);
<?php
$dates = Array
("2018-05-19", "2018-05-20", "2018-05-21", "2018-05-22","2018-05-23");
print_r($dates);
$newDateFormat = array();
foreach($dates as $date){
$date = date('m/d/Y', strtotime($date));
array_push($newDateFormat,$date);
}
print_r($newDateFormat);
?>
I Think it will help you.
<?php
function convert_YYYYMMDD_to_MMDDYYYY($str)
{
return substr($str, 5, 2) . '/' . substr($str, 8, 2) . '/' . substr($str, 0, 4);
}
for ($i =0 ; $i < count($arr) ; $i++)
{
$arr[$i] = convert_YYYYMMDD_to_MMDDYYYY($arr[$i]);
}
?>
So I'm creating a calendar application to put in a website, It's supposed to:
Show current month
Show next (3) months
Show 2 previous months
show the end of previous and start of next month ( if layout lets you )(like windows calender also does.)
So right now i'm at the point where my code generates me a array with all data, it automaticly makes it start at the right position and (according to first day of the month) and it sets the default amount of days to 42. (as that's how windows has done it.)
This is the array output : https://pastebin.com/NqLzNW5Z
This is my Calendar.class.php file : https://pastebin.com/Hin8q7xW (some words are dutch, excuse me for this.)
My question: how do i change the following :
[7] => Array
(
[0] => Before Month Start
[1] => Before Month Start
[2] => Before Month Start
[3] => 01-06-2017
[4] => 02-06-2017
[5] => 03-06-2017
[6] => 04-06-2017
[7] => 05-06-2017
[8] => 06-06-2017
[9] => 07-06-2017
[10] => 08-06-2017
[11] => 09-06-2017
[12] => 10-06-2017
[13] => 11-06-2017
[14] => 12-06-2017
[15] => 13-06-2017
[16] => 14-06-2017
[17] => 15-06-2017
[18] => 16-06-2017
[19] => 17-06-2017
[20] => 18-06-2017
[21] => 19-06-2017
[22] => 20-06-2017
[23] => 21-06-2017
[24] => 22-06-2017
[25] => 23-06-2017
[26] => 24-06-2017
[27] => 25-06-2017
[28] => 26-06-2017
[29] => 27-06-2017
[30] => 28-06-2017
[31] => 29-06-2017
[32] => 30-06-2017
[33] => After month
[34] => After month
[35] => After month
[36] => After month
[37] => After month
[38] => After month
[39] => After month
[40] => After month
[41] => After month
)
To something like this :
[7] => Array
(
[0] => 29-05-2017
[1] => 30-05-2017
[2] => 31-05-2017
[3] => 01-06-2017
[4] => 02-06-2017
[5] => 03-06-2017
[6] => 04-06-2017
[7] => 05-06-2017
[8] => 06-06-2017
[9] => 07-06-2017
[10] => 08-06-2017
[11] => 09-06-2017
[12] => 10-06-2017
[13] => 11-06-2017
[14] => 12-06-2017
[15] => 13-06-2017
[16] => 14-06-2017
[17] => 15-06-2017
[18] => 16-06-2017
[19] => 17-06-2017
[20] => 18-06-2017
[21] => 19-06-2017
[22] => 20-06-2017
[23] => 21-06-2017
[24] => 22-06-2017
[25] => 23-06-2017
[26] => 24-06-2017
[27] => 25-06-2017
[28] => 26-06-2017
[29] => 27-06-2017
[30] => 28-06-2017
[31] => 29-06-2017
[32] => 30-06-2017
[33] => 01-07-2017
[34] => 02-07-2017
[35] => 03-07-2017
[36] => 04-07-2017
[37] => 05-07-2017
[38] => 06-07-2017
[39] => 07-07-2017
[40] => 08-07-2017
[41] => 09-07-2017
)
Note : The first and last month which is able to be seen doesn't have to show one month earlier and one month later. As this would require me to load in another month. just for those couple of days.
The end result will be put together with a slider. When the used clicks the next month arrow the slider wil display the next month.
If someone knows how to help me get this done, please do let me know. If someone knows a better approach to this, Also please do let me know!
thanks
This is untested, so let me know if you run into any problems.
public function makeMonth($m, $y)
{
$maand = array();
// Get info for this year
$info = $this->getInfo($m, $y);
// Get array with dates
$amount = $info['days'];
$dateStart = strtotime("{$y}-{$m}-1");
for ($i = 1; $i <= $amount; $i++) {
$maand[$i] = str_pad($i, 2, '0', STR_PAD_LEFT) . "-" . $m . "-" . $y;
$dateEnd = strtotime("{$y}-{$m}-{$i}");
}
// place the array content correctly
$needed = 42;
$begin = $info['firstday'] - 1;
$nognodig = $needed - ($begin + $amount);
#begin van array
while ($begin > 0) {
$begin--;
$dateStart = strtotime("-1 day", $dateStart);
array_unshift($maand, date("d-m-Y", $dateStart));
}
#eind van array
while ($nognodig > 0) {
$nognodig--;
$dateEnd = strtotime("+1 day", $dateEnd);
array_push($maand, date("d-m-Y", $dateEnd));
}
}
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
)