0

I know there's a lot around here but for many articles I read and test this simply don't work..

I'm saving date with ACF like d.m.y and reading too.

When I make this type of comparison it works BUT only for the current month. No matter what I do, this is the working thing I've done that works, any advise please?

if(strtotime(date('d.m.y')) > strtotime($cd_date_from)) {
   $output .= ' class="event-passed"';
}

This means that I'm hiding only the event on the table row that the date is older than today (showing today). If the date is 15.03.19 or 28.03.19 it shows BUT if it's 15.04.19 (other month) it don't.

Thank you in advance

FilipeOS
  • 801
  • 1
  • 11
  • 42
  • so today is 15.03.09 that's why it is not working:) – myxaxa Mar 15 '19 at 12:46
  • Possible duplicate of [How to calculate the difference between two dates using PHP?](https://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – Dave Mar 15 '19 at 12:48

2 Answers2

1

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-), the date string is parsed as y-m-d.

To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.

http://php.net/manual/en/function.strtotime.php

Use:

if(strtotime(date('Y-m-d')) > strtotime($cd_date_from)) {
   $output .= ' class="event-passed"';
}

and pass Y-m-d format values to $cd_date_from

Andrei Lupuleasa
  • 2,677
  • 3
  • 14
  • 32
0

it's much easier to use \DateTimeInterface to work with DateTime in php: \DateTime::createFromFormat

<?php
$format = 'd.m.y';
$cd_date_from = '12.03.19';
$date = \DateTime::createFromFormat($format, $cd_date_from);
$date->setTime(0,0,0,0);
if (new \DateTime() > $date) {
    echo 'hello';
}
myxaxa
  • 1,391
  • 1
  • 8
  • 7
  • according your reply the row with the today's date is not showing. The future ones and the yesterday's row yes. – FilipeOS Mar 15 '19 at 13:04
  • DateTime object contain time also, so if you don't want to check time you can use $date->setTime(0,0,0,0); for example – myxaxa Mar 15 '19 at 13:30