Howdy all,

New here. And I've done a search. Just can't find what I'm looking for.

I have a number in some text that I want to increment every week by 100 . And, i need it done automagically.

I have thought initially of putting the number in a DB table and have a cron update it every friday but, that just seems to maybe be a bit of an overkill.

Any ideas?

Thanks in advance.

Dani AI

Generated

Short, practical options that avoid surprises.

If exact weekly ticks are required even when traffic is low, schedule a weekly job that updates one stored value. Make that job idempotent: compute how many full weeks have passed since the last update and add 100 * weeks_delta in a single SQL statement so missed runs don't double-apply. For reliability use an atomic UPDATE (InnoDB/transaction) and log the run outcome.

If you prefer zero scheduled jobs, store a base price and a start date and compute the current price on each page view. That is simple, fully deterministic, and scales well if you add a small cache (transient or memcached) so you do the date math only occasionally.

Example compute-on-read (UTC safe):

$base = 1000; // base price in dollars (or cents if you prefer)
$start = new DateTime('2020-01-01', new DateTimeZone('UTC'));
$now   = new DateTime('now', new DateTimeZone('UTC'));
$weeks = floor(($now->getTimestamp() - $start->getTimestamp()) / (7*24*60*60));
$price = $base + 100 * $weeks;

Example idempotent SQL update for a weekly cron:

UPDATE my_price
SET amount = amount + 100 * GREATEST(0, FLOOR(TIMESTAMPDIFF(DAY, last_updated, UTC_TIMESTAMP()) / 7)),
    last_updated = UTC_TIMESTAMP()
WHERE id = 1;

Notes and gotchas: always use server (UTC) time as advised; if traffic is low, WordPress wp_cron is unreliable—use system cron to call wp-cron.php or hook into wp_schedule_event but ensure the site receives the actual trigger. As mentioned, choose a DB for high-read sites; if you write weekly only, writes are tiny but reads can be cached. If using cron, test it manually, record output to a log, and make updates idempotent so one missed run is harmless.

Recommended Answers

All 6 Replies

You could store the number in either a txt file (if you don't want to do the mysql connection thing just for it) or in a db and also store the date/time for which that number "expires" and it should be incremented. Then every time a user views the page you can do a check to see if the time is past the time in the file and if so, increment the number before displaying it to the user.

You want to make sure you use server time through and not the time on the users computer.

Generally when you want to automate a PHP script, be it big or small you run it in a CRON job, so that's what I'd recommend but I'm curious to see what any other peoples input will be!

how are you going to use this number? after i know this i can tell you which solution (database, txt file, ect.) would be better.

Thanks for all the quick responses.

This is basically a price that goes up by $100 every week. So, it is just getting displayed on a page.

if its something that gets repeated viewings on a page, you will need something with good performance like a database. file reading is pretty resource intensive, but you might not have a problem if its a small file.

pretty much, this one up to you. i prefer a database solution because its quicker and in my opinion easier to update and work with in general. then i would use a cron job to increment the number in the database by 100 every week.

Thank you. I have gone with a db solution with a cron. Then, I had to figure out how to put it in a wordpress doc. dynamically. But, managed that too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.