Create a 12 months array from few months given - php

I've got this PHP array:
array(
[1528840800] => 50 // 12 June 2018 22:00:00
[1531173600] => 37 // 9 July 2018 22:00:00
[1540940400] => 44 // 30 October 2018 23:00:00
[1543532400] => 9 // 29 November 2018 23:00:00
)
How can i get a full 12 months array where remaining months value are zero?
array(
[01] => 0
[02] => 0
[03] => 0
[04] => 0
[05] => 0
[1528840800] => 50 // 12 June 2018 22:00:00
[1531173600] => 37 // 9 July 2018 22:00:00
[08] => 0
[09] => 0
[1540940400] => 44 // 30 October 2018 23:00:00
[1543532400] => 9 // 29 November 2018 23:00:00
[12] => 0
)
This not works property on the second value..
for ($i=1;$i<13;$i++) {
foreach ($array as $key => $value) {
if (date('m',$key)===sprintf('%02d', $i)) {
$fullmonths[date('d/m/Y',$key)] = $value;
break;
}else{
$fullmonths['XXXX/'.sprintf('%02d', $i).'/2018]=0;
}
}
}
Many thanks.

Just some changes to skip to the next month correctly. You can break or continue a loop on any level. If set continue 2 you starts next itteration of the for loop. Else, you will fill default value
for ($i=1;$i<13;$i++) {
foreach ($array as $key => $value) {
if (date('m',$key) === sprintf('%02d', $i)) {
$fullmonths[date('d/m/Y',$key)] = $value;
// If found skip to next month
continue 2;
}
}
$fullmonths['XX/'.sprintf('%02d', $i).'/2018'] = 0;
}
demo

Kind of what you were already doing.
If you want to re:use the original array, just change $months to $array.
$array = [
"1528840800" => 50,
"1531173600" => 37,
"1540940400" => 44,
"1543532400" => 9
];
$months = [];
for ($i=1;$i <= 12;$i++) {
foreach($array as $time => $value) {
$date = date("n", $time);
if ($date == $i) {
$months[$time] = $value;break;
} else {
$months[$i] = 0;
}
}
}
var_dump($months);
If you need Zero padding to the months, simply add str_pad() to the [$i], like these:
[str_pad($i, 2, "0", STR_PAD_LEFT)]

You can try something like this using STR_PAD_LEFT and update the key.
$mainArray=array();
$output = array('1528840800' => '50', '1531173600' => '37', '1540940400' => '44', '1543532400' => '9');
for ($i=1; $i <= 12; $i++)
{
$value = str_pad($i, 2, "0", STR_PAD_LEFT);
$mainArray[$value] = 0; //set Everykey to Zero
}
foreach ($output as $key => $value)
{
$joy = gmdate("m", $key);
$mainArray[$key]=$value; //update the key
unset($mainArray[$joy]);
}

Related

array_merge seems not to be recognised key. I would like to insert numeric key but it always starts from 0

I would like to make an array like below.
The key should be day. The value should be string weekday name.
$array = [
1 =>['mon'],
2 =>['tue'],
3 =>['wed']
];
However $day (key) can't seem to be recognised.
CODE is
//$i is the first day of a specific month. this sample is 2022-11-01
for($j = $i ; (int)date_create($j)->format('w') < 6 ; $j++){
print_r("くj");
print_r($j);
$day = date_create($j)->format('j');
print_r("day");
print_r($day);
$dailyArray = array_merge($dailyArray ,array( $day => mb_strtolower(date_create($j)->format('D'))));//
print_r("THE RESULT!!");
print_r($dailyArray);
result(November/2022) is
THE RESULT!! day4
Array
(
[0] => Tue
[1] => Wed
[2] => Thu
[3] => Fri
)
It's not the question, but it can't set (int)date_create($j)->format('w') <= 6 . How can I do the loop until sat?
The following script was based on your code basically and it will print out the result as expected, till Saturday
$dailyArray = array();
$d = 1;
while($d < 6 ){
$j = implode("-", array(2022, 11, str_pad($d, 2, '0', STR_PAD_LEFT)));
print_r("くj");
print_r($j);
$day = date_create($j)->format('j');
print_r("day");
print_r($day);
$dailyArray = array_merge($dailyArray , array( $day => strtolower(date_create($j)->format('D'))));
$d++;
}
print_r("<p>THE RESULT!!");
print_r($dailyArray);
It should print out
THE RESULT!!Array ( [0] => tue [1] => wed [2] => thu [3] => fri [4] => sat )
Actually, if (int)date_create($j)->format('w') < 6 is preferred, a shorter version could be
$d = 1;
$j = implode("-", array(2022, 11, str_pad($d, 2, '0', STR_PAD_LEFT)));
while((int)date_create($j)->format('w') < 6 ){
$j = implode("-", array(2022, 11, str_pad($d, 2, '0', STR_PAD_LEFT)));
$dailyArray[$d] = strtolower(date_create($j)->format('D'));
$d++;
}
It printed out:
THE RESULT!!Array ( [1] => tue [2] => wed [3] => thu [4] => fri [5] => sat )

PHP get closest time to given array of times [duplicate]

This question already has answers here:
How to get closest date compared to an array of dates in PHP
(6 answers)
Closed 3 years ago.
I have an array called $timeslots with timeslots like:
array:32 [▼
0 => "2018-12-15T12:00:00.0000000"
1 => "2018-12-15T12:15:00.0000000"
2 => "2018-12-15T12:30:00.0000000"
3 => "2018-12-15T12:45:00.0000000"
4 => "2018-12-15T13:00:00.0000000"
5 => "2018-12-15T13:15:00.0000000"
6 => "2018-12-15T13:45:00.0000000"
7 => "2018-12-15T14:15:00.0000000"
8 => "2018-12-15T14:30:00.0000000"
9 => "2018-12-15T14:45:00.0000000"
10 => "2018-12-15T15:00:00.0000000"
11 => "2018-12-15T15:15:00.0000000"
12 => "2018-12-15T15:30:00.0000000"
13 => "2018-12-15T15:45:00.0000000"
14 => "2018-12-15T16:15:00.0000000"
15 => "2018-12-15T16:45:00.0000000"
16 => "2018-12-15T17:00:00.0000000"
17 => "2018-12-15T17:30:00.0000000"
18 => "2018-12-15T17:45:00.0000000"
19 => "2018-12-15T18:30:00.0000000"
20 => "2018-12-15T18:45:00.0000000"
21 => "2018-12-15T19:15:00.0000000"
22 => "2018-12-15T19:45:00.0000000"
23 => "2018-12-15T20:15:00.0000000"
24 => "2018-12-15T20:45:00.0000000"
25 => "2018-12-15T21:00:00.0000000"
26 => "2018-12-15T21:15:00.0000000"
27 => "2018-12-15T21:30:00.0000000"
28 => "2018-12-15T21:45:00.0000000"
29 => "2018-12-15T22:00:00.0000000"
30 => "2018-12-15T22:15:00.0000000"
31 => "2018-12-15T22:30:00.0000000"
]
Also, I have a variable like:
$expected_time = 2018-12-15T18:00:00.0000000; // this can be different value, so its not unique value
$expected_time is never into array $timeslots but I need to find closest value to $expected_time... How I can do that?
How I can get the closest timeslot value from array $timeslots to $expected_time and calculate the difference in minutes?
Any idea?
As Nico mentioned in the comments, it's pretty straightforward. Just looping and calculating the time difference.
$timeslots = [...];
$expected_time = "2018-12-15T18:00:00.0000000";
$timestamp = strtotime($expected_time);
$diff = null;
$index = null;
foreach ($timeslots as $key => $time) {
$currDiff = abs($timestamp - strtotime($time));
if (is_null($diff) || $currDiff < $diff) {
$index = $key;
$diff = $currDiff;
}
}
echo $timeslots[$index];
Here is one solution fitting your requirements:
function findClosestDate($expectedDate,$dates)
{
$differenceInMinutes = null;
$expectedDate = new DateTime($expectedDate);
$expectedDateEpoch = $expectedDate->getTimestamp();
$returnIndex = -1;
for($i = 0; $i<count($dates); $i++)
{
$dateObject = new DateTime($dates[$i]);
$dateEpoch = $dateObject->getTimestamp();
$difference = abs($expectedDateEpoch-$dateEpoch);
$difference = $difference/60;
if($differenceInMinutes === null || $difference < $differenceInMinutes)
{
$differenceInMinutes = $difference;
$returnIndex = $i;
}
}
return array(
"closest" => $dates[$returnIndex],
"difference" => $differenceInMinutes
) ;
}
This makes use of the DateTime class to create a DateTime object and get the respective timestamp. The minutes are then calculated by the absolute difference between the expectedDate and the entry in the dates array. After iterating over the whole array, the closest match and the difference is returned in one array.
as your list is already sorted - u can put the element into array and use sort again - and it will be much faster then calculating the diff in each iteration.
<?php
$timeslots = [
...
];
$expected_time = "2018-12-15T18:00:00.0000000";
$counter = count($timeslots);
$timeslots = array_flip($timeslots);
$timeslots[$expected_time] = $counter;
ksort($timeslots);
while (key($timeslots) !== $expected_time) {
$prev = key($timeslots);
next($timeslots);
}
next($timeslots);
$next = key($timeslots);
$expected_time = new \DateTime($expected_time);
$closestDiff = min(($expected_time)->diff(new \DateTime($prev)), (new \DateTime($next))->diff($expected_time));
var_dump($closestDiff->i);

How to count array with the same month

$dates = array('2017-03-24 01:48:09', '2017-03-24 11:48:09', '2017-04-07 01:12:19', '2017-04-14 01:49:09', '2017-04-21 01:45:09', '2017-04-28 01:38:09');
if given array above..
i'm making a report to monitor user growth monthly.. i want to count the no. of entries per month
sample April 2 counts
$dates = array('2017-03-24 01:48:09', '2017-03-24 11:48:09', '2017-04-07 01:12:19', '2017-04-14 01:49:09', '2017-04-21 01:45:09', '2017-04-28 01:38:09');
$count = array();
foreach ($dates as $d) {
$count[date('m', strtotime($d))]++;
}
print_r($count);
Output will be:
Array ([03] => 2 [04] => 4 )
That is: 03 (March) has 2 values in given array.
If you need 03 as March, then use:
$count[date('F', strtotime($d))]++;
Then, output will be:
Array ([March] => 2 [April] => 4 )
<?php
$dates = array('2017-03-24 01:48:09', '2017-03-24 11:48:09', '2017-04-07 01:12:19', '2017-04-14 01:49:09', '2017-04-21 01:45:09', '2017-04-28 01:38:09');
foreach($dates as $date)
$year_months[] = date('Y-m', strtotime($date));
var_export($year_months);
var_export(array_count_values($year_months));
Output:
array (
0 => '2017-03',
1 => '2017-03',
2 => '2017-04',
3 => '2017-04',
4 => '2017-04',
5 => '2017-04',
)array (
'2017-03' => 2,
'2017-04' => 4,
)

PHP How To Calculate Yearly Increase by Given Start and End Year

By given 2 range of year, current population and target population, how to get total population every year by using PHP?
Example:
Year | Population
2014 | 100000
2018 | 132000
On paper calculation like this:
132000 / 100000 = 1.0718
so we will get result every year (On paper):
2014 = 100000
2015 = 107187 (100000 * 1.0718)
2016 = 114890 (107187 * 1.0718)
2017 = 123147 (114890 * 1.0718)
2018 = 132000
How to hold previous year variable to get result as above?
This is my PHP code:
for($i > $start; $i < $end; $i++) {
$this->ProjectPopulation->create();
$increase = array(
'project_id' => $project_id,
'year' => $i,
'percent_increase' => $this->percentage_increase($current_population, $target_population, $year),
'population' => ??? // Problem here
);
$this->ProjectPopulation->save($increase);
}
Thanks
There are two resources I'd recommend you taking a look at:
http://php.net/manual/en/ref.math.php
and
http://www.w3schools.com/php/php_ref_math.asp
You can solve this by doing the calculation each time, but it would be faster to use a loop in order to iterate through them all.
So something like this:
$population = 100000;
$year = 2014;
//I'd print the first one out prior to the loop, or you could put an if($year == 2014) in the loop, your choice really.
for(i= 0; i < 4; i++)
{
//do calculation in here.
echo "The current years is" . $year;
$yearsCalculation = $population * 1.0718;
//then save that value for the next time around the loop.
$population = $yearsCalculation ;
echo $yearsCalculation ;
//increase years each time round loop
$year++;
}
If you then want to store it in a MySQL database you might want to have a look at this tutorial:
http://www.w3schools.com/php/php_mysql_insert.asp
Hope that helped!
Solve
private function power($current_population, $target_population, $year) {
$devide = $target_population / $current_population;
$pwrdevider = 1 / $year;
$power = pow($devide, $pwrdevider);
return $power;
}
private function percentage_increase($current_population, $target_population, $year) {
$devide = $target_population / $current_population;
$power = pow($devide, 1 / $year);
$increase = ($power - 1) * 100;
return $increase;
}
$start = 2014;
$end = 2018;
$diff = $end - $start;
for($i = 1, $data = 100000; $i <= $diff; $i++) {
$data *= $this->power(100000, 132000, $diff);
$increase = array(
'project_id' => 1,
'year' => $start += 1,
'percent_increase' => $this->percentage_increase(100000, 132000, $diff),
'population' => $data
);
print_r($increase);
}
This is my result
Array
(
[project_id] => 1
[year] => 2015
[percent_increase] => 7.1873373728262
[population] => 107187.33737283
)
Array
(
[project_id] => 1
[year] => 2016
[percent_increase] => 7.1873373728262
[population] => 114891.25293076
)
Array
(
[project_id] => 1
[year] => 2017
[percent_increase] => 7.1873373728262
[population] => 123148.87489076
)
Array
(
[project_id] => 1
[year] => 2018
[percent_increase] => 7.1873373728262
[population] => 132000
)
Thanks all

Check for overlapping times on the same date in an array?

I have an array that is produced from people wanting to reserve a time block to volunteer with our organization. I want to check to see if they chose time blocks on the same day that overlap. In my example array below the first and third elements are overlapping and I need to detect that. Any recommendation would be much appreciated:
Array
(
[0] => Array
(
[id_pc_time_blocks] => 3
[id_pc] => 2
[pc_date] => 2012-11-21
[pc_time_block] => 9:00 AM-1:00 PM
[pc_time_block_max] => 25
[pc_time_block_count] => 0
[pc_name] => Atlanta
)
[1] => Array
(
[id_pc_time_blocks] => 4
[id_pc] => 2
[pc_date] => 2012-11-21
[pc_time_block] => 1:00 PM-5:00 PM
[pc_time_block_max] => 25
[pc_time_block_count] => 10
[pc_name] => Atlanta
)
[2] => Array
(
[id_pc_time_blocks] => 6
[id_pc] => 2
[pc_date] => 2012-11-21
[pc_time_block] => 10:00 AM-2:00 PM
[pc_time_block_max] => 25
[pc_time_block_count] => 0
[pc_name] => Atlanta
)
[3] => Array
(
[id_pc_time_blocks] => 6
[id_pc] => 2
[pc_date] => 2012-11-23
[pc_time_block] => 10:00 AM-2:00 PM
[pc_time_block_max] => 25
[pc_time_block_count] => 0
[pc_name] => Atlanta
)
[4] => Array
(
[id_pc_time_blocks] => 6
[id_pc] => 2
[pc_date] => 2012-11-23
[pc_time_block] => 3:00 AM-6:00 PM
[pc_time_block_max] => 25
[pc_time_block_count] => 0
[pc_name] => Atlanta
)
)
Not recommended for HUGE arrays, but here's a quick solution. You need to break hte times into unix time stamps for comparisson
// Run down each element of the array. (I've called it MyStartArray)
$numElements = count($MyStartArray);
for ($i=0; $i<$numElements ; $i++) {
// Calculate "Start Time" and "End Time" as Unix time stamps (use mktime function) and store as another items in the array
// You can use preg_match or substr to get the values to pump into mktime() below - not writing hte whole thing for you ;)
$MyStartArray[$i]['start_time'] = mktime( ... );
$MyStartArray[$i]['end_time'] = mktime( ... );
// Now run through all the previous elements to see if a start time is before the end time, or an end time is after the start time.
if ($i > 0) {
for ($j=0; $j<$i;$j++) {
if ($MyStartArray[$i]['start_time'] < $MyStartArray[$j]['end_time'] ||
$MyStartArray[$j]['end_time'] > $MyStartArray[$j]['start_time'] ) {
echo 'CLASH';
}
}
}
}
Here is my solution for checking each date for overlapping times. The only thing is, this is for my particular scenario and does not account for overlapping years as I dont have that need for this application.
Here is my working example:
$dateIdx = 0;
foreach($timeblocks_array as $obj) {
$timeblocks_array[$dateIdx]["intDay"] = idate("z",strtotime($obj["pc_date"]));
$timeblocks_array[$dateIdx]["intStart"] = intval($obj["start_time"]);
$timeblocks_array[$dateIdx]["intEnd"] = intval($obj["end_time"]);
$mindates[] = idate("z",strtotime($obj["pc_date"]));
$dateIdx++;
}
$minDateSingle = min($mindates);
$maxDateSingle = max($mindates);
$currentDate = $minDateSingle;
$dateIdx = 0;
while ($currentDate <= $maxDateSingle) {
$hrIndex = 0;
while ($hrIndex < 24) {
$matrixArray[$dateIdx][$hrIndex]["count"] = 0;
$matrixArray[$dateIdx][$hrIndex]["intDay"] = $currentDate;
$hrIndex++;
}
// calculate counts:
$hourIdx = 0;
foreach($matrixArray[$dateIdx] as $hour){
foreach($timeblocks_array as $block) {
if ($hour["intDay"] == $block["intDay"]) {
if ($hourIdx >= $block["intStart"] && $hourIdx < $block["intEnd"]) {
$matrixArray[$dateIdx][$hourIdx]["count"] = $matrixArray[$dateIdx][$hourIdx]["count"] + 1;
$matrixArray[$dateIdx][$hourIdx]["requests"][] = $block;
}
}
}
$hourIdx++;
}
$dateIdx++;
$currentDate = $currentDate + 1;
}
//loop through the matrix array and timeblocks array to see if they intersect
foreach($matrixArray as $day) {
$hourIdx = 0;
foreach($day as $hour) {
if ($hour["count"] > 1) {
//echo $hour["intDay"]." - Overlap on Hour $hourIdx\n";
$smarty->assign('overlappingError', 1);
$error = 1;
foreach($hour["requests"] as $overlapblock) {
//echo " --> conflict: ". $overlapblock["pc_date"]." ".$overlapblock["pc_time_block"]." (".$overlapblock["intStart"]." to ".$overlapblock["intEnd"].")\n";
}
} else if ($hour["count"] == 1) {
// these are valid hours
}
$hourIdx++;
}
}
Robbie's answer did not work for me. I found the rules that needed to be in place going off his example for just dates were:
$i[start] needs to be less than and not equal to $i[stop]
$i[start] needs to greater than and not equal to $j[stop]
$j[start] needs to be less than and not equal to $j[stop]
Therefore my solution was:
$numElements = count($dates);
for ($i=0; $i<$numElements; $i++) {
$dates[$i]['start_time'] = strtotime($dates[$i]['start']);
$dates[$i]['end_time'] = strtotime($dates[$i]['end']);
if ($i > 0) {
for ($j=0; $j<$i;$j++) {
if($dates[$i]['start_time'] >= $dates[$i]['end_time'] || $dates[$i]['start_time'] <= $dates[$j]['end_time'] || $dates[$j]['start_time'] >= $dates[$j]['end_time']) {
$this->set_error(['dates_overlap']);
$this->dates_overlap = true;
break;
}
}
}
if(isset($this->dates_overlap))
break;
}

Categories