How to print array data following current date - php

I have an array like the following:
$f= array("Sunday", "Monday","Tuesday","Wednesday", "Thursday","Friday", "Saturday");
I have run a foreach loop for $f and print date. Result is:
Array ( [0] => Sunday [1] => Monday [2] => Tuesday [3] => Wednesday [4] => Thursday [5] => Friday [6] => Saturday )
but I want if it is Wednesday today, then the output will be:
Array ( [0] => Wednesday [1] => Thursday [2] => Friday [3] => Saturday [4] => Sunday [5] => Monday [6] => Tuesday )
Here is my code:
$f= array("Sunday", "Monday","Tuesday","Wednesday", "Thursday","Friday", "Saturday");
foreach ($f as $value) {
if($value == date('l')){
$date[] = $value;
}
}
print_r($date);
it return me the following result:
Array ( [0] => Thursday [1] => Thursday [2] => Thursday [3] => Thursday [4] => Thursday [5] => Thursday [6] => Thursday )

You can do it by native php functions:
$i = array_search(date('l'), $f);
$date = array_merge(array_splice($f, $i), $f);
array_search finds today item in the array, array_splice removes tail of the array and returns it, array_merge combines that arrays in the proper order
demo on eval.in

Related

Manipulate the array based on start_time and end_time in PHP

i want output based on start_time and end_time.
Suppose there is parent array which has start_time and end_time has 6am and 8am. give it parent element 0.
3rd array has start_time and end_time has 6am and 6:30am this time falls between 1st arrays start_time and end_time so give it parent 1.
2nd array has start_time and end_time has 6am and 6:15am this time falls between 2nd array start_time and end_time so give it parent 2.
Note: there can be a lot of arrays and there is 24 hours time so anyone can be parent of anyother array. you can see in the example.
input array:
Array
(
[0] => Array
(
[id] => 1
[start_time] => 6am
[end_time] => 8am
)
[1] => Array
(
[id] => 2
[start_time] => 6am
[end_time] => 6:15am
)
[2] => Array
(
[id] => 3
[start_time] => 6am
[end_time] => 6:30am
)
)
Expected output:
Array
(
[0] => Array
(
[id] => 1
[start_time] => 6am
[end_time] => 8am
[parent] => 0
)
[1] => Array
(
[id] => 2
[start_time] => 6am
[end_time] => 6:15am
[parent] => 2 //increment this value of 3rd array parent by +1 because this time falls under 3rd array
)
[2] => Array
(
[id] => 3
[start_time] => 6am
[end_time] => 6:30am
[parent] => 1 //increment this value of 1st array parent by +1 because this time falls under 1st array
)
)
My code:
$count = count($input);
$output = array();
for ($i=0; $i < $count; $i++) {
$output[$i] = $input[$i];
$output[$i]['parent'] = $i;
for ($j=0; $j < $count; $j++) {
if ($i !== $j) {
if (checkOverlap($input[$i]['start_time'], $input[$i]['end_time'], $input[$j]['start_time'], $input[$j]['end_time'])) {
$output[$i]['parent'] = $j;
break;
}
}
}
}
function checkOverlap($start1, $end1, $start2, $end2) {
$start1 = strtotime($start1);
$end1 = strtotime($end1);
$start2 = strtotime($start2);
$end2 = strtotime($end2);
if ($start1 < $start2) {
if ($end1 > $start2 && $end1 < $end2) {
return true;
}
} else {
if ($start1 > $start2 && $start1 < $end2) {
return true;
}
}
return false;
}
I'm getting this output now which is wrong.
Array
(
[0] => Array
(
[id] => 1
[start_time] => 6am
[end_time] => 8am
[parent] => 0
)
[1] => Array
(
[id] => 2
[start_time] => 6am
[end_time] => 6:15am
[parent] => 1
)
[2] => Array
(
[id] => 3
[start_time] => 6am
[end_time] => 6:30am
[parent] => 2
)
)
it would very help for me if you can solve this.
see this event picture

Find duplicate values in a multidimensional array and create new multidimensional array that contains no dupes

I have the following array taken from a database:
Array
(
[0] => Array
(
[facility_name] => AFC Clayton Juniors
[day] => 15
[month] => Apr
[year] => 2016
[start_time] => 20
[end_time] => 21
)
[1] => Array
(
[facility_name] => AFC Clayton Juniors
[day] => 15
[month] => Apr
[year] => 2016
[start_time] => 22
[end_time] => 23
)
[2] => Array
(
[facility_name] => Chorlton Runners
[day] => 15
[month] => Apr
[year] => 2016
[start_time] => 10
[end_time] => 11
)
[3] => Array
(
[facility_name] => Chorlton Runners
[day] => 15
[month] => Apr
[year] => 2016
[start_time] => 19
[end_time] => 20
)
)
And I need it to be nested as follows::
Array
(
[0] => Array
(
[facility_name] => AFC Clayton Juniors
[dates] => Array
(
[date1] => Array
(
[date] => 15 Apr 2016
[timeslots] => Array
(
[timeslot1] => Array
(
[start_time] => 20
[end_time] => 21
)
[timeslot2] => Array
(
[start_time] => 22
[end_time] => 23
)
)
[date2] => 16 Apr 2016 //etc
)
)
[1] => Array
(
[facility_name] = Chorlton Runners
[dates] => Array //etc
)
)
To elaborate: I need to have NO duplicate names in my array, and the dates that exist for the same name to be entered as new keys to the dates array that match the same name, and the same thing for time slots. How can I do this?
For this solution let's say the array taken from the databse is named $facilities. The code would be:
$noDupes = array();
foreach ($facilities as $fac) {
$facilityIndex = -1; // The index of the facility name, -1 indicates it wasn't found.
$dateIndex = ''; // The index of the date string, an empty string indicates it wasn't found.
$timeslotIndex = ''; // The index of the timeslot, an empty string indicates it wasn't found.
$facDate = "{$fac['day']} {$fac['month']} {$fac['year']}"; // The date string (dd mmm aaaa)
foreach ($noDupes as $f => $facility) {
if ($fac['facility_name'] == $facility['facility_name']) {
// If the facility name was found we take the corresponding index (0, 1, 2, etc.).
$facilityIndex = $f;
foreach ($facility['dates'] as $d => $date) {
if ($facDate == $date['date']) {
// If the date string was found we take the corresponding index (date1, date2, date3, etc.).
$dateIndex = $d;
foreach ($date['timeslots'] as $t => $timeslot) {
if ($fac['start_time'] == $timeslot['start_time'] && $fac['end_time'] == $timeslot['end_time']) {
// If the timeslot was found we take the corresponding index (timeslot1, timeslot2, timeslot3, etc.).
$timeslotIndex = $t;
break; // end timeslot loop
}
}
break; // end date loop
}
}
break; // end facility loop
}
}
if ($facilityIndex == -1) {
// Take the new index for the date and timeslot if-statements
$facilityIndex = count($noDupes);
$noDupes[] = array(
'facility_name' => $fac['facility_name'],
'dates' => array()
);
}
if ($dateIndex == '') {
// Calculate the new index for the date (date1, date2, etc.)
$dateNum = count($noDupes[$facilityIndex]['dates']) + 1;
$dateIndex = "date{$dateNum}";
$noDupes[$facilityIndex]['dates'][$dateIndex] = array(
'date' => $facDate,
'timeslots' => array()
);
}
if ($timeslotIndex == '') {
// Calculate the new index for the timeslot (timeslot1, timeslot2, etc.)
$timeslotNum = count($noDupes[$facilityIndex]['dates'][$dateIndex]['timeslots']) + 1;
$timeslotIndex = "timeslot{$timeslotNum}";
$noDupes[$facilityIndex]['dates'][$dateIndex]['timeslots'][$timeslotIndex] = array(
'start_time' => $fac['start_time'],
'end_time' => $fac['end_time']
);
}
}
Using JSON array will make your task very easy, and is supported in PHP

Generate an array using DateInterval and DatePeriod for shifts

I'm using DateInterval and DatePeriod to generate shifts in a medical application
$begin = new DateTime('2012-08-01 08:00');
$end = new DateTime('2012-08-01 12:00');
$interval = new DateInterval('PT45M');
$daterange = new DatePeriod($begin, $interval ,$end);
Those functions allow me to generate the following array
Array
(
[0] => 08:00
[1] => 08:45
[2] => 09:30
[3] => 10:15
[4] => 11:00
[5] => 11:45
)
Which functions and how could I do so the final array gets completed and chunked as this?
Array
(
[0] => Array
(
[0] => 08:00
[1] => 08:45
)
[1] => Array
(
[0] => 08:45
[1] => 09:30
)
[2] => Array
(
[0] => 09:30
[1] => 10:15
)
[3] => Array
(
[0] => 10:15
[1] => 11:00
)
[4] => Array
(
[0] => 11:00
[1] => 11:45
)
)
Thanks
$previous = null;
$dates = array();
foreach ($period as $dt) {
$current = $dt->format("H:i");
if (!empty($previous)) {
$show = new DateTime($current);
$dates[] = array($previous, $show->format('H:i'));
}
$previous = $current;
}
After retrieve your array using DateTime function. Just Plug n Play with the code. :)
$retrieveArray = array('08:00','08:45','09:30','10:15','11:00','11:45'); // Sample Array, Assumed mentioned in Question.
$final = array();
for ($i=0; $i < count($retrieveArray)-1; $i++) {
$j = $i+1;
$final[$i] = array($retrieveArray[$i], $retrieveArray[$j]);
}
Use var_dump($final); to check array result.
Desired Output
Array
(
[0] => Array
(
[0] => 08:00
[1] => 08:45
)
[1] => Array
(
[0] => 08:45
[1] => 09:30
)
[2] => Array
(
[0] => 09:30
[1] => 10:15
)
[3] => Array
(
[0] => 10:15
[1] => 11:00
)
[4] => Array
(
[0] => 11:00
[1] => 11:45
)
)
Edit:
To Add Last Shift
#jhon is right. It skips the last boundary value if it matches exactly the length of the interval.
As you need it, I suggest to ADD +1 min to your end time. It's a tweak/fix but server the purpose.
$begin = new DateTime('2012-08-01 08:00');
$end = new DateTime('2012-08-01 10:00');
$end = $end->modify( '+1 minute' ); // Tweak, to add last shift value.
$interval = new DateInterval('PT30M');
$daterange = new DatePeriod($begin, $interval ,$end);
To Check Output
foreach ( $daterange as $dt ){
echo '<pre>';
echo $dt->format( "H:i" );
echo '</pre>';
}
It is including last boundary value if it matches exactly the length of the interval.

Array montlhy reporting but not from 1st of month

I have a large array of booking dates and amount, like so :
Array
(
[0] => Array
(
[date] => 2014-04-04
[total] => 30.00
)
[1] => Array
(
[date] => 2014-04-05
[total] => 47.00
)
[9998] => Array
(
[date] => 2014-08-21
[total] => 52.00
)
... ++ a lot of dates associated to numbers.
and process it to get monthly reports :
$months = array();
foreach($myarray as $k=>$v) {
list($y,$m) = explode("-",$v['date']);
$months[$y."-".$m][] = $v['total'];
}
which gives me a nice array :
Array
(
[2014-04] => Array <-- Every amount made in April
(
[0] => 30.00
[1] => 47.00
[2] => 47.00
...
)
[2014-05] => Array <-- Every amount made in May
(
[0] => 68.00
[1] => 42.00
....
)...
However I'm trying to find a way the same thing but starting from a specific day, say 5 for example, in order to get (dates labelled for clarity):
Array
(
[from January 5 to February 4] => Array
(
[0] => 30.00
[1] => 47.00
[2] => 47.00
)
[from February 5 to March 4] => Array
(
[0] => 30.00
[1] => 47.00
[2] => 47.00
)...
So every amount made each month, but starting from the 5th of each month.
Something like this should work. I hate saying should, but I don't have the data to test with. Let me know if anything's wrong:
$months = array();
foreach($months as $k => $v) {
list($y, $m, $d) = explode("-", $v['date']);
if($d < 5) {
if($m == 1) {
$y--;
$m = 12;
} else {
$m--;
}
}
$months[$y . "-" . $m][] = $v['total'];
}

Reorganize array by date [duplicate]

This question already has answers here:
PHP re-order array of month names
(5 answers)
Closed 8 years ago.
I'm trying to reorganize my array orderned by date.
For example this is my array:
Array(
[0] => august
[1] => july
[2] => october
[3] => september
)
How can i reorganize it chronologically, so it would become this:
Array(
[0] => july
[1] => august
[2] => september
[3] => october
)
Use date_parse to parse the month name and convert it to the corresponding number and then use ksort to sort them.
$myArray = array('august', 'july', 'october', 'september');
foreach($myArray as $value) {
$m = date_parse($value);
$output[$m['month']] = ucfirst($value);
}
ksort($output);
print_r($output);
Output:
Array
(
[7] => July
[8] => August
[9] => September
[10] => October
)
Source: #12424968
Demo!
This should work for any date that strtotime can parse
function cmp($a, $b)
{
$a_time = strtotime($a);
$b_time = strtotime($b);
if ($a_time == $b_time) {
return 0;
}
return ($a_time < $b_time) ? -1 : 1;
}
// Array to be sorted
$array = array('november', 'august', 'december', 'february');
print_r($array);
// Sort and print the resulting array
usort($array, 'cmp');
print_r($array);
Output:
Array
(
[0] => november
[1] => august
[2] => december
[3] => february
)
Array
(
[0] => february
[1] => august
[2] => november
[3] => december
)

Categories