【问题标题】:Octobercms twig how to show upcoming birthdaysOctobercms twig 如何显示即将到来的生日
【发布时间】:2021-03-16 00:56:27
【问题描述】:
我的生日存储在:{{ user.profile.dob }},我想在接下来的 30 天内显示用户的生日。这是我的代码:
{% 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 %}
【问题讨论】:
标签:
date
twig
octobercms
octobercms-plugins
【解决方案1】:
在 PHP 代码中执行逻辑创建您自己的插件或页面代码。
以下是可能的示例。当然,您将使用数组,但您只需要使用foreach 或->each() 即可。 PHP代码:
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;
}
树枝:
{% if difference < 30 %}
<h2>You have a birthday this month in {{ difference }} days!</h2>
{% else %}
<h2>Your birthday is in {{ difference }} days.</h2>
{% endif %}