Getting Night Mode to Load on a Schedule

Published:

I recently put together a simple eye-comfort mode for the site. The idea was to have it take effect at night, roughly from 7 p.m. to 7 a.m. The original theme did not include a dark mode, and browsing it in the evening felt a bit harsh to me. Maybe my eyes are just more sensitive than most, especially since I have astigmatism.

I barely know how to write code, so the whole process was basically me following tutorials and trying things one by one. I searched through quite a few guides, but with my limited ability, the actual implementation either conflicted with something else or failed outright. After several failed attempts, I got discouraged and put the whole thing aside for a while.

A few days later, while using the computer at the shop, I came across a short piece of code. It was simple enough to understand, so I saved it locally and tried it again after getting home. This time it worked. Unfortunately, I was browsing in incognito mode and forgot which page I copied it from, so I cannot properly credit the original source.

<?php
      date_default_timezone_set('PRC');
      $hour = date('H');
      if($hour > 6 && $hour <= 18){
        echo ' ';
      }else{
        echo '<link href="https://zhou.ge/usr/themes/simple/css/night.css" rel="stylesheet"/>';
    }
    ?>

The logic is easy to follow: within the specified time range, another CSS file is loaded. I do understand a little bit about changing colors in CSS, so editing the night-mode stylesheet is still within reach.

There is one thing I still do not understand, though. Since I do not really know PHP syntax, how should this absolute path be written in the following style without causing an error?

echo '<link href="https://zhou.ge/usr/themes/simple/css/night.css" rel="stylesheet"/>';

What I would like to use is something like this:

<link rel="stylesheet" href="<?php $this->options->themeUrl('/css/night.css'); ?>">

If anyone knows the correct way to write it, I would appreciate the help.

Scheduled night mode

There also seems to be a bug. I set night mode to load from 6 p.m. to 6 a.m., but in practice it does not switch on until 7 p.m. The same delay happens in the morning, too; it remains active until 7 a.m. I still have not figured out why the timing is off by one hour.

One more thing: I came down with a fever today. My throat felt a little sore in the morning, and by the time I got off work in the evening I was dizzy, weak all over, and had diarrhea. I went to the local clinic, and the temperature reading was over 39°C, which honestly did not surprise me. There were quite a few other people there with fever and discomfort as well.

After taking medicine and trying physical cooling, I still felt awful. Could this be the second wave already? I lay down for two or three hours, but my forehead was still burning hot, so I took some Merrill.


Update on February 26: I found some time to make another version of the night mode. This one uses a darker color scheme.

Scheduled night mode

The mobile experience feels better when the surrounding light is not very bright. With that, this round of tinkering is basically finished for now.

Current active time: every day from 8:00 p.m. to 7:00 a.m. Feel free to visit and complain about it.