Compare dates not working

Hello,

I am comparing 2 dates. It’s clear that $ db_minus7 is larger, so the value of $ can_invoiced should be “possible,” but it is “Yes.” When i do

<?php

$db_minus7 = '2010-07-05 09:45:29.420';
$completion_date = '30.07.2009';
if(date("m-d-Y",strtotime($db_minus7)) > date("m-d-Y",strtotime($completion_date))) {
    $can_invoiced = 'maybe';
} else {
    $can_invoiced = 'Yes';
}

echo $can_invoiced;

?>

please, help

+3
source share
4 answers

why don't you just compare the times when they formed them again as follows:

if(strtotime($db_minus7) > strtotime($completion_date)) {
    $can_invoiced = 'maybe';
} else {
    $can_invoiced = 'Yes';
}

EDIT:

if you want to use date (), use "Ymd"either "Y-m-d"as a template, because it is a comparison of strings, and it is a logical order of work (sort the templates from "large" (years) to small (days ... or maybe seconds, if you necessary));

+5
source

Do not use "m-d-Y"but "Y-m-d".

01-01-2010 02-01-2009 (bad!), 2010-01-01 2009-01-02 ( !).

+2

datestamps, :

if(strtotime($db_minus7) > strtotime($completion_date)) { 

If you need to compare dates as strings, use Ymd, not mdy

0
source

Never compare dates as strings, even if it works for your test boxes, sooner or later it will fall on your feet ... or maybe ... Richard Stallman will prey on you, I don’t know ...

if(strtotime($db_minus7) > strtotime($completion_date)) {
    $can_invoiced = 'maybe';
} else {
    $can_invoiced = 'Yes';
}
0
source

Source: https://habr.com/ru/post/1766763/


All Articles