Echo all months with days calendar - php

I am using the code below to echo the current month. How can i enhance it so that is shows all the months with the names and days and dates..
Code:
<?php
$today = getdate();
$firstDay = getdate(mktime(0,0,0,$today['mon'],1,$today['year']));
$lastDay = getdate(mktime(0,0,0,$today['mon']+1,0,$today['year']));
?>
<?php
echo '<table>';
echo ' <tr><th colspan="7">'.$today['month']." - ".$today['year']."</th></tr>";
echo '<tr class="days">';
echo ' <td>Mo</td><td>Tu</td><td>We</td><td>Th</td>';
echo ' <td>Fr</td><td>Sa</td><td>Su</td></tr>';
?>
<?php
echo '<tr>';
for($i=1;$i<$firstDay['wday'];$i++){
echo '<td> </td>';
}
$actday = 0;
for($i=$firstDay['wday'];$i<=7;$i++){
$actday++;
echo "<td>$actday</td>";
}
echo '</tr>';
?>
<?php
$fullWeeks = floor(($lastDay['mday']-$actday)/7);
for ($i=0;$i<$fullWeeks;$i++){
echo '<tr>';
for ($j=0;$j<7;$j++){
$actday++;
echo "<td>$actday</td>";
}
echo '</tr>';
}
?>
<?php
if ($actday < $lastDay['mday']){
echo '<tr>';
for ($i=0; $i<7;$i++){
$actday++;
if ($actday <= $lastDay['mday']){
echo "<td>$actday</td>";
}
else {
echo '<td> </td>';
}
}
echo '</tr>';
}
?>

Try this:
function getDates($year)
{
$dates = array();
date("L", mktime(0,0,0, 7,7, $year)) ? $days = 366 : $days = 365;
for($i = 1; $i <= $days; $i++){
$month = date('m', mktime(0,0,0,1,$i,$year));
$wk = date('W', mktime(0,0,0,1,$i,$year));
$wkDay = date('D', mktime(0,0,0,1,$i,$year));
$day = date('d', mktime(0,0,0,1,$i,$year));
$dates[$month][$wk][$wkDay] = $day;
}
return $dates;
}
it will return an array of months->weeks->day->weekday of the year you pass to the function. Hopefully it should be easy to traverse through the array to print everything out. Am sure there are a lot of tweaks you can make to that but its a start.
I would also try and stay away from printing out html using echo, for example instead of;
echo '<tr>';
for($i=1;$i<$firstDay['wday'];$i++){
echo '<td> </td>';
}
do;
<tr>;
<?php for($i=1;$i<$firstDay['wday'];$i++){ ?>
<td><?php echo $var; ?></td>
<?php } ?>
It kind of makes the code more readable I think.
EDIT: Just thought I should include an example of a use case as well, as below:
<?php $dates = getDates(2011);
$weekdays = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'); ?>
<?php foreach($dates as $month => $weeks) { ?>
<table>
<tr>
<th><?php echo implode('</th><th>', $weekdays); ?></th>
</tr>
<?php foreach($weeks as $week => $days){ ?>
<tr>
<?php foreach($weekdays as $day){ ?>
<td>
<?php echo isset($days[$day]) ? $days[$day] : '&nbsp'; ?>
</td>
<?php } ?>
</tr>
<?php } ?>
</table>
<?php } ?>
Which gives you the output:

You can use this function to convert entire year into array
function year2array($year) {
$res = $year >= 1970;
if ($res) {
// this line gets and sets same timezone, don't ask why :)
date_default_timezone_set(date_default_timezone_get());
$dt = strtotime("-1 day", strtotime("$year-01-01 00:00:00"));
$res = array();
$week = array_fill(1, 7, false);
$last_month = 1;
$w = 1;
do {
$dt = strtotime('+1 day', $dt);
$dta = getdate($dt);
$wday = $dta['wday'] == 0 ? 7 : $dta['wday'];
if (($dta['mon'] != $last_month) || ($wday == 1)) {
if ($week[1] || $week[7]) $res[$last_month][] = $week;
$week = array_fill(1, 7, false);
$last_month = $dta['mon'];
}
$week[$wday] = $dta['mday'];
}
while ($dta['year'] == $year);
}
return $res;
}
Call it like
print_r(year2array(2011));
You'll see this in source (months->weeks->days):
Array
(
[1] => Array
(
[0] => Array
(
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] => 1
[7] => 2
)
[1] => Array
(
[1] => 3
[2] => 4
[3] => 5
[4] => 6
[5] => 7
[6] => 8
[7] => 9
)
[2] => Array
(
[1] => 10
[2] => 11
[3] => 12
[4] => 13
[5] => 14
[6] => 15
[7] => 16
)
[3] => Array
(
[1] => 17
[2] => 18
[3] => 19
[4] => 20
[5] => 21
[6] => 22
[7] => 23
)
[4] => Array
(
[1] => 24
[2] => 25
[3] => 26
[4] => 27
[5] => 28
[6] => 29
[7] => 30
)
[5] => Array
(
[1] => 31
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
)
)
[2] => Array
(
[0] => Array
(
[1] =>
[2] => 1
[3] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
)
[1] => Array
(
[1] => 7
[2] => 8
[3] => 9
[4] => 10
[5] => 11
[6] => 12
[7] => 13
)
[2] => Array
(
[1] => 14
[2] => 15
[3] => 16
[4] => 17
[5] => 18
[6] => 19
[7] => 20
)
[3] => Array
(
[1] => 21
[2] => 22
[3] => 23
[4] => 24
[5] => 25
[6] => 26
[7] => 27
)
[4] => Array
(
[1] => 28
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
)
)
[3] => Array
(
[0] => Array
(
[1] =>
[2] => 1
[3] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
)
[1] => Array
(
[1] => 7
[2] => 8
[3] => 9
[4] => 10
[5] => 11
[6] => 12
[7] => 13
)
[2] => Array
(
[1] => 14
[2] => 15
[3] => 16
[4] => 17
[5] => 18
[6] => 19
[7] => 20
)
[3] => Array
(
[1] => 21
[2] => 22
[3] => 23
[4] => 24
[5] => 25
[6] => 26
[7] => 27
)
[4] => Array
(
[1] => 28
[2] => 29
[3] => 30
[4] => 31
[5] =>
[6] =>
[7] =>
)
)
[4] => Array
(
[0] => Array
(
[1] =>
[2] =>
[3] =>
[4] =>
[5] => 1
[6] => 2
[7] => 3
)
[1] => Array
(
[1] => 4
[2] => 5
[3] => 6
[4] => 7
[5] => 8
[6] => 9
[7] => 10
)
[2] => Array
(
[1] => 11
[2] => 12
[3] => 13
[4] => 14
[5] => 15
[6] => 16
[7] => 17
)
[3] => Array
(
[1] => 18
[2] => 19
[3] => 20
[4] => 21
[5] => 22
[6] => 23
[7] => 24
)
[4] => Array
(
[1] => 25
[2] => 26
[3] => 27
[4] => 28
[5] => 29
[6] => 30
[7] =>
)
)
[5] => Array
(
[0] => Array
(
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] => 1
)
[1] => Array
(
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
)
[2] => Array
(
[1] => 9
[2] => 10
[3] => 11
[4] => 12
[5] => 13
[6] => 14
[7] => 15
)
[3] => Array
(
[1] => 16
[2] => 17
[3] => 18
[4] => 19
[5] => 20
[6] => 21
[7] => 22
)
[4] => Array
(
[1] => 23
[2] => 24
[3] => 25
[4] => 26
[5] => 27
[6] => 28
[7] => 29
)
[5] => Array
(
[1] => 30
[2] => 31
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
)
)
[6] => Array
(
[0] => Array
(
[1] =>
[2] =>
[3] => 1
[4] => 2
[5] => 3
[6] => 4
[7] => 5
)
[1] => Array
(
[1] => 6
[2] => 7
[3] => 8
[4] => 9
[5] => 10
[6] => 11
[7] => 12
)
[2] => Array
(
[1] => 13
[2] => 14
[3] => 15
[4] => 16
[5] => 17
[6] => 18
[7] => 19
)
[3] => Array
(
[1] => 20
[2] => 21
[3] => 22
[4] => 23
[5] => 24
[6] => 25
[7] => 26
)
[4] => Array
(
[1] => 27
[2] => 28
[3] => 29
[4] => 30
[5] =>
[6] =>
[7] =>
)
)
[7] => Array
(
[0] => Array
(
[1] =>
[2] =>
[3] =>
[4] =>
[5] => 1
[6] => 2
[7] => 3
)
[1] => Array
(
[1] => 4
[2] => 5
[3] => 6
[4] => 7
[5] => 8
[6] => 9
[7] => 10
)
[2] => Array
(
[1] => 11
[2] => 12
[3] => 13
[4] => 14
[5] => 15
[6] => 16
[7] => 17
)
[3] => Array
(
[1] => 18
[2] => 19
[3] => 20
[4] => 21
[5] => 22
[6] => 23
[7] => 24
)
[4] => Array
(
[1] => 25
[2] => 26
[3] => 27
[4] => 28
[5] => 29
[6] => 30
[7] => 31
)
)
[8] => Array
(
[0] => Array
(
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
[7] => 7
)
[1] => Array
(
[1] => 8
[2] => 9
[3] => 10
[4] => 11
[5] => 12
[6] => 13
[7] => 14
)
[2] => Array
(
[1] => 15
[2] => 16
[3] => 17
[4] => 18
[5] => 19
[6] => 20
[7] => 21
)
[3] => Array
(
[1] => 22
[2] => 23
[3] => 24
[4] => 25
[5] => 26
[6] => 27
[7] => 28
)
[4] => Array
(
[1] => 29
[2] => 30
[3] => 31
[4] =>
[5] =>
[6] =>
[7] =>
)
)
[9] => Array
(
[0] => Array
(
[1] =>
[2] =>
[3] =>
[4] => 1
[5] => 2
[6] => 3
[7] => 4
)
[1] => Array
(
[1] => 5
[2] => 6
[3] => 7
[4] => 8
[5] => 9
[6] => 10
[7] => 11
)
[2] => Array
(
[1] => 12
[2] => 13
[3] => 14
[4] => 15
[5] => 16
[6] => 17
[7] => 18
)
[3] => Array
(
[1] => 19
[2] => 20
[3] => 21
[4] => 22
[5] => 23
[6] => 24
[7] => 25
)
[4] => Array
(
[1] => 26
[2] => 27
[3] => 28
[4] => 29
[5] => 30
[6] =>
[7] =>
)
)
[10] => Array
(
[0] => Array
(
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] => 1
[7] => 2
)
[1] => Array
(
[1] => 3
[2] => 4
[3] => 5
[4] => 6
[5] => 7
[6] => 8
[7] => 9
)
[2] => Array
(
[1] => 10
[2] => 11
[3] => 12
[4] => 13
[5] => 14
[6] => 15
[7] => 16
)
[3] => Array
(
[1] => 17
[2] => 18
[3] => 19
[4] => 20
[5] => 21
[6] => 22
[7] => 23
)
[4] => Array
(
[1] => 24
[2] => 25
[3] => 26
[4] => 27
[5] => 28
[6] => 29
[7] => 30
)
[5] => Array
(
[1] => 31
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
)
)
[11] => Array
(
[0] => Array
(
[1] =>
[2] => 1
[3] => 2
[4] => 3
[5] => 4
[6] => 5
[7] => 6
)
[1] => Array
(
[1] => 7
[2] => 8
[3] => 9
[4] => 10
[5] => 11
[6] => 12
[7] => 13
)
[2] => Array
(
[1] => 14
[2] => 15
[3] => 16
[4] => 17
[5] => 18
[6] => 19
[7] => 20
)
[3] => Array
(
[1] => 21
[2] => 22
[3] => 23
[4] => 24
[5] => 25
[6] => 26
[7] => 27
)
[4] => Array
(
[1] => 28
[2] => 29
[3] => 30
[4] =>
[5] =>
[6] =>
[7] =>
)
)
[12] => Array
(
[0] => Array
(
[1] =>
[2] =>
[3] =>
[4] => 1
[5] => 2
[6] => 3
[7] => 4
)
[1] => Array
(
[1] => 5
[2] => 6
[3] => 7
[4] => 8
[5] => 9
[6] => 10
[7] => 11
)
[2] => Array
(
[1] => 12
[2] => 13
[3] => 14
[4] => 15
[5] => 16
[6] => 17
[7] => 18
)
[3] => Array
(
[1] => 19
[2] => 20
[3] => 21
[4] => 22
[5] => 23
[6] => 24
[7] => 25
)
[4] => Array
(
[1] => 26
[2] => 27
[3] => 28
[4] => 29
[5] => 30
[6] => 31
[7] =>
)
)
)
So, now it's easy to create month table for every month you need using something like this
function month2table($month, $calendar_array) {
$ca = 'align="center"';
$res = "<table cellpadding=\"2\" cellspacing=\"1\" style=\"border:solid 1px #000000;font-family:tahoma;font-size:12px;background-color:#ababab\"><tr><td $ca>Mo</td><td $ca>Tu</td><td $ca>We</td><td $ca>Th</td><td $ca>Fr</td><td $ca>Sa</td><td $ca>Su</td></tr>";
foreach ($calendar_array[$month] as $month=>$week) {
$res .= '<tr>';
foreach ($week as $day) {
$res .= '<td align="right" width="20" bgcolor="#ffffff">' . ($day ? $day : ' ') . '</td>';
}
$res .= '</tr>';
}
$res .= '</table>';
return $res;
}
Use these functions like
$calarr = year2array(2011);
echo month2table(1, $calarr); // January
echo month2table(2, $calarr); // February
...
echo month2table(12, $calarr); // December
..or put months in for loop.
So... e.g. for January 2011 in your browser you'll see this
Hope this helps.

$daysArr = array('Mon','Tue','Wed','Thu','Fri','Sat','Sun');
$monthtotdays= cal_days_in_month(CAL_GREGORIAN,date('m'),date("Y"));
$currdays=jddayofweek (cal_to_jd(CAL_GREGORIAN, date('m'),1, date("Y")) , 2 );
$currdaysval = 0;
echo "<table border=1px>";
echo "<tr>";
for($d=0;$d<=6;$d++){
echo "<td>". $daysArr[$d]."</td>";
if($daysArr[$d]==$currdays) $currdaysval = $d;
}
echo "</tr>";
echo "<tr>";
if($currdaysval > 0 ){
echo '<td colspan="'.$currdaysval.'"> </td>';
}
for($i=1;$i<=$monthtotdays;$i++){
echo "<td>".$i."</td>";
if(($i+$currdaysval )%7 <= 0 ){
echo "</tr><tr>";
}
}
echo "</tr></table>"

The best answer has an inaccuracy! You should add a condition if year is leap-year BEFORE a cycle, otherwise you will have problems with output of first days of first month see screenshot.
The necessary condition: date("L", mktime(0,0,0, 7,7, $year)) ? $days = 366 : $days = 365;
(if year is leap-year then counter of cycle = 366 else 365)

Wrap your current code within a function then pass an argument to it with your desired date.

Related

PHP: How to create a 2d array that counts up through the inner arrays

my code creates this:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
)
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
)
[2] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
)
[3] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
)
)
but I want this
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
)
[1] => Array
(
[0] => 11
[1] => 12
[2] => 13
[3] => 14
[4] => 15
[5] => 16
[6] => 17
[7] => 18
[8] => 19
[9] => 20
)
[2] => Array
(
[0] => 21
[1] => 22
[2] => 23
[3] => 24
[4] => 25
[5] => 26
[6] => 27
[7] => 28
[8] => 29
[9] => 30
)
[3] => Array
(
[0] => 31
[1] => 32
[2] => 33
[3] => 34
[4] => 35
[5] => 36
[6] => 37
[7] => 38
[8] => 39
[9] => 40
)
)
This is my code:
<?php
require 'vendor/autoload.php';
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
$loader = new FilesystemLoader(__DIR__ . '/templates');
$twig = new Environment($loader);
$rowCount =4;
$columnCount = 10;
$grid = array();
$row = array();
$column = array();
for ($n=1; $n<=$columnCount;$n++){
array_push($row, $n);
}
for($i=1; $i<=$rowCount; $i++){
array_push($grid, $row);
}
echo "<pre>";
print_r($grid);
echo "<pre>";
echo $twig->render('view.html', array(
'grid' => $grid,
))
?>
I think I have to put one for loop in the other, i just did it this way for now so you can understand the problem better.
PS: The view.html which is rendered is empty, i want to give it the 2d array later so it creates a grid out of the array with an id for each cell.
If the question is stupid im sorry, but be sure i tried it for myself long enough and it didnt work out.
With friendly regards,
Strambo
On each row you should to fill column. And then put (array_push()) row in the grid. When you filling the column you should increase the column counter (value in your cell).
$rowCount = 4;
$columnCount = 10;
$grid = [];
$currentCollumn = 1;
for($i = 1; $i <= $rowCount; $i++){
$row = [];
for($j = 0; $j < $columnCount; $j++) {
$row[$j] = $currentCollumn;
$currentCollumn++;
}
array_push($grid, $row);
}
echo "<pre>";
print_r($grid);
echo "<pre>";
P.S.: There are no stupid questions, it's ok question :)
The main issue you can see is that your row is getting created one time -- in that first loop. Then you're taking the same copy of the row and pushing it onto the grid rowCount of times.
So yes, two main ideas here:
Any time you have some kind of "grid" or "matrix" problem you're probably looking to use a nested loop, as you suspected
You don't want to re-initialize your $currentValue (the number you're placing in every point in the grid) every time -- you only want to initialize it once, and then have it maintain a single incrementing count throughout the grid -- not resetting itself, since you want sequential numbers all the way through
Last tip here is that when you start to get muddled or confused dealing with loops, it helps to rename your counter values from $i/$n to something more descriptive to help you keep track of what is happening where
That gives us this code:
<?php
$rowCount = 4;
$columnCount = 10;
$grid = array();
$row = array();
$column = array();
$currentValue = 1; // we start at 1 and want to count to 40, not re-initialize this in the loop
for ($currentRow = 1; $currentRow <= $rowCount; $currentRow++) {
$row = array();
// first section in for loop is empty since we already initialized $currentValue outside the loops
for (; $currentValue <= ($columnCount * $currentRow); $currentValue++) {
array_push($row, $currentValue);
}
array_push($grid, $row);
}
print_r($grid);
Which will print
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
)
[1] => Array
(
[0] => 11
[1] => 12
[2] => 13
[3] => 14
[4] => 15
[5] => 16
[6] => 17
[7] => 18
[8] => 19
[9] => 20
)
[2] => Array
(
[0] => 21
[1] => 22
[2] => 23
[3] => 24
[4] => 25
[5] => 26
[6] => 27
[7] => 28
[8] => 29
[9] => 30
)
[3] => Array
(
[0] => 31
[1] => 32
[2] => 33
[3] => 34
[4] => 35
[5] => 36
[6] => 37
[7] => 38
[8] => 39
[9] => 40
)
)

multidimensional array sort based on database column value compare

I have following multidimensional array
Array
(
[1] => 22
[2] => 12
[3] => Array
(
[0] => 4
[1] => 21
)
[5] => Array
(
[0] => 9
[1] => 3
)
[7] => 7
[8] => 6
[9] => 1
[10] => 2
[11] => 10
[12] => 0
[13] => 23
[14] => 18
[15] => 16
[16] => 19
[17] => 15
[18] => 5
[19] => 8
[20] => 11
[21] => 13
[22] => 17
[23] => 14
[24] => 20
)
I want to sort morethan one value below array from main array.
Array
(
[3] => Array
(
[0] => 4
[1] => 21
)
[5] => Array
(
[0] => 9
[1] => 3
)
)
Each key value compare with database column and sort an array. If not found value then array keep as it is.
For example:
The below key value is 21 have in database column then am expected results as,
[3] => Array
(
[0] => 21
[1] => 4
)
look as next array, the next array key value not found in the database then expected results as it is,
[5] => Array
(
[0] => 9
[1] => 3
)
The final output look for,
Array
(
[1] => 22
[2] => 12
[3] => Array
(
[0] => 21
[1] => 4
)
[5] => Array
(
[0] => 9
[1] => 3
)
[7] => 7
[8] => 6
[9] => 1
[10] => 2
[11] => 10
[12] => 0
[13] => 23
[14] => 18
[15] => 16
[16] => 19
[17] => 15
[18] => 5
[19] => 8
[20] => 11
[21] => 13
[22] => 17
[23] => 14
[24] => 20
)
Could you please help me
Check this.
$dump_array=Array('1' => 2,'2' => 12,'3' => Array
('0' => 8,'1' => 21 ),
'5' => Array ('0' => 9,'1' => 3),
'7' => 7,
'8' => 64,
'9' => 15,
'10' => 22,
);
echo "<pre> Soure :"; print_r($dump_array);echo "</pre>";
sort($dump_array);
$result_array=array();
$array_unlist=array();
foreach($dump_array as $key=>$value){
if(is_array($value)){
sort($value);
array_push($array_unlist,$value);
}
else {array_push($result_array,$value);}
}
foreach($result_array as $index=>$val){
foreach($array_unlist as $key=>$digit){
if($val<$digit[0])
{
$res=$index.".5";
$result_array[$res]=$digit;
}
}
}
ksort($result_array);
echo "<pre> Result :"; print_r(array_values($result_array));echo "</pre>";
output:
Soure :Array
(
[1] => 2
[2] => 12
[3] => Array
(
[0] => 8
[1] => 21
)
[5] => Array
(
[0] => 9
[1] => 3
)
[7] => 7
[8] => 64
[9] => 15
[10] => 22
)
Result :Array
(
[0] => 2
[1] => Array
(
[0] => 3
[1] => 9
)
[2] => 7
[3] => Array
(
[0] => 8
[1] => 21
)
[4] => 12
[5] => 15
[6] => 22
[7] => 64
)
try this
foreach($array as $k => $v){
if(is_array($v)){
rsort($array[$k]);
}
}
print_r($array);

Get Range of dates from an array

I'm having the following array of dates :
Array
(
[0] => 2016-02-01
[1] => 2016-02-02
[2] => 2016-02-03
[3] => 2016-02-04
[4] => 2016-02-05
[5] => 2016-02-06
[6] => 2016-02-07
[7] => 2016-02-08
[8] => 2016-02-09
[9] => 2016-02-13
[10] => 2016-02-14
[11] => 2016-02-15
[12] => 2016-03-13
[13] => 2016-03-14
[14] => 2016-03-15
[15] => 2016-03-16
[16] => 2016-03-17
[17] => 2016-03-18
[18] => 2016-03-19
[19] => 2016-04-19
[20] => 2016-04-20
[21] => 2016-04-21
[22] => 2016-04-22
)
How can i get the array dates that form a range. Like
Array
(
[0] => 2016-02-01
[1] => 2016-02-02
[2] => 2016-02-03
[3] => 2016-02-04
[4] => 2016-02-05
[5] => 2016-02-06
[6] => 2016-02-07
[7] => 2016-02-08
[8] => 2016-02-09
)
Array
(
[0] => 2016-02-13
[1] => 2016-02-14
[2] => 2016-02-15
)
Array
(
[0] => 2016-03-13
[1] => 2016-03-14
[2] => 2016-03-15
[3] => 2016-03-16
[4] => 2016-03-17
[5] => 2016-03-18
[6] => 2016-03-19
)
The dates in the main array I listed can be of any order. The example I provided all the date ranges can be identifiable. But the raay is dynamic and can have date in random order. All I want to have date ranges out if that array. I'm getting mad of thinking. Please help. Thanks in advance.
You have the following code. Please try this.
$array = Array(0 => '2016-02-01',
1 => '2016-02-02',
2 => '2016-03-19',
3 => '2016-02-04',
4 => '2016-02-05',
5 => '2016-03-18',
6 => '2016-02-07',
7 => '2016-02-08',
8 => '2016-02-09',
9 => '2016-02-13',
10 => '2016-02-14',
11 => '2016-02-15',
12 => '2016-03-13',
13 => '2016-03-14',
14 => '2016-03-15',
15 => '2016-03-16',
16 => '2016-03-17',
17 => '2016-02-06',
18 => '2016-02-03',
19 => '2016-04-19',
20 => '2016-04-20',
21 => '2016-04-21',
22 => '2016-04-22');
//echo "<pre>";print_r($array);
sort($array);
$newDateArray = array();
$i = 0;
$arrayCount = count($array);
foreach($array as $key=>$val) {
//echo $key;
if(isset($array[$key+1])) {
$date1 = $val;
$date2 = $array[$key+1];
$newDateArray[$i][] = $date1;
$datetime1 = date_create($date1);
$datetime2 = date_create($date2);
$interval = date_diff($datetime1, $datetime2);
if($interval->days > 1) {
$i++;
}
}
else if($arrayCount == ($key+1)) {
$newDateArray[$i][] = $date2;
}
}
echo "<pre>";print_r($newDateArray);exit;
your result would be as follows.
Array
(
[0] => Array
(
[0] => 2016-02-01
[1] => 2016-02-02
[2] => 2016-02-03
[3] => 2016-02-04
[4] => 2016-02-05
[5] => 2016-02-06
[6] => 2016-02-07
[7] => 2016-02-08
[8] => 2016-02-09
)
[1] => Array
(
[0] => 2016-02-13
[1] => 2016-02-14
[2] => 2016-02-15
)
[2] => Array
(
[0] => 2016-03-13
[1] => 2016-03-14
[2] => 2016-03-15
[3] => 2016-03-16
[4] => 2016-03-17
[5] => 2016-03-18
[6] => 2016-03-19
)
[3] => Array
(
[0] => 2016-04-19
[1] => 2016-04-20
[2] => 2016-04-21
[3] => 2016-04-22
)
)
Now you can easily split your array. and get the result what you exactly want.
Have a Great Day!!!... Fill free to ask more your doubts...

How to get values in an array with many arrays in php?

I am confused with this list of array. I have a text file that contains the values that I will need to insert to the database. I have exploded it so that I can get those separated values.
REP 1020001,3,140822,140822;0111,260.00,23,34,3,54,1,2,4,5,12,23,46;0214,22.00,32,4,11,25,4,12,23,5,2,2,44;0313,25.00,5,52,12,45,12,5,6,7,12,3,33;
My code is just like this:
$read = FileRead($tmp);
$perline = explode(";",$read);
for($j=0; $j<count($perline); $j++)
{
$percomma = explode(",", $perline[$j]);
print_r ($percomma);
}
And the result is this:
Array ( [0] => 1020001 [1] => 3 [2] => 140822 [3] => 140822 ) Array ( [0] => 0111 [1] => 260.00 [2] => 23 [3] => 34 [4] => 3 [5] => 54 [6] => 1 [7] => 2 [8] => 4 [9] => 5 [10] => 12 [11] => 23 [12] => 46 ) Array ( [0] => 0214 [1] => 22.00 [2] => 32 [3] => 4 [4] => 11 [5] => 25 [6] => 4 [7] => 12 [8] => 23 [9] => 5 [10] => 2 [11] => 2 [12] => 44 ) Array ( [0] => 0313 [1] => 25.00 [2] => 5 [3] => 52 [4] => 12 [5] => 45 [6] => 12 [7] => 5 [8] => 6 [9] => 7 [10] => 12 [11] => 3 [12] => 33 ) Array ( [0] => )
What should I do to get the value from the 2nd array to the 4th array? Should I put it inside another array to make it multidimensional? Or there are other ways to solve this?
EDIT
My question is how will I be able to get the values from the print_r($percomma) when there are a lot of arrays in the result. The result array is up there that says And the result is this
EDIT 2
As making the array to be multidimensional I get this as a result:
Array ( [0] => Array ( [0] => 0111 [1] => 260.00 [2] => 23 [3] => 34 [4] => 3 [5] => 54 [6] => 1 [7] => 2 [8] => 4 [9] => 5 [10] => 12 [11] => 23 [12] => 46 ) ) Array ( [0] => Array ( [0] => 0214 [1] => 22.00 [2] => 32 [3] => 4 [4] => 11 [5] => 25 [6] => 4 [7] => 12 [8] => 23 [9] => 5 [10] => 2 [11] => 2 [12] => 44 ) ) Array ( [0] => Array ( [0] => 0313 [1] => 25.00 [2] => 5 [3] => 52 [4] => 12 [5] => 45 [6] => 12 [7] => 5 [8] => 6 [9] => 7 [10] => 12 [11] => 3 [12] => 33 ) ) Array ( [0] => Array ( [0] => ) )
It shows that they're all individual arrays. My code is this:
$read = FileRead($tmp);
$perline = explode(";",$read);
for($j=0; $j<count($perline); $j++)
{
$percomma = explode(",", $perline[$j]);
$entries = array($percomma);
print_r ($entries);
}
EDIT 3
From subas_poudel's answer I get this result:
Array
(
)
Array
(
[0] => Array
(
[0] => 0111
[1] => 260.00
[2] => 23
[3] => 34
[4] => 3
[5] => 54
[6] => 1
[7] => 2
[8] => 4
[9] => 5
[10] => 12
[11] => 23
[12] => 46
)
)
Array
(
[0] => Array
(
[0] => 0111
[1] => 260.00
[2] => 23
[3] => 34
[4] => 3
[5] => 54
[6] => 1
[7] => 2
[8] => 4
[9] => 5
[10] => 12
[11] => 23
[12] => 46
)
[1] => Array
(
[0] => 0214
[1] => 22.00
[2] => 32
[3] => 4
[4] => 11
[5] => 25
[6] => 4
[7] => 12
[8] => 23
[9] => 5
[10] => 2
[11] => 2
[12] => 44
)
)
Array
(
[0] => Array
(
[0] => 0111
[1] => 260.00
[2] => 23
[3] => 34
[4] => 3
[5] => 54
[6] => 1
[7] => 2
[8] => 4
[9] => 5
[10] => 12
[11] => 23
[12] => 46
)
[1] => Array
(
[0] => 0214
[1] => 22.00
[2] => 32
[3] => 4
[4] => 11
[5] => 25
[6] => 4
[7] => 12
[8] => 23
[9] => 5
[10] => 2
[11] => 2
[12] => 44
)
[2] => Array
(
[0] => 0313
[1] => 25.00
[2] => 5
[3] => 52
[4] => 12
[5] => 45
[6] => 12
[7] => 5
[8] => 6
[9] => 7
[10] => 12
[11] => 3
[12] => 33
)
)
Array
(
[0] => Array
(
[0] => 0111
[1] => 260.00
[2] => 23
[3] => 34
[4] => 3
[5] => 54
[6] => 1
[7] => 2
[8] => 4
[9] => 5
[10] => 12
[11] => 23
[12] => 46
)
[1] => Array
(
[0] => 0214
[1] => 22.00
[2] => 32
[3] => 4
[4] => 11
[5] => 25
[6] => 4
[7] => 12
[8] => 23
[9] => 5
[10] => 2
[11] => 2
[12] => 44
)
[2] => Array
(
[0] => 0313
[1] => 25.00
[2] => 5
[3] => 52
[4] => 12
[5] => 45
[6] => 12
[7] => 5
[8] => 6
[9] => 7
[10] => 12
[11] => 3
[12] => 33
)
)
The last set of array is what I needed but how can I get those values?
EDIT 4
With my simple mistake I put the array_slice inside my for loop that's why I get too many arrays. Now this is the result of #subas_poudel's answer.
Array
(
[0] => Array
(
[0] => 0111
[1] => 260.00
[2] => 23
[3] => 34
[4] => 3
[5] => 54
[6] => 1
[7] => 2
[8] => 4
[9] => 5
[10] => 12
[11] => 23
[12] => 46
)
[1] => Array
(
[0] => 0214
[1] => 22.00
[2] => 32
[3] => 4
[4] => 11
[5] => 25
[6] => 4
[7] => 12
[8] => 23
[9] => 5
[10] => 2
[11] => 2
[12] => 44
)
[2] => Array
(
[0] => 0313
[1] => 25.00
[2] => 5
[3] => 52
[4] => 12
[5] => 45
[6] => 12
[7] => 5
[8] => 6
[9] => 7
[10] => 12
[11] => 3
[12] => 33
)
)
you can use multidimensional array to hold all the value. then you can get all the array you want by either $percomma[index you want] or using array_slice.
$read = FileRead($tmp);
$perline = explode(";",$read);
for($j=0; $j<count($perline); $j++)
{
$percomma[] = explode(",", $perline[$j]);
}
echo '<pre>'.print_r (array_slice($percomma,1,3),true).'</pre>';
$array = array
(
array("bla",22,18),
array("blaa",15,13),
array("blaaa",5,2),
array("blaaaa",17,15)
);
for ($row = 0; $row < 4; $row++) {
echo "<p><b>Row number $row</b></p>";
echo "<ul>";
for ($col = 0; $col < 3; $col++) {
echo "<li>".$array[$row][$col]."</li>";
}
echo "</ul>";
}
I'm going to copy/edit your original code posting, to show what I was talking about in my comment.
$read = FileRead($tmp);
$perline = explode(";",$read);
$percomma = array(); //new array-declaration line
for($j=0; $j<count($perline); $j++)
{ $percomma[j] = explode(",", $perline[$j]);
print_r ($percomma[j]);
}
Your first "And the result is" line is a printed sequence of arrays, and would be unchanged by implementing the above tweaks to your code. However, because the values in the $percomma variable are no longer overwritten in each loop that obtains an array from the explode() function, you may now, at any point after the above code, do something like:
print_r($percomma[1]); //re-print 2nd array, or
$ary_el = $percomma[2][1]; //get 2nd element of 3rd array (22.00) into a variable.

php calendar and multidimensional arrays

I have an array of calendar dates, and an multidimensional array of reports where the key to the reports is the date
Array of calendar dates structure
Array
(
[1] => 2014-05-01
[2] => 2014-05-02
[3] => 2014-05-03
[4] => 2014-05-04
[5] => 2014-05-05
[6] => 2014-05-06
[7] => 2014-05-07
[8] => 2014-05-08
[9] => 2014-05-09
[10] => 2014-05-10
[11] => 2014-05-11
[12] => 2014-05-12
[13] => 2014-05-13
[14] => 2014-05-14
[15] => 2014-05-15
[16] => 2014-05-16
[17] => 2014-05-17
[18] => 2014-05-18
[19] => 2014-05-19
[20] => 2014-05-20
[21] => 2014-05-21
[22] => 2014-05-22
[23] => 2014-05-23
[24] => 2014-05-24
[25] => 2014-05-25
[26] => 2014-05-26
[27] => 2014-05-27
[28] => 2014-05-28
[29] => 2014-05-29
[30] => 2014-05-30
[31] => 2014-05-31
)
And my Array of reports structure
Array
(
[2014-05-01] => Array
(
[0] => 16
[1] => 17
[2] => 18
[3] => 20
[4] => 21
[5] => 22
[6] => 15
[7] => 14
[8] => 13
[9] => 1
[10] => 3
[11] => 4
[12] => 5
[13] => 12
)
[2014-05-03] => Array
(
[0] => 5
[1] => 4
[2] => 3
[3] => 1
[4] => 40
)
[2014-05-11] => Array
(
[0] => 2
[1] => 5
[2] => 4
[3] => 3
[4] => 7
[5] => 8
[6] => 9
[7] => 10
[8] => 6
[9] => 1
)
[2014-05-17] => Array
(
[0] => 3
[1] => 10
[2] => 9
[3] => 8
[4] => 7
[5] => 6
[6] => 2
[7] => 5
[8] => 4
[9] => 1
)
[2014-05-18] => Array
(
[0] => 4
[1] => 5
[2] => 3
[3] => 1
)
[2014-05-19] => Array
(
[0] => 5
[1] => 4
[2] => 3
[3] => 1
)
[2014-05-20] => Array
(
[0] => 1
[1] => 9
[2] => 8
[3] => 7
[4] => 6
[5] => 2
[6] => 5
[7] => 4
[8] => 3
[9] => 10
)
[2014-05-26] => Array
(
[0] => 1
[1] => 3
[2] => 4
[3] => 5
)
[2014-05-27] => Array
(
[0] => 10
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 9
[6] => 1
[7] => 6
[8] => 8
[9] => 7
)
)
Now i want to loop through the calendar and for every value in the calendar array (the date) that matches the key of the report array (eg the date), i want to attach the reports to calendar value and use the value calendar as a key to the reports other wise i want to add a string saying no reports done.
heres how i though i might go about
foreach ($calendar as $cal)
{
foreach ($report as $key => $rdate)
{
if ($key == $cal)
{
$calendar[][$cal] = $rdate;
}
}
}
this however just adds them to the end of the calendar array and not where the calendar value and report key match.
Like so
Array
(
[1] => 2014-05-01
[2] => 2014-05-02
[3] => 2014-05-03
[4] => 2014-05-04
[5] => 2014-05-05
[6] => 2014-05-06
[7] => 2014-05-07
[8] => 2014-05-08
[9] => 2014-05-09
[10] => 2014-05-10
[11] => 2014-05-11
[12] => 2014-05-12
[13] => 2014-05-13
[14] => 2014-05-14
[15] => 2014-05-15
[16] => 2014-05-16
[17] => 2014-05-17
[18] => 2014-05-18
[19] => 2014-05-19
[20] => 2014-05-20
[21] => 2014-05-21
[22] => 2014-05-22
[23] => 2014-05-23
[24] => 2014-05-24
[25] => 2014-05-25
[26] => 2014-05-26
[27] => 2014-05-27
[28] => 2014-05-28
[29] => 2014-05-29
[30] => 2014-05-30
[31] => 2014-05-31
[32] => Array
(
[2014-05-01] => Array
(
[0] => 16
[1] => 17
[2] => 18
[3] => 20
[4] => 21
[5] => 22
[6] => 15
[7] => 14
[8] => 13
[9] => 1
[10] => 3
[11] => 4
[12] => 5
[13] => 12
)
)
[33] => Array
(
[2014-05-03] => Array
(
[0] => 5
[1] => 4
[2] => 3
[3] => 1
[4] => 40
)
)
[34] => Array
(
[2014-05-11] => Array
(
[0] => 2
[1] => 5
[2] => 4
[3] => 3
[4] => 7
[5] => 8
[6] => 9
[7] => 10
[8] => 6
[9] => 1
)
)
[35] => Array
(
[2014-05-17] => Array
(
[0] => 3
[1] => 10
[2] => 9
[3] => 8
[4] => 7
[5] => 6
[6] => 2
[7] => 5
[8] => 4
[9] => 1
)
)
[36] => Array
(
[2014-05-18] => Array
(
[0] => 4
[1] => 5
[2] => 3
[3] => 1
)
)
[37] => Array
(
[2014-05-19] => Array
(
[0] => 5
[1] => 4
[2] => 3
[3] => 1
)
)
[38] => Array
(
[2014-05-20] => Array
(
[0] => 1
[1] => 9
[2] => 8
[3] => 7
[4] => 6
[5] => 2
[6] => 5
[7] => 4
[8] => 3
[9] => 10
)
)
[39] => Array
(
[2014-05-26] => Array
(
[0] => 1
[1] => 3
[2] => 4
[3] => 5
)
)
[40] => Array
(
[2014-05-27] => Array
(
[0] => 10
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 9
[6] => 1
[7] => 6
[8] => 8
[9] => 7
)
)
)
instead of this i would like this
Array (
[1] => 2014-05-01 => Array
(
[0] => 16
[1] => 17
[2] => 18
[3] => 20
...........
)
I think where i have been bashing my head against a wall i cant see the woods from the trees.
Any help would be grand or guidence would be grand.
regards Mike
So by writing this out i must of figured it out so it actually helped me just by forming the question, this is what i changed
foreach ($dates_month_with_reports as $cal)
{
foreach ($reportdates as $key => $rdate)
{
if ($key == $cal)
{
//took away the extra array
$dates_month_with_reports[$cal] = $rdate;
}
}
}
and in the calendar array i added the date to the key aswell which now gives me this
Array
(
[2014-05-01] => Array
(
[0] => 16
[1] => 17
[2] => 18
[3] => 20
[4] => 21
[5] => 22
[6] => 15
[7] => 14
[8] => 13
[9] => 1
[10] => 3
[11] => 4
[12] => 5
[13] => 12
)
[2014-05-02] => 2014-05-02
[2014-05-03] => Array
(
[0] => 5
[1] => 4
[2] => 3
[3] => 1
[4] => 40
)
[2014-05-04] => 2014-05-04
[2014-05-05] => 2014-05-05
[2014-05-06] => 2014-05-06
[2014-05-07] => 2014-05-07
[2014-05-08] => 2014-05-08
[2014-05-09] => 2014-05-09
[2014-05-10] => 2014-05-10
[2014-05-11] => Array
(
[0] => 2
[1] => 5
[2] => 4
[3] => 3
[4] => 7
[5] => 8
[6] => 9
[7] => 10
[8] => 6
[9] => 1
)
[2014-05-12] => 2014-05-12
[2014-05-13] => 2014-05-13
[2014-05-14] => 2014-05-14
[2014-05-15] => 2014-05-15
[2014-05-16] => 2014-05-16
[2014-05-17] => Array
(
[0] => 3
[1] => 10
[2] => 9
[3] => 8
[4] => 7
[5] => 6
[6] => 2
[7] => 5
[8] => 4
[9] => 1
)
[2014-05-18] => Array
(
[0] => 4
[1] => 5
[2] => 3
[3] => 1
)
[2014-05-19] => Array
(
[0] => 5
[1] => 4
[2] => 3
[3] => 1
)
[2014-05-20] => Array
(
[0] => 1
[1] => 9
[2] => 8
[3] => 7
[4] => 6
[5] => 2
[6] => 5
[7] => 4
[8] => 3
[9] => 10
)
[2014-05-21] => 2014-05-21
[2014-05-22] => 2014-05-22
[2014-05-23] => 2014-05-23
[2014-05-24] => 2014-05-24
[2014-05-25] => 2014-05-25
[2014-05-26] => Array
(
[0] => 1
[1] => 3
[2] => 4
[3] => 5
)
[2014-05-27] => Array
(
[0] => 10
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 9
[6] => 1
[7] => 6
[8] => 8
[9] => 7
)
[2014-05-28] => 2014-05-28
[2014-05-29] => 2014-05-29
[2014-05-30] => 2014-05-30
[2014-05-31] => 2014-05-31
)
I appologize for wasting your time i didnt mean to, but after i wrote the question a figured it out.
$calendar1 = array();
foreach ($calendar as $cal)
{
foreach ($report as $key => $rdate)
{
if ($key == $cal)
{
$calendar1[][$cal] = $rdate;
}
}
}

Categories