How to get the last second of the month with datetime? Or last second of arbitrary date

76 views Asked by At

Tried this:

$thismonth = new DateTime(dateReformat( 'Y-M' ));
$end       = $thismonth->modify('last day of this month 23:59');

got this: 2018-02-28 23:59 as expected.

Tried this:

$thismonth->modify('last day of this month 23:59:59');

got this: 2018-02-28 23:59 expected: 2018-02-28 23:59:59

I'm trying to make a query, where I dont miss anything from the last minute of the month.

1

There are 1 answers

0
Indra Kumar S On

As @salathe mentioned in comment

In order to get seconds, You must provide it in format

  echo $thismonth->format('Y-m-d H:i:s'); // note the "s"

Or as you requested in your comment

yes, however the user sometimes picks the date, like 2018-02-15. So, I would need something like "where tbl.date < 2018-02-15 23:59:59 " Maybe I should edit my question a little.

Use where clause in sql queries like this

WHERE date(tbl.date) <= '2018-02-15'