party-ring
party-ring

Reputation: 1871

Get Twig start of day date_modify

I am trying to get the start of today's date and time in Twig. I found that you can get the start of the current week by doing the following:

{{ now | date_modify('monday this week')|date('d-m-y H:i:s') }}

So I assume there will be a way to do this for the start of today. So far, I have tried:

{{ now | date_modify('start of day')|date('d-m-y H:i:s') }}

{{ now | date_modify('start of today')|date('d-m-y H:i:s') }}

Which resulted in:

PHP Warning: DateTime::modify(): Failed to parse time string (start of day) at position 0

PHP Warning: DateTime::modify(): Failed to parse time string (start of today) at position 0

I would like to know:

  1. What is the correct syntax to ask this?
  2. Where can I find a list of these strings?
  3. Any alternatives to passing in strings

Thank you.

Upvotes: 2

Views: 1324

Answers (2)

Michael Schmid
Michael Schmid

Reputation: 5005

{{ date('today') | format_datetime() }}

date (as well as date_modify) uses PHP’s strtotime function. today refers to 00:00:00 of the current day, as stated in PHP’s relative format reference.

Upvotes: 0

Majksa
Majksa

Reputation: 36

you can find those strings here: https://www.php.net/manual/en/datetime.formats.relative.php.

So I think what are you looking for is this: {{ now | date_modify('today') | date('d-m-y h:m:s') }}

Upvotes: 2

Related Questions