In summary:
On a Drupal 8 project, i am trying to programatically get the difference between two different times (current time - Node creation time)
Use case:
I have "nodeabc" already available on the site.
When a specific webform is submitted and by using a custom webform handler, I want to get the difference in minutes between current time and nodeabc creation time.
Say nodeabc was created on: 24/06/2019 08:00 AM and the webform is being submitted on: 24/06/2019 07:00 PM, so the difference between both times will be: 660 minutes.
I have already loaded the node by its nid but how to continue in achieving the above ?
UPDATE #1:
From @Prestosaurus answer, I did the below in my webform custom handler php file:
$nid = '125';
$node = \Drupal\node\Entity\Node::load($nid);
$creationtime = $node->getCreatedTime();
// Get "Time Diff Since" creation time.
$creationsince = \Drupal::service('date.formatter')->formatTimeDiffSince($creationtime);
the above code returned: 7 hours 30 minutes
which is good but then I added the below code to the above one:
$totalamount = $creationsince * 5000;
But while testing it returns a php error InvalidArgumentException: $string ("35000") must be a string.
I believe I am half way to the answer but still need to convert the $creationsince
value to 450
minutes instead of 7 hours 30 minutes
.
Is that doable ?
11.25 × 5 = 90$
$minutesPassed = round(($submission->completed->value - $createdTimestamp)/60) . ' minutes';
. There may be D8 methods for this, but I have not found them...