Get recurring dates within date interval - php

I guess I’m stuck with this problem, so I’m hoping that you can give me some ideas how I can solve this task.
I’m working on an app that allows you to get an overview about your recurring payments (I know, this stuff exists already, this is just a side project) and your expenses.
The API is nearly finished but I struggle with this section: I need to know, if a recurring payment is due within a given time span.
The recurring events (contracts) contain amongst other fields startDate, endDate, intervalType (enum [weekly, monthly, quarterly, biannually, annually])
Currently I have this as my doctrine query:
return $this->createQueryBuilder('e')
->andWhere('e.startDate >= :start')
->andWhere('e.endDate <= :end')
->andWhere('e.user = :user')
->setParameter('start', $start)
->setParameter('end', $end)
->setParameter('user', $user)
->getQuery()
->getResult();
where the parameters start and end are the frame of interval.
But how can I check if and how often the contracts are due within the selected frame?
Afaik this would only be possible by iterating over the result of the query and check if the contract intersects with the timeframe and also note how often and when it occurs during this time.
But I have no idea how I can do this.
Example Data:
[name, intervalType, startDate, endDate, amount]
rent, monthly, 2019-01-01, 2020-10-11, -500
utility, monthly, 2019-01-01, 2020-10-11, -150
salary, monthly, 2019-01-01, 2020-10-11, 1700
investment, biannually, 2019-02-10, null , 2500
food, weekly, 2019-01-01, null , -50
If I have the timeframe (2019-05-01 - 2019-05-31) of this month I would get those contracts:
rent 2019-05-01
utility 2019-05-01
salary 2019-05-01
food 2019-05-01
food 2019-05-08
food 2019-05-15
food 2019-05-22
food 2019-05-29
If I'd choose the following 2 months (2019-07-01 - 2019-08-31) I would get this:
rent 2019-07-01
rent 2019-08-01
utility 2019-07-01
utility 2019-08-01
salary 2019-07-01
salary 2019-08-01
food 2019-07-01
food 2019-07-08
food 2019-07-15
food 2019-07-22
food 2019-07-29
food 2019-08-01
food 2019-08-08
food 2019-08-15
food 2019-08-22
food 2019-08-29
investment 2019-08-01
Would this be possible with DateTime and DateInterval?

The logic with the weekly interval looks wrong to me. At least with my bank app a weekly schedule would always fall on the same weekday, with 7 days between two consecutive occurrences.
So with that logic you could use this function:
function getScheduleBetween($data, $intervalStart, $intervalEnd) {
$ref = [
"yearly" => [12, "months"],
"annually" => [12, "months"],
"biannually" => [6, "months"],
"quarterly" => [3, "months"],
"monthly" => [1, "months"],
"weekly" => [7, "days"],
"daily" => [1, "days"]
];
$intervalStart = new DateTime($intervalStart);
$intervalEnd = new DateTime($intervalEnd);
$result = [];
foreach($data as $schedule) {
// Convert start/end to DateTime
$startDate = new DateTime($schedule["startDate"]);
$endDate = $schedule["endDate"] ? min(new DateTime($schedule["endDate"]), $intervalEnd) : $intervalEnd;
$name = $schedule["name"];
$interval = $schedule["intervalType"];
if (!isset($ref[$interval])) throw "Invalid interval type";
list($coeff, $interval) = $ref[$interval];
$match = clone $startDate;
if ($match < $intervalStart) {
$diff = $intervalStart->diff($match);
$count = $interval == "days" ? $diff->format("%a") : $diff->m + $diff->y * 12 + ($diff->d ? 1 : 0);
$count = ceil($count / $coeff) * $coeff;
$match->modify("+$count $interval");
}
while ($match <= $endDate) {
$temp = clone $match;
$result[] = ["name" => $name, "date" => $temp->format("Y-m-d")];
$match->modify("+$coeff $interval");
}
}
array_multisort(array_column($result, "date"), $result);
return $result;
}
Example use:
$data = [
["name" => "rent", "intervalType" => "monthly", "startDate" => "2019-01-01", "endDate" => "2020-10-11", "amount" => -500],
["name" => "utility", "intervalType" => "monthly", "startDate" => "2019-01-01", "endDate" => "2020-10-11", "amount" => -150],
["name" => "salary", "intervalType" => "monthly", "startDate" => "2019-01-01", "endDate" => "2020-10-11", "amount" => 1700],
["name" => "investment", "intervalType" => "biannually", "startDate" => "2019-02-10", "endDate" => null, "amount" => 2500],
["name" => "food", "intervalType" => "weekly", "startDate" => "2019-01-01", "endDate" => null, "amount" => -50],
];
$result = getScheduleBetween($data, "2019-05-01", "2019-05-31");
print_r($result);
With the larger period:
$result = getScheduleBetween($data, "2019-05-01", "2019-08-31");
print_r($result);
If you have other types of intervals I believe you can easily extend this function to support them, even with the other logic of "weekly".

Related

Failed asserting two arrays are equal but shows that the arrays are the same with no difference?

I'm trying to pass a test that involves running a query that returns a series of logins to test whether two arrays are equal in the test.
In the past I have tried changing the format of the query to make the test pass as well as editing the arrays and eventually it equalled the two arrays. Unfortunately the test still doesn't pass.
The function that performs the query to get a series of dates of logins:
public function getLogins(): array
{
return $this->createQuery()
->select('date AS datetime, COUNT(id) as total')
->orderBy('datetime', 'DESC')->groupBy('datetime')
->where('date >= -24 hours')
->getQuery()
->getResult();
}
This is the method in the test class:
public function testGetLogins()
{
$dateLogins = $this->repository->getLogins();
$this->assertCount(4, $dateLogins, "Four instances");
$this->assertEquals([
["datetime" => new \DateTime("now -3 minutes"), "total" => "1"],
["datetime" => new \DateTime("now -7 days"), "total" => "1"],
["datetime" => new \DateTime("now -1 year"), "total" => "1"],
["datetime" => new \DateTime("now -600 days"), "total" => "1"]
], $logins, "The login instances returned match the expected times");
}
I'm expecting the test to pass but instead it is displaying this:
Test Output
The expected and actual arrays are both equal so I'm unsure as to what is causing the test to fail.
\DateTime format contains information about seconds as well. new \DateTime("now -3 minutes") will return now minus 3 minutes but exact amount of seconds, which will be always different, depending on the time when you did launch the test. Apparently you want to compare dates till minutes, so you have to format your dates before comparsion, therefore you have to compare each set separately:
$expectedValues = [
["datetime" => new \DateTime("now -3 minutes"), "total" => "1"],
["datetime" => new \DateTime("now -7 days"), "total" => "1"],
["datetime" => new \DateTime("now -1 year"), "total" => "1"],
["datetime" => new \DateTime("now -600 days"), "total" => "1"]
];
for ($i = 0; $i < count($expectedValues); ++$i) {
$actualDate = (new \DateTime($logins[$i]['datetime']))->format('Y-m-d H:i');
$expectedDate = ($expectedValues[$i]['datetime'])->format('Y-m-d H:i');
$this->assertEquals($expectedDate, $actualDate);
$this->assertEquals($expectedValues[$i]['total'], $logins[$i]['total']);
}

How to override an array of overlaping timeslots

So I need to create a sort of day-per-day calendar with available times, in order for a user to be able to book a meeting with one doctor from a cabinet of multiple doctors.
I hope that this explanation is not too weird already..
Btw I use Laravel 5.5
Here's an example:
Default Schedule of the cabinet : 9:00 to 19:00
Doctor 1 says that on monday, he'll be only available from 13:00 to 15:00
Doctor 2 says that on monday, he'll be only available from 10:00 to 14:00
When I query the available timeslots :
$ids = Doctor::all()->pluck('id');
$workingSchedules = WorkingSchedule::whereIn('user_id', $ids)
->orderBy('start_date')
->whereDate('start_date', '=', $this->datetime->format('Y-m-d'))
->get();
I get:
0 => [
"start_date" => "2017-09-18 10:00:00"
"end_date" => "2017-09-18 14:00:00"
]
1 => [
"start_date" => "2017-09-18 13:00:00"
"end_date" => "2017-09-18 15:00:00"
]
And if nothing shows up from the Database then I use the default cabinet hours.
Then I use Carbon diffInMinutes() method to construct an array of 30 minutes timeslots between those date range (that the user can select).
Anyway, for my script to work correcty I need to transform the result I showed you into this:
0 => [
"start_date" => "2017-09-18 10:00:00"
"end_date" => "2017-09-18 15:00:00"
]
As I only have two timeslots in this example it might be simple a solution, but I might also get an array of 10 timeslots that overlapse one another..
Can somebody help me find a elegant solution that will cover all possible case ?
Thanks a lot in advance.
To be easier, I will suppose $workingSchedules is an array of numbers, then we can easily compare elements
$workingSchedules = [
[
'start_date' => 1,
'end_date' => 5,
],
[
'start_date' => 13,
'end_date' => 16,
],
[
'start_date' => 16,
'end_date' => 17,
],
];
$result = [$workingSchedules[0]];
$index = 0;
foreach ($workingSchedules as $row) {
if ($result[$index]['end_date'] >= $row['start_date']) {
$result[$index]['end_date'] = max($result[$index]['end_date'], $row['end_date']);
} else {
$index++;
$result[] = $row;
}
}
var_dump($result);
Above code will print:
[
[
'start_date' => 1,
'end_date' => 5,
],
[
'start_date' => 13,
'end_date' => 17,
],
]
You can custom the code to compare 2 dates instead numbers
If $workingSchedules is empty, we can simply return default schedule
To merge overlapping time-periods, you could use this code:
$result = [];
$i = -1;
foreach ($workingSchedules as $row) {
if ($i < 0 || $row["end_date"] > $result[$i]["end_date"]) {
if ($i >= 0 && $row["start_date"] <= $result[$i]["end_date"]) {
$result[$i]["end_date"] = $row["end_date"];
} else {
$result[++$i] = $row;
}
}
}
$result will then have non-overlapping periods only.
I hope this will help.
$workingSchedules=array(
0=>array(
"start_date" => "2017-09-18 10:00:00",
"end_date" => "2017-09-18 14:00:00"),
1=>array(
"start_date" => "2017-09-18 13:00:00",
"end_date" => "2017-09-18 15:00:00"
)
);
foreach ($workingSchedules as $schedule){
$start=new DateTime($schedule['start_date']);
$end=new DateTime($schedule['end_date']);
while ($start<=$end){
echo $start->format('Y-m-d H:i')."<br/>";
$start=$start->add(new DateInterval('PT'.'30'.'M'));
}
}

PHP Determine when multiple(n) datetime ranges overlap each other

I'm having a hell of a time trying to solve the following problem:
It's a calendar program where given a set of available datetime sets from multiple people, I need to figure out what datetime ranges everyone is available in PHP
Availability Sets:
p1: start: "2016-04-30 12:00", end: "2016-05-01 03:00"
p2: start: "2016-04-30 03:00", end: "2016-05-01 03:00"
p3: start: "2016-04-30 03:00", end: "2016-04-30 13:31"
start: "2016-04-30 15:26", end: "2016-05-01 03:00"
I'm looking for a function that I can call that will tell me what datetime ranges all (p) people are available at the same time.
In the above example the answer should be:
2016-04-30 12:00 -> 2016-04-30 13:31
2016-04-30 15:26 -> 2016-05-01 03:00
I did find this similar question and answer
Datetime -Determine whether multiple(n) datetime ranges overlap each other in R
But I have no idea what language that is, and have to unable to translate the logic in the answer.
Well that was fun. There's probably a more elegant way of doing this than looping over every minute, but I don't know if PHP is the language for it. Note that this currently needs to manage the start and end times to search separately, although it would be fairly trivial to calculate them based on the available shifts.
<?php
$availability = [
'Alex' => [
[
'start' => new DateTime('2016-04-30 12:00'),
'end' => new DateTime('2016-05-01 03:00'),
],
],
'Ben' => [
[
'start' => new DateTime('2016-04-30 03:00'),
'end' => new DateTime('2016-05-01 03:00'),
],
],
'Chris' => [
[
'start' => new DateTime('2016-04-30 03:00'),
'end' => new DateTime('2016-04-30 13:31')
],
[
'start' => new DateTime('2016-04-30 15:26'),
'end' => new DateTime('2016-05-01 03:00')
],
],
];
$start = new DateTime('2016-04-30 00:00');
$end = new DateTime('2016-05-01 23:59');
$tick = DateInterval::createFromDateString('1 minute');
$period = new DatePeriod($start, $tick, $end);
$overlaps = [];
$overlapStart = $overlapUntil = null;
foreach ($period as $minute)
{
$peopleAvailable = 0;
// Find out how many people are available for the current minute
foreach ($availability as $name => $shifts)
{
foreach ($shifts as $shift)
{
if ($shift['start'] <= $minute && $shift['end'] >= $minute)
{
// If any shift matches, this person is available
$peopleAvailable++;
break;
}
}
}
// If everyone is available...
if ($peopleAvailable == count($availability))
{
// ... either start a new period...
if (!$overlapStart)
{
$overlapStart = $minute;
}
// ... or track an existing one
else
{
$overlapUntil = $minute;
}
}
// If not and we were previously in a period of overlap, end it
elseif ($overlapStart)
{
$overlaps[] = [
'start' => $overlapStart,
'end' => $overlapUntil,
];
$overlapStart = null;
}
}
foreach ($overlaps as $overlap)
{
echo $overlap['start']->format('Y-m-d H:i:s'), ' -> ', $overlap['end']->format('Y-m-d H:i:s'), PHP_EOL;
}
There are some bugs with this implementation, see the comments. I'm unable to delete it as it's the accepted answer. Please use iainn or fusion3k's very good answers until I get around to fixing it.
There's actually no need to use any date/time handling to solve this
problem. You can exploit the fact that dates in this format are in alphabetical as well as chronological order.
I'm not sure this makes the solution any less complex. It's probably less
readable this way. But it's considerably faster than iterating over every minute so you might choose it if performance is a concern.
You also get to use
every
single
array
function
out there, which is nice.
Of course, because I haven't used any date/time functions, it might not work if Daylight Savings Time or users in different time zones need dealing with.
$availability = [
[
["2016-04-30 12:00", "2016-05-01 03:00"]
],
[
["2016-04-30 03:00", "2016-05-01 03:00"]
],
[
["2016-04-30 03:00", "2016-04-30 13:31"],
["2016-04-30 15:26", "2016-05-01 03:00"]
]
];
// Placeholder array to contain the periods when everyone is available.
$periods = [];
// Loop until one of the people has no periods left.
while (count($availability) &&
count(array_filter($availability)) == count($availability)) {
// Select every person's earliest date, then choose the latest of these
// dates.
$start = array_reduce($availability, function($carry, $ranges) {
$start = array_reduce($ranges, function($carry, $range) {
// This person's earliest start date.
return !$carry ? $range[0] : min($range[0], $carry);
});
// The latest of all the start dates.
return !$carry ? $start : max($start, $carry);
});
// Select each person's range which contains this date.
$matching_ranges = array_filter(array_map(function($ranges) use($start) {
return current(array_filter($ranges, function($range) use($start) {
// The range starts before and ends after the start date.
return $range[0] <= $start && $range[1] >= $start;
}));
}, $availability));
// Find the earliest of the ranges' end dates, and this completes our
// first period that everyone can attend.
$end = array_reduce($matching_ranges, function($carry, $range) {
return !$carry ? $range[1] : min($range[1], $carry);
});
// Add it to our list of periods.
$periods[] = [$start, $end];
// Remove any availability periods which finish before the end of this
// new period.
array_walk($availability, function(&$ranges) use ($end) {
$ranges = array_filter($ranges, function($range) use($end) {
return $range[1] > $end;
});
});
}
// Output the answer in the specified format.
foreach ($periods as $period) {
echo "$period[0] -> $period[1]\n";
}
/**
* Output:
*
* 2016-04-30 12:00 -> 2016-04-30 13:31
* 2016-04-30 15:26 -> 2016-05-01 03:00
*/
A different approach to your question is to use bitwise operators. The benefits of this solution are memory usage, speed and short code. The handicap is that — in your case — we can not use php integer, because we work with large numbers (1 day in minutes is 224*60), so we have to use GMP Extension, that is not available by default in most php distribution. However, if you use apt-get or any other packages manager, the installation is very simple.
To better understand my approach, I will use an array with a total period of 30 minutes to simplify binary representation:
$calendar =
[
'p1' => [
['start' => '2016-04-30 12:00', 'end' => '2016-04-30 12:28']
],
'p2' => [
['start' => '2016-04-30 12:10', 'end' => '2016-04-30 12:16'],
['start' => '2016-04-30 12:22', 'end' => '2016-05-01 12:30']
]
];
First of all, we find min and max dates of all array elements, then we init the free (time) variable with the difference in minutes between max and min. In above example (30 minutes), we obtain 230-20=1,073,741,823, that is a binary with 30 ‘1’ (or with 30 bits set):
111111111111111111111111111111
Now, for each person, we create the corresponding free-time variable with the same method. For the first person is easy (we have only one time interval): the difference between start and min is 0, the difference between end and min is 28, so we have 228-20=268435455, that is:
001111111111111111111111111111
At this point, we update global free time with a AND bitwise operation between global free time itself and person free time. The OR operator set bits if they are set in both compared values:
111111111111111111111111111111 global free time
001111111111111111111111111111 person free time
==============================
001111111111111111111111111111 new global free time
For the second person, we have two time intervals: we calculate each time interval with know method, then we compone global person free time using OR operator, that set bits if they are set in either first or second value:
000000000000001111110000000000 12:10 - 12:16
111111110000000000000000000000 12:22 - 12:30
==============================
111111110000001111110000000000 person total free time
Now we update global free time with the same method used for first person (AND operator):
001111111111111111111111111111 previous global free time
111111110000001111110000000000 person total free time
==============================
001111110000001111110000000000 new global free time
└────┘ └────┘
:28-:22 :16-:10
As you can see, at the end we have an integer with bits set only in minutes when everyone is available (you have to count starting from right). Now, you can convert back this integer to datetimes. Fortunately, GMP extension has a method to find 1/0 offset, so we can avoid to perform a for/foreach loop through all digits (that in real case are many more than 30).
Let's see the complete code to apply this concept to your array:
$calendar =
[
'p1' => [
['start' => '2016-04-30 12:00', 'end' => '2016-05-01 03:00']
],
'p2' => [
['start' => '2016-04-30 03:00', 'end' => '2016-05-01 03:00']
],
'p3' => [
['start' => '2016-04-30 03:00', 'end' => '2016-04-30 13:31'],
['start' => '2016-04-30 15:26', 'end' => '2016-05-01 03:00']
]
];
/* Get active TimeZone, then calculate min and max dates in minutes: */
$tz = new DateTimeZone( date_default_timezone_get() );
$flat = call_user_func_array( 'array_merge', $calendar );
$min = date_create( min( array_column( $flat, 'start' ) ) )->getTimestamp()/60;
$max = date_create( max( array_column( $flat, 'end' ) ) )->getTimestamp()/60;
/* Init global free time (initially all-free): */
$free = gmp_sub( gmp_pow( 2, $max-$min ), gmp_pow( 2, 0 ) );
/* Process free time(s) for each person: */
foreach( $calendar as $p )
{
$pf = gmp_init( 0 );
foreach( $p as $time )
{
$start = date_create( $time['start'] )->getTimestamp()/60;
$end = date_create( $time['end'] )->getTimestamp()/60;
$pf = gmp_or( $pf, gmp_sub( gmp_pow( 2, $end-$min ), gmp_pow( 2, $start-$min ) ) );
}
$free = gmp_and( $free, $pf );
}
$result = [];
$start = $end = 0;
/* Create resulting array: */
while( ($start = gmp_scan1( $free, $end )) >= 0 )
{
$end = gmp_scan0( $free, $start );
if( $end === False) $end = strlen( gmp_strval( $free, 2 ) )-1;
$result[] =
[
'start' => date_create( '#'.($start+$min)*60 )->setTimezone( $tz )->format( 'Y-m-d H:i:s' ),
'end' => date_create( '#'.($end+$min)*60 )->setTimezone( $tz )->format( 'Y-m-d H:i:s' )
];
}
print_r( $result );
Output:
Array
(
[0] => Array
(
[start] => 2016-04-30 12:00:00
[end] => 2016-04-30 13:31:00
)
[1] => Array
(
[start] => 2016-04-30 15:26:00
[end] => 2016-05-01 03:00:00
)
)
3v4l.org demo
Some additional notes:
At the start, we set $tz to current timezone: we will use it later, at the end, when we create final dates from timestamps. Dates created from timestamps are in UTC, so we have to set correct timezone.
To retrieve initial $min and $max values in minutes, firstly we flat original array, then we retrieve min and max date using array_column.
gmp_sub subtract second argument from first argument, gmp_pow raise number (arg 1) into power (arg 2).
In the final while loop, we use gmp_scan1 and gmp_scan0 to retrieve each ‘111....’ interval, then we create returning array elements using gmp_scan1 position for start key and gmp_scan0 position for end key.

Plot user transactions for every day of week

In my DB there is Transaction table. This table contains information about user money transactions. My task is to generate two diagrams(graphs) showing transaction amount per day, and per month.
Example of the diagram that I should implement is displayed on the image below:
As you can see from the diagram, when there are no transactions per day, the total value should be 0.
In the database, I group my transactions by day using the following query:
select DATE_FORMAT(t.transactionDate ,"%b %d") as dayName, sum(t.amount) total from `Transaction` t, t.transactionStatus = 1 and t.user=17
group by dayName
order by t.transactionDate asc;
This is working like a charm when there are transactions for every day, but this is not working fine when there is a day without any transaction. Let's say in DB we have the following transactions:
May 1st 20$
May 1st 30$
May 1st 38$
May 2nd 20$
May 4th 100$
May 4th 50$
So no transactions on May 3rd.
When I group these transactions I get the following result:
May 1st 88$
May 2nd 20$
May 4th 150$
What I want now is to generate May 3rd 0$. Is it possible to do it directly in DB, or I have to process it using PHP?
If I have to process it using PHP, any ideas?
For plotting I'm using Chart JS library, and here is example of data input:
var areaChartData = {
labels: ["May 01", "May 02", "May 04"],
datasets: [
{
label: "Transactions",
fillColor: "rgba(210, 214, 222, 1)",
strokeColor: "rgba(210, 214, 222, 1)",
pointColor: "rgba(210, 214, 222, 1)",
pointStrokeColor: "#c1c7d1",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: [88,20,150]
},
]
};
As you can see, my idea is to use values from the database directly in the chart, where transaction date is my X-axis, and total is my Y-axis.
One approach is, to have all possible dates and values first as an PHP array:
$labels = array(
"May 1st" => 0,
"May 2nd" => 0,
"May 3rd" => 0,
"May 4th" => 0,
"May 6th" => 0,
"May 6th" => 0,
....
);
Then fill all the values from the database to their according date key in the array
...
foreach ( $rows as $row )
{
$labels[$row['date']] = $row['agg_val'];
...
Now you can iterate over the $labels array and put out the values. Those, which are not in the DB (no transactions) are initialized to zero.
Looking at your query, it will generate $result like below,
array (size=3)
0 =>
array (size=2)
'dayName' => string 'May 01' (length=6)
'total' => int 88
1 =>
array (size=2)
'dayName' => string 'May 02' (length=6)
'total' => int 20
2 =>
array (size=2)
'dayName' => string 'May 04' (length=6)
'total' => int 150
This script will prepare array for chartdata,
$begin = reset($result) ['dayName']; // 1st date in result
$end = end($result) ['dayName']; // last date in result
$begin = new DateTime($begin);
$end = new DateTime($end);
$end->modify('+1 day');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
function search($input, $day)
{
foreach($input as $k => $v)
{
if ($v['dayName'] == $day) return $k;
}
return false;
}
$chartdata = []; // array for chartdata
foreach($period as $dt) // loop for everyday from 1st to last day
{
$format = $dt->format('M d');
$flag = search($result, $dt->format('M d'));
$chartdata[$format] = is_int($flag) ? $result[$flag]['total'] : 0;
}
Now, your $chartdata contains the missing day May 03 from transactions table.
Now, use this as
labels: <?php echo json_encode(array_keys($chartdata))?>,
and
data: <?php echo json_encode(array_values($chartdata))?>

Importancy algorithm based on time

I have an array of users, as follows;
<?php
$users = array(
array(
"id" => 1,
"last_updated" => 1398933140,
"weight" => 2.0
),
array(
"id" => 2,
"last_updated" => 1398933130,
"weight" => 0
),
array(
"id" => 3,
"last_updated" => 1398933120,
"weight" => 1.0
),
array(
"id" => 4,
"last_updated" => 1398933110,
"weight" => 0
)
);
?>
I want to (asynchronously) refresh some stats on users (for which I'm using a crobjob) ordered on last_updated, in essence the user with the most stale stats.
However, I want to add weights to users and calculate that into the equation. Is it good enough to just convert the weight to some amount of seconds and substract that from the last_updated timestamp?
I know my example array has time differences of 10 seconds, but I only want to start adding the weight criteria after 3600 seconds.
// Start substracting weight
if ($timediff > 3600) {
// The longer the timediff, the heavier the weight becomes
$weight_severity = (($timediff/1000) * $weight) * SOME_CONSTANT;
// Putting the 'last_updated' back farther in the past
$substract = $timestamp - $weight_severity;
}
Is this a good 'algorithm' or will this go horribly wrong when the differences become pretty large?
At the moment I have nearly 2000 users (expected will become 10.000), so theoretically a full loop takes 2000 minutes. My concern is, will a user with a weight of 2.0 be buried under 500 'insignificant' users?
Update: I have enhanced my code a bit.
<?php
$users = array(
array(
"id" => 1,
"last_updated" => 1399281955,
"weight" => 2.0
),
array(
"id" => 2,
"last_updated" => 1399281955 - 15000,
"weight" => 0
),
array(
"id" => 3,
"last_updated" => 1399281955 - 30000,
"weight" => 1.0
),
array(
"id" => 4,
"last_updated" => 1399281955 - 45000,
"weight" => 0
)
);
$results = array();
foreach ($users as $index => $user) {
$factor = 3;
$timestamp = $user['last_updated'];
$substract = $timestamp;
// Start substracting weight
$timediff = time() - $timestamp;
if ($timediff > 3600) {
// The longer the timediff, the heavier the weight becomes
$weight_severity = pow((($timediff/1000) * $user['weight']), $factor);
// Putting the 'last_updated' back farther in the past
$substract = $timestamp - $weight_severity;
}
$users[$index]['weight_updated'] = floor($substract);
$users[$index]['timediff'] = $timediff;
$users[$index]['diff'] = $users[$index]['last_updated'] -
$users[$index]['weight_updated'];
}
echo '<pre>';
print_r($users);
usort($users, function($a, $b) {
return (($a['weight_updated'] == $b['weight_updated'])) ?
0 : ($a['weight_updated'] < $b['weight_updated']) ? -1 : 1;
});
print_r($users);
So without weights, the user IDs would be: 4,3,2,1. But with my 'algorithm' it's now 3,4,2,1. User ID 3 because of it's weight, is getting done before 4. This is with a time difference of 15000 seconds (a little over 4 hours).

Categories