0

Having some difficulties with my if/else statements recognizing my date and time greater or less than properly in my custom WordPress theme using ACF.

I think I may have it narrowed down to that is comparing strings instead of timestamps from this post but am baffled how to output that properly in twig? Here would be an example of what I currently have:

{% set dateandtime = fn('wp_date', "F j, Y g:i a") %}
{% set salestart = post.sale_start | date("F j, Y g:i a") %}
{% set saleend = post.sale_end | date("F j, Y g:i a") %}

and then:

{% if salestart >= dateandtime %}
  <p>This sale will start {{salestart}}.</p> 
  {% elseif dateandtime >= salestart and dateandtime <= saleend %}
    <p>This sale will end {{saleend}}.</p>
  {% elseif dateandtime >= saleend %} 
      <p>Sorry this sale has ended as of {{saleend}}.</p>
{% endif %}

I think my issue is that its comparing strings instead of real datestamps but not quite sure what to do differently?

{{ dump(dateandtime) }} ouputs: ~src\Extension\DebugExtension.php:70:string 'July 31, 2021 2:43 pm' 
{{ dump(salestart) }} ouputs: ~src\Extension\DebugExtension.php:70:string 'August 4, 2021 9:00 pm' 
{{ dump(saleend) }} ouputs: ~src\Extension\DebugExtension.php:70:string 'August 6, 2021 5:00 pm' 

I tried fiddling with |format_datetime() from Twig Docs but didn't have much success? A clear explanation will be much appreciated as I am still a noob at this :) ~ Thank-you!

ShrockCo
  • 357
  • 1
  • 13
  • 1
    Use the function `date` not the filter `{% set salestart = date(datepost.sale_start) %}` – DarkBee Jul 31 '21 at 19:23
  • 1
    Does this answer your question? [Compare date with a specific one and DateTime to string in Twig](https://stackoverflow.com/questions/11774520/compare-date-with-a-specific-one-and-datetime-to-string-in-twig) – DarkBee Jul 31 '21 at 19:23
  • Thank-you, but how would I format it for the wp_date in wordpress `{% set dateandtime = fn('wp_date', "F j, Y g:i a") %}` because this also just outputs in string – ShrockCo Jul 31 '21 at 19:39
  • I tried this but it won't work `{% set dateandtime = fn('wp_date', 'date') %}` and `{% set dateandtime = date(wp_date) %}` – ShrockCo Jul 31 '21 at 19:51
  • I could do this but `{% set dateandtime = date(now) %}` then its off by -4:00 hrs I would like `dateandtime ` to be the WordPress time. – ShrockCo Jul 31 '21 at 19:58
  • Okay I think this may work here: `{% set dateandtime = fn('wp_date', 'U') %}` feel free to comment if there is a better way and many thanks again to @DarkBee for your help! – ShrockCo Jul 31 '21 at 21:00

0 Answers0