<p>You can create a <a href="https://craftcms.com/docs/4.x/extend/commands.html#module-setup" rel="nofollow noreferrer">controller command</a> that does that and call it in set intervals from a cronjob. The controller command will find the element that's storing your number, increment the number by the set interval and then save it. Something like this:</p>
<pre class="lang-php prettyprint-override"><code>$entryWithNumberField = \craft\elements\Entry::find()
    -&gt;my_number_field(':notempty:')
    -&gt;one();
$currentNumber = $entryWithNumberField-&gt;getFieldValue('my_number_field');
$entryWithNumberField-&gt;setFieldValue('my_number_field', $currentNumber + 0.2);
\Craft::$app-&gt;getElements()-&gt;saveElement($entryWithNumberField);
</code></pre>
<p>However, this isn't a great solution as its very error prone. If the cron job fails for any reason (the server rebooting as part of a planned maintenance, for instance), the number will no longer be accurate.</p>
<p>Instead of saving the number every two hours, simply calculate the current number wherever you need to use it based on the defined starting point and the current time. This way, it will always be accurate, won't have the possibility for race conditions or be dependent on the previous state.</p>
<p>For example:</p>
<pre class="lang-php prettyprint-override"><code>$entry = Entry::find()-&gt;my_number_field(':notempty:')-&gt;one();

$offset = $entry-&gt;my_number_field;

$now = time();
$start = $entry-&gt;dateCreated-&gt;getTimestamp();
$twoHourIntervalsPassed = floor($now - $start / 3600 / 2);
$incrementBy = $twoHourIntervalsPassed * 0.2;
$final = $offset + $incrementBy;
</code></pre>

CC BY-SA 4.0
2022-05-20T20:17:03.147Z
Thank you, it seems correct, I'll give it a shot on Monday :)
loliki
CC BY-SA 4.0
2022-05-20T12:14:15.623Z
2022-05-20T12:14:15.623Z
MoritzLost
<p>I'm building a component in which the value of a number should be increased every 2 hours by 0.1.</p>
<p>What would be the best solution for that?
I have a field type number with the value 1200 by default.</p>
<p>Is there a way I can change in the dashboard this value every 2 hours? Or Should I do it in Twig and push it somehow to the Database? Is there a way to update the field from twig to DB?</p>

<craft3><database>
Increment automatically the number field
# Increment automatically the number field

(Asked by: username_0 on 2022-05-20)

I'm building a component in which the value of a number should be increased every 2 hours by 0.1.

What would be the best solution for that?
I have a field type number with the value 1200 by default.

Is there a way I can change in the dashboard this value every 2 hours? Or Should I do it in Twig and push it somehow to the Database? Is there a way to update the field from twig to DB?

**Tags:** craft3, database


---

## 1 answers

### Answer by username_1 on 2022-05-20. Score: 3 | Accepted answer

You can create a controller command that does that and call it in set intervals from a cronjob. The controller command will find the element that's storing your number, increment the number by the set interval and then save it. Something like this:
$entryWithNumberField = \craft\elements\Entry::find()
    ->my_number_field(':notempty:')
    ->one();
$currentNumber = $entryWithNumberField->getFieldValue('my_number_field');
$entryWithNumberField->setFieldValue('my_number_field', $currentNumber + 0.2);
\Craft::$app->getElements()->saveElement($entryWithNumberField);

However, this isn't a great solution as its very error prone. If the cron job fails for any reason (the server rebooting as part of a planned maintenance, for instance), the number will no longer be accurate.
Instead of saving the number every two hours, simply calculate the current number wherever you need to use it based on the defined starting point and the current time. This way, it will always be accurate, won't have the possibility for race conditions or be dependent on the previous state.
For example:
$entry = Entry::find()->my_number_field(':notempty:')->one();

$offset = $entry->my_number_field;

$now = time();
$start = $entry->dateCreated->getTimestamp();
$twoHourIntervalsPassed = floor($now - $start / 3600 / 2);
$incrementBy = $twoHourIntervalsPassed * 0.2;
$final = $offset + $incrementBy;

> **Comments:**
> **username_0**: Thank you, it seems correct, I'll give it a shot on Monday :)  

---

