Snippet · Mecha CMS

Code snippet collection for developers.

Static Time Field

Author: Taufik Nurrohman · 388 Views

Tweet

Automatically generate static time data on the first page visit, so that updating the page files will not change its publication time.

Hook::set('shield.enter', function() use($site) {
    if ($page = Lot::get('page')) {
        $time = Path::F($page->path) . DS . 'time.data';
        if ($site->is !== '404' && !File::exist($time)) {
            File::write($page->time)->saveTo($time);
        }
    }
});

Note: The default shield uses $page->update to show the current date which will output the file modification time as the page date. To show the current page date created by this snippet, you can use $page->time or $page->date->{$format}:

<p><?php echo $page->date->en_us; ?></p>

Related: $date->extract()

0 Comments

No comments yet.