0

Hey I am trying to make a href with current year and month, sent with get request to a controller function this way <a href="{{ path('transactions', {'year' : date("Y"), 'month' : date("n")}) }}"></a> and im getting "An exception has been thrown during the rendering of a template ("Warning: preg_match() expects parameter 2 to be string, object given")."

I assume that I am getting an object instead of string but I have no clue how to overcome it and I didn't found any reasonable way to solve it.

2
  • I would use $date = new DateTime('2000-01-01'); $result = $date->format('Y-m-d H:i:s'); like $date->format('y') for the year and so on. Commented Jan 24, 2021 at 12:11
  • @PrashanthBenny I'm not sure if I'm able to do this like that inside twig template Commented Jan 24, 2021 at 12:38

2 Answers 2

2

To default way to do it inside twig is using NOW, e.g.

<a href="{{ path('transactions', {'year' : 'NOW'|date('Y'), 'month' : 'NOW'|date('m'), }) }}"></a>

documentation

Sign up to request clarification or add additional context in comments.

1 Comment

That's exactly what i was looking for, thanks a lot!
0

In a controler responsible for rendering this specific view i created 2 variables standing for current year and month and then i passed them to the view.

class MainController extends AbstractController
{
    /**
     * @Route("/", name="main")
     */
    public function index(): Response
    {
        $current_year = date('Y');
        $current_month = date('n');
        return $this->render('main/index.html.twig', [
            'current_year' => $current_year,
            'current_month' => $current_month,
        ]);
    }
}

In a view i render a href with following:

<a href="{{ path('transactions', {'year' : current_year, 'month' : current_month}) }}"></a

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.