-1

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 ?

  • Are you trying to achieve a human-readable difference like "11 hours ago"? Or do you want a numeric value in seconds like "39600"? – aaronbauman Jun 24 at 19:38
  • @aaronbauman actually i need both ! (1) i want a human-readable difference like "7 hours 30 minutes" so i can save the value in a text box field for users to read it. (2) i want also a numeric value in hours like 7.6 so i can multiply this value (Ex: 7.6) by another numeric value and save the results in another text box... in other words, say that a user get pay 5$ for 1 working hour, when the user will work from 24/06/2019 08:00 AM until 24/06/2019 07:15 PM so he worked 11 hours 15 minutes which means 11.25 hours and so 11.25 × 5 = 90$ – user93906 Jun 24 at 22:47
  • $creationsince is a string. Try: $minutesPassed = round(($submission->completed->value - $createdTimestamp)/60) . ' minutes';. There may be D8 methods for this, but I have not found them... – Prestosaurus Jun 26 at 16:46
0

Given a node, $node:

$diffSeconds = time() - $node->getCreatedTime();
$diffHours = (($delta_seconds / 60.0) / 60.0);
$formattedTimeDiff = \Drupal::service('date.formatter')
  ->formatTimeDiffSince($node->getCreatedTime());

$diffSeconds is the number of seconds between node creation time and now.

Thus $diffHours is the number of hours (float) between node creation time and now.

formatTimeDiffSince() converts this to a human readable value, e.g. "1 hour 15 minutes"


For the webform timestamps:

function MYMODULE_preprocess_node(&$variables) {

  $createdTimestamp = $variables['node']->getCreatedTime();

  $webform_submissions = \Drupal::entityTypeManager()
    ->getStorage('webform_submission')
    ->loadByProperties([
    'entity_type' => 'node',
    'entity_id' => $variables['node']->id(),
  ]);

  $submission_timestamps = [];
  $timeDiffs = [];
  $timeAgos = [];

  foreach ($webform_submissions as $submission) {
    $submission_timestamps[] = $submission->completed->value;
    $timeDiffs[] = $submission->completed->value - $createdTimestamp;
    $timeAgos[] = \Drupal::service('date.formatter')
      ->formatTimeDiffSince($submission->completed->value);
  }

}

Webform timestamps cover ->created, ->completed, and ->changed.

  • Can you please explain how to make the current time not static ? – user93906 Jun 25 at 12:12
  • The above is a handy D8 method, but it sounds like you want to compare two static time stamps. If I understand correctly, and this will ultimately be shown in node template, I believe you just need the node created date and the associate webform submission date. These would both be acquired in hook_preprocess_node I would assume. After that sounds like standard manipulation with math on the time values. – Prestosaurus Jun 25 at 16:56
  • Answer has been edited. Again, for the human readable created against submission time, will need a little more leg work, but that’s just formatting. – Prestosaurus Jun 26 at 3:57
  • please see my edit to the question (Update #1), I believe i am half way to the answer! – user93906 Jun 26 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy