DEV Community

Cover image for Finding best post time on DEV (improved version, with PHP)
Rodion Gorkovenko
Rodion Gorkovenko

Posted on

Finding best post time on DEV (improved version, with PHP)

I explained recently how we can find the hour of the day, in which more posts here at DEV are created. That post was rather exercise in bash.

Let's try to figure out at what hour of the day it is better to publish post to get better chance of reaction and comments - for this I'll refine the code and do this in PHP this time. Python may be better when whimsical analytics is necessary - but in this case I prefer PHP for its simple functions to retrieve and parse json.


As a brief reminder - we can fetch random articles from DEV API in JSON format and analyze various fields here, extracting time of creation etc. Upon retrieving many articles we can judge at what time there are more users on site. The API link for articles is

What improvements

  1. Let us use hour of publishing rather hour of creation (i.e. when users first see the article).

  2. Let's not just count how many articles are published in given hour, but add bonus points for articles having enough reactions or comments.

Let's start

Main loop of the script is like this - we get top article ID from env this time (you've seen we can take it from API itself instead - but now I want some manual control). Then we pick some random articles with ids between this one and 32000 earlier. Some of them do not exists (perhaps were removed). Others we can count:


$max = getenv('MAX'); // 263822
$url = '';

$count = 0;

while ($count < 100) {
    $id = $max - rand(0, 32000);
    $resp = @file_get_contents($url . $id); // sending HTTP get to API
    if ($resp === false) {
        echo "$id - error\n";
    $json = json_decode($resp);
    $pub = $json->published_at;  // not created_at!
    $hour = substr($pub, 11, 2); // date format is 2020-01-02T11:22:33Z
    $count += 1;
    echo "$id -> $hour\n";

This code will print article IDs we have tried - and either hour of publishing, or word error.

Let's add an array of flags to mark which IDs we have already tried, so we don't repeat:

$seen = array();

//... and in the loop
while ($count < 1000) {
    $id = $max - rand(0, 32000);
    if (isset($seen[$id])) {
    $seen[$id] = true;

And counters for every hour:

$hours = array_fill(0, 24, 0);

    //... and inside loop
    $hours[$hour + 0] += 1;

At the end we can print out results (you can do pretty chart of course):

echo implode(" ", $hours) . "\n";

Ok, the code will work. You can try it and see distribution of article publishing hours. But that's not exactly what we want!

Bonus for reactions and comments

Now we add 1 point for every article published in given hour. Let's add 1 bonus point if that article has over 5 comments, for example - and another 1 bonus point if that article has at least one comment.

    $cmn = $json->comments_count;
    $rct = $json->positive_reactions_count;
    $hours[$hour + 0] += 1 + ($cmn > 0 ? 1 : 0) + ($rct > 10 ? 2 : 0);

Of course you can change these thresholds and assign different bonus values. I won't spoil you fun of doing this small research yourself - but in general you can get distribution similar to one shown above.

Good luck and happy coding to everyone! Thanks for reading that far!

Top comments (0)