Handling calendar / date in pages?

Post Reply
kr428
Sr. Bludit
Posts: 38
Joined: Thu Jan 05, 2023 9:07 pm
Has thanked: 11 times
Been thanked: 3 times

Folks;
as I haven't found good starters here, are there any recommendations on how to make best use of dates in bludit? Like, in example, I'd enjoy adding a "this day <xx> years ago" section, or limit my starting page only to posts from the last <n> days. Is that something that can be achieved the way bludit stores its (meta)data? Any good pointers on that?
Thanks in advance and best regards,
Kristian
User avatar
Misteric
Ssr. Bludit
Posts: 16
Joined: Mon Aug 08, 2022 2:55 pm
Has thanked: 3 times
Been thanked: 6 times

Hi Kristian,

To show how many minutes and days are past, you can use the php-code below:

Code: Select all

<?php 
function timeBetween($start_date,$end_date)   
 {   

    $diff = $end_date-$start_date;   
    $seconds = 0;   
    $hours   = 0;   
    $minutes = 0;   
    if($diff % 86400 <= 0){$days = $diff / 86400;}  // 86,400 seconds in a day   

    if($diff % 86400 > 0)   
    {   
        $rest = ($diff % 86400);   
        $days = ($diff - $rest) / 86400;   
        if($rest % 3600 > 0)   
        {   
            $rest1 = ($rest % 3600);   
            $hours = ($rest - $rest1) / 3600;   
            if($rest1 % 60 > 0)   
            {   
                $rest2 = ($rest1 % 60);   
            $minutes = ($rest1 - $rest2) / 60;   
            $seconds = $rest2;   
            }   
            else{$minutes = $rest1 / 60;}   
        }   
        else{$hours = $rest / 3600;}   
    }   

    if($days > 0){$days = $days.' days, ';}   
    else{$days = false;}   
    if($hours > 0){$hours = $hours.' hours, ';}   
    else{$hours = false;}   
    if($minutes > 0){$minutes = $minutes.' minutes, ';}   
    else{$minutes = false;}   
    $seconds = $seconds.' seconds';

    return $days.''.$hours.''.$minutes.''.$seconds;   
}

echo timeBetween(strtotime($page->date()), strtotime(date("Y-m-d H:i:s")));
?>
I hope it's helpfull!


Misteric
kr428
Sr. Bludit
Posts: 38
Joined: Thu Jan 05, 2023 9:07 pm
Has thanked: 11 times
Been thanked: 3 times

Misteric wrote: Tue Apr 09, 2024 3:37 pm Hi Kristian,

To show how many minutes and days are past, you can use the php-code below:

//..//
I hope it's helpfull!


Misteric
Thanks, definitely so. :) However, there's one thing missing to my mental model at the moment: Say, I'd like to display all posts of "April 11" on some earlier year, this would require some way to query Bludit for posts that have been created on "some April 11" earlier, ideally without having to traverse and check all of them. Would this be possible...?
User avatar
Misteric
Ssr. Bludit
Posts: 16
Joined: Mon Aug 08, 2022 2:55 pm
Has thanked: 3 times
Been thanked: 6 times

Kristian

You have the creation date and the Modified day:

with an if statement like this:

Code: Select all

<?php

$date = $page->date();

if(isset($page->dateModified())) { 
  $date = $page->dateModified(); 
}

if(strtotime($date) > strtotime(date("2024-10-31 23:59:59"))) {
  echo timeBetween(strtotime($date), strtotime(date("Y-m-d H:i:s")));
}

?>
(You can change the year 2024 in the if-statement with a variable.)


Misteric
kr428
Sr. Bludit
Posts: 38
Joined: Thu Jan 05, 2023 9:07 pm
Has thanked: 11 times
Been thanked: 3 times

Ah, ok, so I basically have to iterate across all pages and compare dates with "today"? Won't that fall over performance-wise if the site gets a bit ... bigger? Or am I getting something wrong here? From a database point of view, what I'm looking for is something like "give me all pages created on $day-of-month / $month-of-year regardless of which year".

(This snippet, though, scratches another itch I was having for quite a while, so thanks for that.🙈)
Post Reply