I want to know a nice way to check which element in an array is the leading one (has priority) by its begin/end date.
For example, here is the array:
(
[0] => Array
(
[ID] => 16
[begin_date] => DateTime Object
(
[date] => 2016-01-01
[timezone_type] => 3
[timezone] => UTC
)
[end_date] => DateTime Object
(
[date] => 2016-11-30
[timezone_type] => 3
[timezone] => UTC
)
)
[1] => Array
(
[ID] => 33
[begin_date] => DateTime Object
(
[date] => 2015-01-04
[timezone_type] => 3
[timezone] => UTC
)
[end_date] => DateTime Object
(
[date] => 2016-02-29
[timezone_type] => 3
[timezone] => UTC
)
)
)
In this case the second element has priority. Because its begin_date is earlier than the begin_date of the first element. And, its end_date is still in the future.
Conditions:
If the begin_date is in the future, it cannot be the leading element
If the end_date has expired (if today > end_date), the leading
element must be the one with the latest end_date
Thanks in advance!
uasort($memberArray,function($a,$b){
$c = $a['begin_date']['date'] - $b['begin_date']['date';
$c .= $a['overall']['PD'] - $b['overall']['PD'];
$c .= $a['overall']['PF'] - $b['overall']['PF'];
$c .= $a['overall']['W'] - $b['overall']['W'];
$c .= $a['overall']['MP'] - $b['overall']['MP'];
return $c;
});
Related
When using print_r($myArray, true) for one of my arrays I get:
Array
(
[id] => 14
[name] => My Name
[*createdAt] => Carbon\Carbon Object
(
[date] => 2020-03-18 10:00:26.000000
[timezone_type] => 3
[timezone] => UTC
)
[*updatedAt] => Carbon\Carbon Object
(
[date] => 2020-08-26 10:10:10.000000
[timezone_type] => 3
[timezone] => UTC
)
)
What is the meaning of the asterisk in [*createdAt] and [*updatedAt]?
Also, I cannot access these fields. How can I access them?
The asterisk indicates an Object. And it is protected. This is the reason why you can't access them.
I have a tried numerous examples on stack already and I cant seem to get this right! I am trying to pull the date from this DateTime Object also the reason for the two lines of time was because I was trying different methods and compare the return.
[created] => DateTime Object
(
[date] => 2019-06-06 15:22:25.720000
[timezone_type] => 1
[timezone] => +00:00
)
but for some reason i'm getting the completely wrong date and time.
19-09-04 22:37:18
1970-01-01 01:00:00
19-09-04 22:37:18
1970-01-01 01:00:00
19-09-04 22:37:18
1970-01-01 01:00:00
19-09-04 22:37:18
1970-01-01 01:00:00
I've tried the following:
JSON Array to PHP DateTime
PHP date time from string
Arrays - foreach brings ->Fatal error: Cannot use object of type
PHP CODE:
<?php
$vPaccomments = $issue->fields->comment->comments;
foreach ($vPaccomments as $vPaccomment) {
$vPacAvatarUrl = $vPaccomment->author->avatarUrls;
$size16 = "16x16";
$vPacCreated = $vPaccomment->created;
$vPacDate = $vPacCreated->date;
$vPacDateConv = date('Y-m-d H:i:s',strtotime($vPacDate));
$dateTime = new DateTime($vPacDate);
echo $dateTime->format('y-m-d H:i:s');
echo '<pre>'; print_r($vPacDateConv);
echo "<div class=\"col\">";
//echo "<img width=\"16px\" src=\"" . $vPacAvatarUrl[$size16] . "\"> " . "<font color=\"#0015ff\">" .$vPaccomment->author->displayName ."</font> added a comment - " . date_format($vPacCreateDate, 'Y-m-d H:i:s');
echo "<br>";
echo "<hr>";
echo "</div>";
}
?>
If I echo '<pre>'; print_r($vPacCreated);
I get the correct results i want to grab
DateTime Object
(
[date] => 2019-06-06 15:22:25.720000
[timezone_type] => 1
[timezone] => +00:00
)
DateTime Object
(
[date] => 2019-06-07 13:58:31.970000
[timezone_type] => 1
[timezone] => +00:00
)
DateTime Object
(
[date] => 2019-06-17 14:07:23.040000
[timezone_type] => 1
[timezone] => +00:00
)
DateTime Object
(
[date] => 2019-06-17 14:25:13.840000
[timezone_type] => 1
[timezone] => +00:00
)
Here is the portion of the values:
[comment] => JiraRestApi\Issue\Comments Object
(
[startAt] => 0
[maxResults] => 4
[total] => 4
[comments] => Array
(
[0] => JiraRestApi\Issue\Comment Object
(
[self] => https://xxx/rest/api/2/xxx
[id] => xxx
[author] => JiraRestApi\Issue\Reporter Object
(
[self] => https://xxx/rest/api/2/xxx
[name] => xxx
[emailAddress] => xxx
[avatarUrls] => Array
(
[48x48] => https://xxx/secure/useravatar?avatarId=xxx
[24x24] => https://xxx/secure/useravatar?size=small&avatarId=xxx
[16x16] => https://xxx/secure/useravatar?size=xsmall&avatarId=xxx
[32x32] => https://xxx/secure/useravatar?size=medium&avatarId=xxx
)
[displayName] => xxx
[active] => 1
[wantUnassigned:JiraRestApi\Issue\Reporter:private] =>
[accountId] =>
[key] => xxx
[timeZone] => America/New_York
)
[body] => xxx
CREATED ON:6/6/2019
COPIED TO:\\xxx
[updateAuthor] => JiraRestApi\Issue\Reporter Object
(
[self] => https://xxx/rest/api/2/xxx
[name] => xxx
[emailAddress] => xxx
[avatarUrls] => Array
(
[48x48] => https://xxx/secure/useravatar?avatarId=xxx
[24x24] => https://xxx/secure/useravatar?size=small&avatarId=xxx
[16x16] => https://xxx/secure/useravatar?size=xsmall&avatarId=xxx
[32x32] => https://xxx/secure/useravatar?size=medium&avatarId=xxx
)
[displayName] => xxx
[active] => 1
[wantUnassigned:JiraRestApi\Issue\Reporter:private] =>
[accountId] =>
[key] => xxx
[timeZone] => America/New_York
)
[created] => DateTime Object
(
[date] => 2019-06-06 15:22:25.720000
[timezone_type] => 1
[timezone] => +00:00
)
Judging from your input data, it looks like $vPacCreated is already a DateTime object, so you should be able to just
echo $vPacCreated->format('Y-m-d H:i:s');
I have array in PHP which contains a date and the amount of days I want to add to that date. As I am looping over these dates and adding the specified amount, the [date] values on each loop keeps getting incremented with each pass of the loop. Here is my array...
Array
(
[0] => Array
(
[date] => DateTime Object
(
[date] => 2017-10-10 06:00:00.000000
[timezone_type] => 2
[timezone] => GMT
)
[daysToAdd] => 3
)
[1] => Array
(
[date] => DateTime Object
(
[date] => 2017-10-10 06:00:00.000000
[timezone_type] => 2
[timezone] => GMT
)
[daysToAdd] => 5
)
[2] => Array
(
[date] => DateTime Object
(
[date] => 2017-10-10 06:00:00.000000
[timezone_type] => 2
[timezone] => GMT
)
[daysToAdd] => 10
)
)
I am passing all of this data to a function to process it...
->processDates($data);
Inside of my PHP function...
foreach($array['dates'] as $day){
$originalDateTime = $day['date']; # original dateTime
$dateTime = $day['date']; # the dateTime as a new var
$dateTime = $dateTime->modify('+'.$day['daysToAdd'].' days'); // modify the dateTime
print_r([
'originalDate'=>$originalDateTime,
'newDate'=>$dateTime,
]);
}
This is what happens when the first element is ran inside of the function...
!!! The original date is also incremented by the value passed when I added it back to the array, even though i set it as a completely unmodified variable inside of the function !!!
Array
(
[originalDate] => DateTime Object
(
[date] => 2017-10-13 06:00:00.000000
[timezone_type] => 2
[timezone] => GMT
)
[newDate] => DateTime Object
(
[date] => 2017-10-13 06:00:00.000000
[timezone_type] => 2
[timezone] => GMT
)
)
This is what happens when the second element is ran inside of the function...
Array
(
[originalDate] => DateTime Object
(
[date] => 2017-10-18 06:00:00.000000
[timezone_type] => 2
[timezone] => GMT
)
[newDate] => DateTime Object
(
[date] => 2017-10-18 06:00:00.000000
[timezone_type] => 2
[timezone] => GMT
)
)
And this is what happens on the 3rd iteration...
Array
(
[originalDate] => DateTime Object
(
[date] => 2017-10-28 06:00:00.000000
[timezone_type] => 2
[timezone] => GMT
)
[newDate] => DateTime Object
(
[date] => 2017-10-28 06:00:00.000000
[timezone_type] => 2
[timezone] => GMT
)
)
Each time I loop over the array the original datetime gets messed up by the ->modify. How can I prevent this so that each loop completely gets reset and uses the actual data passed in. I need the ['originalDate'] to maintain as originally set as "2017-10-10 06:00:00.000000"...
Thanks.
I would like to create an array which consists of ascending date objects. I tried the following code:
$dat=date_create_from_format("Y-m-d H:i:s", "2014-11-01 00:00:00");
for ($i=0; $i<=2; $i++) {
$ar[$i]=$dat;
$dat->modify('+1 day');
}
print_r($ar);
The result is three times the same date:
Array
(
[0] => DateTime Object
(
[date] => 2014-11-04 00:00:00.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
[1] => DateTime Object
(
[date] => 2014-11-04 00:00:00.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
[2] => DateTime Object
(
[date] => 2014-11-04 00:00:00.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
)
But what I would like to get is:
Array
(
[0] => DateTime Object
(
[date] => 2014-11-01 00:00:00.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
[1] => DateTime Object
(
[date] => 2014-11-02 00:00:00.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
[2] => DateTime Object
(
[date] => 2014-11-03 00:00:00.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
)
Someone has an idea? Probably a newbie-thing ;-)
As $dat is an obejct, all items in $ar store references to this object. So, when this object changes ($dat->modify()), all references immediately see this changes. To create different object, you can clone source object:
$dat = date_create_from_format("Y-m-d H:i:s", "2014-11-01 00:00:00");
for ($i = 0; $i <= 2; $i++) {
$ar[$i] = clone $dat;
$dat->modify('+1 day');
}
print_r($ar);
Here clone operator takes you current $dat object and makes a copy of it. So when you change $dat again, copy doesn't know about it.
$className = "Class B: Wednesday 6pm";
// $studentArray is multidimensional array with student info
foreach($studentArray as $student) {
echo array_search($className,$student);
}
// Contents of $student is
Array
(
[Groups] => 187,267
[Birthday] => DateTime Object
(
[date] => 1981-02-04 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[_IGTestScore] => 0
[Email] => blank#blank.com
[_IGClass1] => Class B: Wednesday 6pm
[_IGAttendedClass1] => DateTime Object
(
[date] => 2016-02-17 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[FirstName] => Joe
[Id] => 3
[LastName] => Schmoe
)
The output of that is:
_IGTestScore
If I do a var_dump on $className and $student['_IGClass1] I get:
string(22) "Class B: Wednesday 6pm"
string(22) "Class B: Wednesday 6pm"
Never had an issue with array_search before, but this has been driving me crazy, and can't seem to figure out what is going on here. It seems that any string search is coming out the same. But if I do a search for an integer, like 3, it will pull out "Id" correctly.
UPDATE - whole multi-dimensional array
Array
(
[0] => Array
(
[Groups] => 187,267
[Birthday] => DateTime Object
(
[date] => 1981-02-04 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[_IGTestScore] => 0
[Email] => blank#blank.com
[_IGClass1] => Class B: Wednesday 6pm
[_IGAttendedClass1] => DateTime Object
(
[date] => 2016-02-17 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[FirstName] => Joe
[Id] => 3
[LastName] => Schmoe
)
)
I have found the problem... but I don't know the reason: for me, it is a mystery!
The problem is _IGTestScore as integer: if _IGTestScore is an integer, array_search() returns _IGTestScore ( In fact it returns _IGTestScore for any searchstring, even if they doesn't exists! ), if _IGTestScore is set to '0' (string) or to a positive integer, array_search() returns correct value!
It's a bug?
Someone have an explanation?
Edit:
Obviously, there is a solution: use the “strict” parameter:
array_search( $className, $student, True );
But... Why “Class B: Wednesday 6pm” == 0?