I have a problem with date comparing logic in MySQL and PHP: date difference functions return different results for the same dates. Example:
SQL
SELECT DATEDIFF('2024-06-02', '2024-05-03 12:57')
30
PHP
$x = date_diff( new DateTime( '2024-06-02' ), new DateTime( '2024-05-03 12:57' ) );

This is causing an assertion error when I pass data selected from the DB with the where DATEDIFF... criteria to PHP that checks the date with the date_diff() function.
Is there a strict equivalent for MySQL function in PHP (or vice versa), so that both return 29 (or 30)?
Both should return 30 but the PHP code you have written doesn't account for time so it reports 29.
If you normalize both to midnight, it will report 30 days.
echo $x = date_diff(
(new DateTime( '2024-06-02' ))->setTime(0,0),
(new DateTime( '2024-05-03 12:57' ))->setTime(0,0)
)->days;
// Outputs: 30
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With