0

I am new in PHP

when I am trying to do this

if( date('m-Y',strtotime('2016-11-01 00:00:00')) < date('m-Y') ) {
    echo "yes";
} else {
    echo 'no';
}

but it always do false [output 'no'].

I must need to compare months is less than current month , means compare date do not have same months

where I am wrong to compare that date ?

Php Symfony Guru
  • 137
  • 1
  • 12
  • 4
    These are not dates, they are strings. If you want to compare dates, you should compare `DateTime` objects or timestamps. – jeroen Jan 11 '17 at 08:02
  • 2
    @jeroen or dates as string but formatted as Y-m-d – krzysiej Jan 11 '17 at 08:03
  • I must need to compare month , means compare date do not have same months – Php Symfony Guru Jan 11 '17 at 08:03
  • This will always return false because you are comparing with current date `date('m-Y')` – Akshay Jan 11 '17 at 08:05
  • May be `strtotime(date('m-Y',strtotime('2016-11-01 00:00:00'))) < strtotime( date('m-Y'))` – Sougata Bose Jan 11 '17 at 08:07
  • 1
    @AnkitSoni. Please check https://eval.in/713989. You need to change the format `m-Y` -> `Y-m` so that they can be recognized. – Sougata Bose Jan 11 '17 at 08:19
  • 1
    @jeroen I have removed a bunch of your comments because they were drifting into needless discussion based on a wrong premise. Strings actually *are* natively comparable using `<` and `>`. They are *not* cast to numbers. `'a' < 'b'` is `true` while `'c' < 'b'` is `false`. It actually works this way. – deceze Jan 11 '17 at 09:43

4 Answers4

2

Use DateTime to compare dates:

$date = new DateTime('2016-11-01 00:00:00');
$now = new DateTime();

if ($date < $now && $date->format('m-Y') != $now->format('m-Y')) {
    echo 'yes';
} else {
    echo 'no';
}
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
0

I copied your program so that it reads:

<?php
$x=date('m-Y',strtotime('2016-11-01 00:00:00'));
echo "$x\n";
$y=date("m-Y");
echo "$y\n";

if ($x < date('m-Y') ) {
    echo "yes";
} else {
    echo 'no';
}

On running it the output is:

# php x.php 
11-2016
01-2017
no

That is why it fails. If you are checking for just the month you need to check for equality. Otherwise you need to reorder the date formatting to be "Y-m" (not 'm-Y') for less/greater than comparisons. Comparing the strings is fine.

mlewis54
  • 2,372
  • 6
  • 36
  • 58
0

date function always return a string. In your if construct you compare two strings. For current time:

"11-2016" < "01-2017"

In this case "11-2016" greater than "01-2017". It will be better to use DateTime class.

$date = new DateTime('2016-11-01 00:00:00');
$now = new DateTime();

if ($date < $now && $date->format('m-Y') != $now->format('m-Y')) {
    echo 'yes';
} else {
    echo 'no';
}

or in your example you need to change format to 'Y-m'.

-1

You should use a decent format to compare the dates. Instead of m-Y, use Y-m-d.

Currently, you are converting the dates to strings, with their months first. So the first date becomes 11-2016, the second becomes 01-2017. PHP compares these as strings, and finds that 0 is less thans 1, so considers the second string to be less.

Daan Meijer
  • 1,324
  • 7
  • 12