how to get dates in reverse order in php? [duplicate] - php

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
)

Related

How to find and store in PHP array all the hours found inside of an SQL database `Datetime` column seperated by day?

First of all, I am using this piece of code in PHP to retrieve data from a database.
DATABASE DATA CODE:
// Database Details
$servername = "localhost";
$username = "admin_testdb";
$password = "123412345";
$dbname = "test_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM `HOURLY_DATA` WHERE 1 ORDER BY `HOURLY_DATA`.`TIME` ASC";
// Run $sql content as sql script in db.
$result=$conn->query($sql);
This code is just fine and has absolutely no errors or problems related to my goal here.
Also the specific data table I am looking after in this Query is the table named TIME which is of type "Datetime" format in SQL which looks like this:
DATETIME FORMAT:
YYYY-MM-DDTHH:MM:SS or to make it more clear an example would be 1970-09-24T05:00:00
Now moving onto what I am trying to achieve, my code and then the problem.
The Goal:
I have a list of data in the datetime format explained above, where theres HOURS missing, the increment of the HOURS is always +1 meaning that an example of missing data inside this data table would be:
2021-11-01T00:00:00
2021-11-01T01:00:00
2021-11-01T02:00:00
2021-11-01T05:00:00
2021-11-01T06:00:00
2021-11-01T07:00:00
2021-11-01T08:00:00
2021-11-01T13:00:00
2021-11-01T14:00:00
2021-11-01T15:00:00
2021-11-01T16:00:00
2021-11-01T17:00:00
2021-11-01T18:00:00
2021-11-01T19:00:00
2021-11-01T20:00:00
2021-11-01T21:00:00
So by taking a look at this DATETIME list above, we can understand that there is data missing and more specifically hours missing like 22:00:00 or 23:00:00 etc...
Now I want to write a code that will find this missing HOUR steps.
So far I have tried this code:
// Run $sql content as sql script in db.
$result=$conn->query($sql);
$flag = 0;
$i = 0;
$x = 0;
$cont = array();
$temp = array();
foreach ($result as $value) {
$singular_value = $value["TIME"];
$singular_value = str_replace(" ", "", $singular_value);
$day_value = $singular_value[8].$singular_value[9];
$day_value = (int) $day_value;
$singular_value = $singular_value[11].$singular_value[12];
$singular_value = (int) $singular_value;
// First time running.
if ( $flag == 0 ){
$day_chair = $day_value;
$flag = 1;
}
if ( $day_chair == $day_value ){
$temp[]= $value["TIME"];
$cont[$i]= $temp;
$day_chair = $day_value;
} else {
if ( $day_value == 30 || $day_value == 31 ){
$day_chair = 1;
continue;
} else {
$day_chair += 1;
$temp = array();
}
$i++;
}
}
print("<pre>".print_r($cont,true)."</pre>");
*PROBLEM:
To begin with this code returns the hour 00 which is considered to be the 12 o'clock midnight only for the first iteration or day.
Also the code above is supposed to return and array that will look like this:
Array
(
[0] => Array
(
[0] => 2021-11-01T00:00:00
[1] => 2021-11-01T01:00:00
[2] => 2021-11-01T02:00:00
[3] => 2021-11-01T03:00:00
[4] => 2021-11-01T04:00:00
[5] => 2021-11-01T05:00:00
[6] => 2021-11-01T06:00:00
[7] => 2021-11-01T07:00:00
[8] => 2021-11-01T08:00:00
[9] => 2021-11-01T09:00:00
[10] => 2021-11-01T10:00:00
[11] => 2021-11-01T11:00:00
[12] => 2021-11-01T12:00:00
[13] => 2021-11-01T13:00:00
[14] => 2021-11-01T14:00:00
[15] => 2021-11-01T15:00:00
[16] => 2021-11-01T16:00:00
[17] => 2021-11-01T17:00:00
[18] => 2021-11-01T18:00:00
[19] => 2021-11-01T19:00:00
[20] => 2021-11-01T20:00:00
[21] => 2021-11-01T21:00:00
[22] => 2021-11-01T22:00:00
[23] => 2021-11-01T23:00:00
)
[1] => Array
(
[0] => 2021-11-02T00:00:00
[1] => 2021-11-02T01:00:00
[2] => 2021-11-02T02:00:00
[3] => 2021-11-02T03:00:00
[4] => 2021-11-02T04:00:00
[5] => 2021-11-02T05:00:00
[6] => 2021-11-02T06:00:00
[7] => 2021-11-02T07:00:00
[8] => 2021-11-02T08:00:00
[9] => 2021-11-02T09:00:00
[10] => 2021-11-02T10:00:00
[11] => 2021-11-02T11:00:00
[12] => 2021-11-02T12:00:00
[13] => 2021-11-02T13:00:00
[14] => 2021-11-02T14:00:00
[15] => 2021-11-02T15:00:00
[16] => 2021-11-02T16:00:00
[17] => 2021-11-02T17:00:00
[18] => 2021-11-02T18:00:00
[19] => 2021-11-02T19:00:00
[20] => 2021-11-02T20:00:00
[21] => 2021-11-02T21:00:00
[22] => 2021-11-02T22:00:00
[23] => 2021-11-02T23:00:00
)
[2] => Array
(
[0] => 2021-11-03T00:00:00
[1] => 2021-11-03T01:00:00
[2] => 2021-11-03T02:00:00
[3] => 2021-11-03T03:00:00
[4] => 2021-11-03T04:00:00
[5] => 2021-11-03T05:00:00
[6] => 2021-11-03T06:00:00
[7] => 2021-11-03T07:00:00
[8] => 2021-11-03T08:00:00
[9] => 2021-11-03T09:00:00
[10] => 2021-11-03T10:00:00
[11] => 2021-11-03T11:00:00
[12] => 2021-11-03T12:00:00
[13] => 2021-11-03T13:00:00
[14] => 2021-11-03T14:00:00
[15] => 2021-11-03T15:00:00
[16] => 2021-11-03T16:00:00
[17] => 2021-11-03T17:00:00
[18] => 2021-11-03T18:00:00
[19] => 2021-11-03T19:00:00
[20] => 2021-11-03T20:00:00
[21] => 2021-11-03T21:00:00
[22] => 2021-11-03T22:00:00
[23] => 2021-11-03T23:00:00
)
)
And will leave a GAP or simply do write nothing in the array if the date is missing from the database. But this code return a lot of problematic arrays and just to give an example there are some here:
First day was parsed fine but the second one and all the days after the first day are missing the 12 o'clock aka 00:00:00
E.X:
[0] => Array
(
[0] => 2021-11-01T00:00:00
[1] => 2021-11-01T01:00:00
[2] => 2021-11-01T02:00:00
[3] => 2021-11-01T03:00:00
[4] => 2021-11-01T04:00:00
[5] => 2021-11-01T05:00:00
[6] => 2021-11-01T06:00:00
[7] => 2021-11-01T07:00:00
[8] => 2021-11-01T08:00:00
[9] => 2021-11-01T09:00:00
[10] => 2021-11-01T10:00:00
[11] => 2021-11-01T11:00:00
[12] => 2021-11-01T12:00:00
[13] => 2021-11-01T13:00:00
[14] => 2021-11-01T14:00:00
[15] => 2021-11-01T15:00:00
[16] => 2021-11-01T16:00:00
[17] => 2021-11-01T17:00:00
[18] => 2021-11-01T18:00:00
[19] => 2021-11-01T19:00:00
[20] => 2021-11-01T20:00:00
[21] => 2021-11-01T21:00:00
[22] => 2021-11-01T22:00:00
[23] => 2021-11-01T23:00:00
)
[1] => Array
(
[0] => 2021-11-02T01:00:00
[1] => 2021-11-02T02:00:00
[2] => 2021-11-02T03:00:00
[3] => 2021-11-02T04:00:00
[4] => 2021-11-02T05:00:00
[5] => 2021-11-02T06:00:00
[6] => 2021-11-02T07:00:00
[7] => 2021-11-02T08:00:00
[8] => 2021-11-02T09:00:00
[9] => 2021-11-02T10:00:00
[10] => 2021-11-02T11:00:00
[11] => 2021-11-02T12:00:00
[12] => 2021-11-02T13:00:00
[13] => 2021-11-02T14:00:00
[14] => 2021-11-02T15:00:00
[15] => 2021-11-02T16:00:00
[16] => 2021-11-02T17:00:00
[17] => 2021-11-02T18:00:00
[18] => 2021-11-02T19:00:00
[19] => 2021-11-02T20:00:00
[20] => 2021-11-02T21:00:00
[21] => 2021-11-02T22:00:00
[22] => 2021-11-02T23:00:00
)
Also, there are broken days for example when the day reaches day 30 or 31 of the month this happens:
[28] => Array
(
[0] => 2021-11-29T01:00:00
[1] => 2021-11-29T02:00:00
[2] => 2021-11-29T03:00:00
[3] => 2021-11-29T04:00:00
[4] => 2021-11-29T05:00:00
[5] => 2021-11-29T06:00:00
[6] => 2021-11-29T07:00:00
[7] => 2021-11-29T08:00:00
[8] => 2021-11-29T09:00:00
[9] => 2021-11-29T10:00:00
[10] => 2021-11-29T11:00:00
[11] => 2021-11-29T12:00:00
[12] => 2021-11-29T13:00:00
[13] => 2021-11-29T14:00:00
[14] => 2021-11-29T15:00:00
[15] => 2021-11-29T16:00:00
[16] => 2021-11-29T17:00:00
[17] => 2021-11-29T18:00:00
[18] => 2021-11-29T19:00:00
[19] => 2021-11-29T20:00:00
[20] => 2021-11-29T21:00:00
[21] => 2021-11-29T22:00:00
[22] => 2021-11-29T23:00:00
[23] => 2021-12-01T00:00:00
[24] => 2021-12-01T01:00:00
[25] => 2021-12-01T02:00:00
[26] => 2021-12-01T03:00:00
[27] => 2021-12-01T04:00:00
[28] => 2021-12-01T05:00:00
[29] => 2021-12-01T06:00:00
[30] => 2021-12-01T07:00:00
[31] => 2021-12-01T08:00:00
[32] => 2021-12-01T09:00:00
[33] => 2021-12-01T10:00:00
[34] => 2021-12-01T11:00:00
[35] => 2021-12-01T12:00:00
[36] => 2021-12-01T13:00:00
[37] => 2021-12-01T14:00:00
[38] => 2021-12-01T15:00:00
[39] => 2021-12-01T16:00:00
[40] => 2021-12-01T17:00:00
[41] => 2021-12-01T18:00:00
[42] => 2021-12-01T19:00:00
[43] => 2021-12-01T20:00:00
[44] => 2021-12-01T21:00:00
[45] => 2021-12-01T22:00:00
[46] => 2021-12-01T23:00:00
)
[29] => Array
(
[0] => 2021-12-02T01:00:00
[1] => 2021-12-02T02:00:00
[2] => 2021-12-02T03:00:00
[3] => 2021-12-02T04:00:00
[4] => 2021-12-02T05:00:00
[5] => 2021-12-02T06:00:00
[6] => 2021-12-02T07:00:00
[7] => 2021-12-02T08:00:00
[8] => 2021-12-02T09:00:00
[9] => 2021-12-02T10:00:00
[10] => 2021-12-02T11:00:00
[11] => 2021-12-02T12:00:00
[12] => 2021-12-02T13:00:00
[13] => 2021-12-02T14:00:00
[14] => 2021-12-02T15:00:00
[15] => 2021-12-02T16:00:00
[16] => 2021-12-02T17:00:00
[17] => 2021-12-02T18:00:00
[18] => 2021-12-02T19:00:00
[19] => 2021-12-02T20:00:00
[20] => 2021-12-02T21:00:00
[21] => 2021-12-02T22:00:00
[22] => 2021-12-02T23:00:00
)
QUESTION:
What can I do to solve this programmatic/algorithmic problem or what am I doing wrong in my code?

Skip the week range from months range in PHP

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>";
}

find missing date and then include in array

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

calendar array previous next month

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

Cut array in php

I want to cut array in php. My array is listed below :
Array
(
[0] => 6/1/2014
[1] => 6/2/2014
[2] => 6/3/2014
[3] => 6/4/2014
[4] => 6/5/2014
[5] => 6/6/2014
[6] => 6/7/2014
[7] => 6/8/2014
[8] => 6/9/2014
[9] => 6/10/2014
[10] => 6/11/2014
[11] => 6/12/2014
[12] => 6/13/2014
[13] => 6/14/2014
[14] => 6/15/2014
[15] => 6/16/2014
[16] => 6/17/2014
[17] => 6/18/2014
[18] => 6/19/2014
[19] => 6/20/2014
[20] => 6/21/2014
[21] => 6/22/2014
[22] => 6/23/2014
[23] => 6/24/2014
[24] => 6/25/2014
[25] => 6/26/2014
[26] => 6/27/2014
[27] => 6/28/2014
[28] => 6/29/2014
[29] => 6/30/2014
[30] => 7/1/2014
[31] => 7/2/2014
[32] => 7/3/2014
[33] => 7/4/2014
[34] => 7/5/2014
[35] => 7/6/2014
[36] => 7/7/2014
[37] => 7/8/2014
[38] => 7/9/2014
[39] => 7/10/2014
[40] => 7/11/2014
[41] => 7/12/2014
[42] => 7/13/2014
[43] => 7/14/2014
[44] => 7/15/2014
[45] => 7/16/2014
[46] => 7/17/2014
[47] => 7/18/2014
[48] => 7/19/2014
[49] => 7/20/2014
[50] => 7/21/2014
[51] => 7/22/2014
[52] => 7/23/2014
[53] => 7/24/2014
[54] => 7/25/2014
[55] => 7/26/2014
[56] => 7/27/2014
[57] => 7/28/2014
[58] => 7/29/2014
[59] => 7/30/2014
[60] => 7/31/2014
[61] => 8/1/2014
)
In this array 0 to 29 elements if for 6th Month, 30th to 60th elements are for 7th Month etc..
Now i want this array in the below fashion
Array
(
[0] => 6/1/2014
[1] => 6/2/2014
[2] => 6/3/2014
[3] => 6/4/2014
[4] => 6/5/2014
[5] => 6/6/2014
[6] => 6/7/2014
[7] => 6/8/2014
[8] => 6/9/2014
[9] => 6/10/2014
[10] => 6/11/2014
[11] => 6/12/2014
[12] => 6/13/2014
[13] => 6/14/2014
[14] => 6/15/2014
[15] => 6/16/2014
[16] => 6/17/2014
[17] => 6/18/2014
[18] => 6/19/2014
[19] => 6/20/2014
[20] => 6/21/2014
[21] => 6/22/2014
[22] => 6/23/2014
[23] => 6/24/2014
[24] => 6/25/2014
[25] => 6/26/2014
[26] => 6/27/2014
[27] => 6/28/2014
[28] => 6/29/2014
[29] => 6/30/2014
)
Array
(
[0] => 7/1/2014
[1] => 7/2/2014
[2] => 7/3/2014
[3] => 7/4/2014
[4] => 7/5/2014
[5] => 7/6/2014
[6] => 7/7/2014
[7] => 7/8/2014
[8] => 7/9/2014
[9] => 7/10/2014
[10] => 7/11/2014
[11] => 7/12/2014
[12] => 7/13/2014
[13] => 7/14/2014
[14] => 7/15/2014
[15] => 7/16/2014
[16] => 7/17/2014
[17] => 7/18/2014
[18] => 7/19/2014
[19] => 7/20/2014
[20] => 7/21/2014
[21] => 7/22/2014
[22] => 7/23/2014
[23] => 7/24/2014
[24] => 7/25/2014
[25] => 7/26/2014
[26] => 7/27/2014
[27] => 7/28/2014
[28] => 7/29/2014
[29] => 7/30/2014
[30] => 7/31/2014
)
Array
(
[0] => 8/1/2014
)
This calculation should be in a way that if i chose other months then it also do the same process of cutting array for different months.
you can also separate array by month & and put them in main array
$arrres =array();
foreach($arr as $value)
{
$arrres[str_replace('/','',substr($value,0,2))][] = $value ;
}
print_r($arrres);
you can use this:, it will also generate arrays with same num of days in months (28,30,31)
//generate dates for test
$dates = [];
for($i = 0 ; $i < 90 ; $i++){
$dates[] = Date("d/m/Y",time()-($i*24*3600));
}
//make results, also validates that it's same month and year
$result = [];
foreach($dates as $date){
$dStr = Date("m-y",strtotime(str_replace('/', '-', $date)));
if(!isset($result[$dStr])) $result[$dStr] = [];
$result[$dStr][] = $date;
}
var_dump($result);
I'd explode the value of every index in your array and set it as an index to the new array.
$days = [];
foreach($your_array as $day) {
$days[ explode("/", $day)[0] ][] = $day;
}
var_dump( $days );

Categories