With the strtotime function, it's possible to convert a written-out date and time into a Unix timestamp. This allows you to compare it with other dates.

strtotime

{{"2023-07-01"|strtotime}} 
{{"2023-07-01 13:12:09"|strtotime}} 

Examples

Below are some examples, where $smarty.now is a Unix timestamp by default:

strtotime

{{"2023-07-01"|strtotime}} 
{{"2023-07-01 13:12:09"|strtotime}} 
{{$smarty.now}} (2023-07-03 19:48:46)
{{'first day of this month'|strtotime}}


Output

1688162400 (2023-07-01 00:00:00)
1688209929 (2023-07-01 13:12:09)
1688406526 (2023-07-03 19:48:46)
1688162400 (2023-07-01 00:00:00)

* In the above example, it's also evident that if only a date is provided, the default time is set to 00:00:00.


Below is an example comparing two dates using an if-statement:

strtotime with an if-statement

{{$date1 = "2023-01-01"}}
{{$date2 = "2023-06-24"}}

{{if $string1|strtotime < $date2|strtotime}}
Date 1 is earlier than Date 2.
{{else}}
Date 2 is earlier than  Date 1
{{/if}}


{{$date3 = "2023-06-24 01:00:00"}}
{{$date4 = "2023-06-24"}}

{{if $string3|strtotime < $date4|strtotime}}
Date 3 is earlier than Date 4
{{else}}
Date 4 is earlier than Date 3
{{/if}}


Output:

Date 1 is earlier than Date 2
Date 4 is earlier than Date 3

A strtotime in combination with an date_format_locale:

strtotime with a date_format_locale

{{'first day of this month'|strtotime|date_format_locale:'nl-NL':'MMMM'}} 
{{'first day of this month'|strtotime|date_format_locale:'en-EN':'MMMM'}}


Outcome:

juli (2023-07-01)
July (2023-07-01)

Here's an example with an existing date where you need to add 10 days:

Existing data + 10 days

{{$existingDate = "2023-10-12"}}
        
{{* Add 10 days to the UNIX timestamp *}}
{{$newDateUnix = $existingDate|strtotime+24*60*60*10}}
{{$newDateUnix|date_format:"%d-%m-%Y"}} 22-10-2023
        
{{* Add 10 days based on a text value *}}
{{$newDate = "$existingDate + 10 days"|strtotime|date_format:"%d-%m-%Y"}}
{{$newDate}} 22-10-2023