datetwigoctobercmsoctobercms-plugins

Octobercms twig how to show upcoming birthdays


My birthdays are stored with: {{ user.profile.dob }}, and I would like do display the birthday of users within the next 30 days. Here is my code:

{% for user in users %} 
    {% if user.profile.dob|date('m-d')> 'now'|date_modify('-30 day')  %}   
    {{ user.name }} {{ user.surname }} {{ user.profile.dob }}<br> 
    {% else %}
    <p>no birthdays this week</p>
   {% endif %} 
{% endfor %}

Solution

  • Do logic in PHP Code either create your own plugin or the page code.

    Here is an example of what is possible. Of course you are going to be working with an array but you just need to use a foreach or ->each() to do it. PHP Code:

    use Carbon\Carbon;
    
    function onStart() {
        
        $now = new Carbon('now', 'America/Los_Angeles');
        
        $dob = new Carbon('1/3/2021', 'America/Los_Angeles');
        
        $difference = $now->diffInDays($dob);
        
        $this['difference'] = $difference;
        
        $this['dob'] = $dob;
        
        $this['now'] = $now;
    }
    

    Twig:

    {% if difference < 30 %}
    
    <h2>You have a birthday this month in {{ difference }} days!</h2>
    
    {% else %}
    
    <h2>Your birthday is in {{ difference }} days.</h2>
    
    {% endif %}
    

    enter image description here

    enter image description here