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.
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');
Hi Guys :) I have something like that:
Array
(
[0] => Ideneal\OpenLoad\Entity\RemoteUploadStatus Object
(
[remoteUpload:Ideneal\OpenLoad\Entity\RemoteUploadStatus:private] => Ideneal\OpenLoad\Entity\RemoteUpload Object
(
[id:Ideneal\OpenLoad\Entity\RemoteUpload:private] => xxxx
[folderId:Ideneal\OpenLoad\Entity\RemoteUpload:private] => xxxx
)
[remoteUrl:Ideneal\OpenLoad\Entity\RemoteUploadStatus:private] => https://openload.co/embed/xxxxx
[status:Ideneal\OpenLoad\Entity\RemoteUploadStatus:private] => finished
[bytesLoaded:Ideneal\OpenLoad\Entity\RemoteUploadStatus:private] =>
[bytesTotal:Ideneal\OpenLoad\Entity\RemoteUploadStatus:private] =>
[addedDate:Ideneal\OpenLoad\Entity\RemoteUploadStatus:private] => DateTime Object
(
[date] => 2017-07-21 10:53:55.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
[lastUpdateDate:Ideneal\OpenLoad\Entity\RemoteUploadStatus:private] => DateTime Object
(
[date] => 2017-07-21 10:53:55.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
[fileId:Ideneal\OpenLoad\Entity\RemoteUploadStatus:private] => xxxx
[url:Ideneal\OpenLoad\Entity\RemoteUploadStatus:private] => https://openload.co/f/xxxx <------------ Here is what I want to write to a variable
)
)
(At the end of the code is a comment)
How to do that ?
I tried something like that: print_r($myarray[0][url:Ideneal\OpenLoad\Entity\RemoteUploadStatus:private])
and nothing.
Try to use RemoteUploadStatus object methods:
$myarray[0]->getUrl();
Ref.: https://github.com/Ideneal/OpenLoad/blob/master/src/Entity/RemoteUploadStatus.php
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.
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;
});