Date/Time Object from JSON to PHP Foreach Loop - php

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');

Related

How to display 'created_time' from facebook api response using php sdk 5

I'm using php sdk 5 reading all post from my page using the Facebook API.
My problem is just to echo the created_time from the response data to no avail.
try {
// Get a list of pages with you as admin
$posts_request = $fb->get('/'.$fbpageid.'/feed?fields=id,message,created_time');
} catch (FacebookRequestException $e) {
echo 'Request error: ' . $e->getMessage();
exit;
}
And here a sample of the response data:
Array (
[0] => Array (
[id] => 2072715452753496_2089871617704546
[message] => sample of post number one
[created_time] => DateTime Object (
[date] => 2018-02-15 02:39:28.000000
[timezone_type] => 1
[timezone] => +00:00
)
)
[1] => Array (
[id] => 2072715452753496_2082500291775012
[message] => sample post number two
[created_time] => DateTime Object (
[date] => 2018-02-10 10:24:53.000000
[timezone_type] => 1
[timezone] => +00:00
)
)
[2] => Array (
[id] => 2072715452753496_2072717852753256
[created_time] => DateTime Object (
[date] => 2018-02-03 09:56:16.000000
[timezone_type] => 1
[timezone] => +00:00
)
)
[3] => Array (
[id] => 2072715452753496_2072717476086627
[created_time] => DateTime Object (
[date] => 2018-02-03 09:55:46.000000
[timezone_type] => 1 [timezone] => +00:00
)
)
)
try this :
try {
// Get a list of pages with you as admin
$posts_request = $fb->get('/'.$fbpageid.'feed?fields=id,message,created_time');
foreach ($posts_request as $key => $value){
echo $value['created_time']->format('Y-m-d'); // format your DateTime object
}
}
this could https://paulund.co.uk/datetime-php

PHP DateTime in Object Doesn't Maintain Original Value In Loop

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.

How to access this variable

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

PHP ActiveRecord in CodeIgniter and mysql datetime field

I just added PHP ActiveRecord to my CodeIgniter installation. I'm running a few tests now and am very confused by a few things. I have a products table in mysql, and when I use ActiveRecord to retrieve the first row in the table, this is the output I get:
Product Object
(
[errors] =>
[attributes:ActiveRecord\Model:private] => Array
(
[id] => 1
[ext_id] => 347
[ext_type_id] => 1
[sku] => SYB-RIU-SUP
[url] => http://domain.com
[name] => SUP Wall Storage Rack
[thumbnail_url] => https://thumburl.com
[sort_order] => 20
[profitability] => 40
[last_google_update] => ActiveRecord\DateTime Object
(
[model:ActiveRecord\DateTime:private] => Product Object
*RECURSION*
[attribute_name:ActiveRecord\DateTime:private] => last_google_update
[date] => 2014-05-21 05:01:12
[timezone_type] => 3
[timezone] => UTC
)
[last_internal_update] =>
[discontinued] => 0
[stock_notes] =>
[deleted] => 0
[created_on] => ActiveRecord\DateTime Object
(
[model:ActiveRecord\DateTime:private] => Product Object
*RECURSION*
[attribute_name:ActiveRecord\DateTime:private] => created_on
[date] => 2014-03-25 22:31:58
[timezone_type] => 3
[timezone] => UTC
)
)
[__dirty:ActiveRecord\Model:private] => Array
(
)
[__readonly:ActiveRecord\Model:private] =>
[__relationships:ActiveRecord\Model:private] => Array
(
)
[__new_record:ActiveRecord\Model:private] =>
)
What do the *RECURSION* lines mean in the object? Is that expected or is that a problem? Also, let's say I need to access the last_google_update field, I do the following:
$product = Product::first();
$date = $product->last_google_update;
print_r($date);
This outputs the following:
ActiveRecord\DateTime Object
(
[model:ActiveRecord\DateTime:private] => Product Object
(
[errors] =>
[attributes:ActiveRecord\Model:private] => Array
(
[id] => 1
[ext_id] => 347
[ext_type_id] => 1
[sku] => SYB-RIU-SUP
[url] => http://www.storeyourboard.com/sup-storage-rack.html
[name] => SUP Wall Storage Rack
[thumbnail_url] => https://cdn2.bigcommerce.com/server2100/6p8lmnk/products/347/images/2292/standup_paddle_board_rack__43491.1392317669.225.360.jpg?c=2
[sort_order] => 20
[profitability] => 40
[last_google_update] => ActiveRecord\DateTime Object
*RECURSION*
[last_internal_update] =>
[discontinued] => 0
[stock_notes] =>
[deleted] => 0
[created_on] => ActiveRecord\DateTime Object
(
[model:ActiveRecord\DateTime:private] => Product Object
*RECURSION*
[attribute_name:ActiveRecord\DateTime:private] => created_on
[date] => 2014-03-25 22:31:58
[timezone_type] => 3
[timezone] => UTC
)
)
[__dirty:ActiveRecord\Model:private] => Array
(
)
[__readonly:ActiveRecord\Model:private] =>
[__relationships:ActiveRecord\Model:private] => Array
(
)
[__new_record:ActiveRecord\Model:private] =>
)
[attribute_name:ActiveRecord\DateTime:private] => last_google_update
[date] => 2014-05-21 05:01:12
[timezone_type] => 3
[timezone] => UTC
)
So the get the actual date, I tried this:
print_r($date->date);
However, this gives the following error:
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>A PHP Error was encountered</h4>
<p>Severity: Notice</p>
<p>Message: Undefined property: ActiveRecord\DateTime::$date</p>
<p>Filename: controllers/test.php</p>
<p>Line Number: 16</p>
</div>
But, if I include a call to get_active_vars() first, it works:
print_r(get_object_vars($date))
print_r($date->date);
Outputs:
Array
(
[date] => 2014-05-21 05:01:12
[timezone_type] => 3
[timezone] => UTC
)
2014-05-21 05:01:12
To summarize:
Are the *RECURSION* flags expected?
How do I work with datetime fields and how do I access the actual dates?
RECURSION is not good defenetly
I am not sure but
[model:ActiveRecord\DateTime:private] => Product Object
somehow you have product object in a dateTime so and class trying to parse it that is why recursion.
try to see configuration of the module
So solution is to use format function on the date object property.
$date->date->format('Y-m-d H:i:s')

PHP - Access MSSQL datetime column from the returned array

PHP - Access MSSQL datetime column from the returned array
Array
(
[0] => Array
(
[CallId] => 45
[CallLoginId] => 1
[CustomerId] => 140
[CallOptionId] => 2
[CallTypeId] => 2
[CallStatusId] => 1
[CallDateTime] => DateTime Object
(
[date] => 2012-06-28 00:00:00
[timezone_type] => 3
[timezone] => Asia/Kolkata
)
[ContactNo] => 45151551115
[ContactPerson] => Contact Person name
[ProductId] => 1
[ProdCompanyId] => 1
[ProdCategoryId] => 1
[ModelNo] => 451212151
[ProdUnderId] => 1
[Problem] => Simple Problem Details
[Remarks] => Remarks
[Accessories] => Accessories
[CallCaseId] =>
[CallCaseDate] =>
[ServiceCharge] => 0
[CourierName] =>
[DocketNo] =>
[CompanyId] => 126
[ASPId] => 130
[InsBy] => 134
[InsDate] => DateTime Object
(
[date] => 2012-06-23 17:04:51
[timezone_type] => 3
[timezone] => Asia/Kolkata
)
[UpdBy] => 11
[UpdDate] => DateTime Object
(
[date] => 2012-06-28 18:29:23
[timezone_type] => 3
[timezone] => Asia/Kolkata
)
[FName] => Kumar
[MName] => a
[LName] => Customer
[ProductName] => LenovoDesktop420
[CallOption] => InHouse
[CallType] => H.W.Installation
[ProdCompany] => Lenovo
[ProdCategory] => Desktop
[ProdUnder] => AMC
[CallStatus] => Open
[EntityId] => 134
[InsertBy] => Bhavin Rana
)
)
how can access date time value form this returned array ?
thanks in advance.
You have an outer array with numerically indexed one element [0], which is an associative array. CallDateTime as a key of that array is a DateTime object.
$array[0]['CallDateTime']->format('Y-m-d H:i:s') // 2012-06-28 00:00:00
$array[0]['CallDateTime']->getTimezone()->getName() // Asia/Kolkata
// Same with InsDate and UpdDate
$array[0]['InsDate']->format('Y-m-d H:i:s') // 2012-06-23 17:04:51
$array[0]['InsDate']->getTimezone()->getName() // Asia/Kolkata

Categories