Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 292421

Add a helper function: now()

$
0
0

Problem/Motivation

Whenever I need to save a date+time programmatically, I write

$now = (new \Drupal\Core\Datetime\DrupalDateTime('now', new \DateTimeZone('UTC')))->format('Y-m-d\TH:i:s');
# or
$now = \Drupal::service('date.formatter')->format(time(), 'custom', 'Y-m-d\TH:i:s', 'UTC');

Even with the help of VS Code and/or a use statement, it's quite a lot to remember and rather complicated.

Imagine if I could just ask for drupal_get_now() and get all the goodness of (new \Drupal\Core\Datetime\DrupalDateTime('now', new \DateTimeZone('UTC')))->format('Y-m-d\TH:i:s') in one fell swoop!

Setting the date field on a node would be as easy as
$order_node->set('field_paid_date', drupal_get_now());

I bet

drupal_get_now('date'); // just the date
drupal_get_now('timestamp'); // timestamp

would be useful to lots of people as well.

Steps to reproduce

Try saving a date programmatically.

My guess is that 90% of the dates my sites save programmatically are 'now'.

Proposed resolution

Place the function in the datetime.module.

Remaining tasks

Merge request 10200 needs review and tests.


Viewing all articles
Browse latest Browse all 292421

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>