Render simple php function in twig template I am rebuilding a website in Drupal 8 and the website shows some content with bootstrap classes according to a conditional php function based on time of day

function openStatus(){
$open_str = '&lt;span class="badge badge-success"&gt;Open&lt;span                   class="hidden-xs"&gt; until 5pm&lt;/span&gt;&lt;/span&gt;';
$closed_str = '&lt;span class="badge badge-danger"&gt;Closed&lt;span class="hidden-xs"&gt; - Open Mon-Fri 9AM-5PM&lt;/span&gt;&lt;/span&gt;';
if(gmdate('N') &lt;= 5 &amp;&amp; gmdate('H') &gt;= 8 &amp;&amp; gmdate('H') &lt;= 16){
echo $open_str;
} else {
echo $closed_str;
}


This is what I have so far, which I think works but not sure how to do day of week.

{% if "now"|date('H', "Europe/London") &gt;= 8 &amp;&amp; "now"|date('H', "Europe/London") &lt;= 16  %}
&lt;span class="badge badge-success"&gt;Open&lt;span                     class="hidden-xs"&gt; until 5pm&lt;/span&gt;&lt;/span&gt;
{% else %}
&lt;span class="badge badge-danger"&gt;Closed&lt;span class="hidden-xs"&gt; - Open Mon-Fri 9AM-5PM&lt;/span&gt;&lt;/span&gt;
{% endif %}
Would suggest doing a pre-process function and stuff in the variables everything the template needs to render. The point of Twig is to push the logic back to the PHP side so that your template does not look like "HTML-in-PHP" or "PHP-in-HTML" (whichever way you look at it) as with previous versions of Drupal.