0

I am trying to create a function in php where i check for the events date and if it less than todays date, then show no events. And if it is greater than todays date, display the date.

This is my php function

function todays_date($date)
{
    $date_now = time();

}
function event_start_date($id)
{
    $startDate = strtotime(get_field('event_start_date', $id));
    $date = date('U', $startDate);
    return $date;
}

and the in twig im checking to see if the dates are greater than one another in order to do something else

    {% set date = function('event_start_date', item.id)|date('M d') %}
    {% set today = function('todays_date')|date('M d') %}
                        {% if date < today %}
                        <p>No events found.</p>
                        {% else %}
                        <p class="date">{{ date }}</p>

im not sure what i am doing wrong.

DarkBee
  • 16,592
  • 6
  • 46
  • 58
Sarah
  • 105
  • 1
  • 7
  • Iv never used twig but can you call a function from twig like that? also what errors are you seeing? – Yeak Jun 29 '22 at 20:29
  • the error i get is 'Too few arguments to function todays_date(), 0 passed and exactly 1 expected' – Sarah Jun 29 '22 at 20:30
  • 1
    function('todays_date') doesnt seem like it has any arguments being passed where as function('event_start_date', item.id) has the item.id being passed - again not a twig person but just by observation it doesnt seem like your passing any arguments to the function. Maybe you dont even need to pass argument and should just create the todays_date function without requiring a argument of $date so just make it function todays_date() {} – Yeak Jun 29 '22 at 20:36
  • What is `function`? If you want to access `PHP` function inside `twig` it's better to create an extension and chain `PHP` functions with it – DarkBee Jun 30 '22 at 05:34
  • Does this answer your question? [How to create an extension using Twig](https://stackoverflow.com/questions/45724016/how-to-create-an-extension-using-twig) – DarkBee Jun 30 '22 at 05:35
  • The `todays_date` function does anything there. Can you use time() as it is and not include in another function? As a note, you do not return anything from your todays_date function. And the second line from twig may be: `{% set today = function('time')|date('M d') %}`. But, I think is better to compare time() (a timestamp) with another timestamp. – LucianDex Jun 30 '22 at 05:52
  • What date format is `M d`? Assuming this uses the same format specifiers as PHP DateTime, you would get values like `Jun 30` or `Sep 15` here - and clearly it makes little sense to try and compare these using `>` or `<`. – CBroe Jun 30 '22 at 07:26

0 Answers0