Comparison between month and year

Asked

Viewed 118 times

-2

I need to compare only the year and month between two dates, I did it the following way and it works perfectly:

$mes1 = "2018/02";
$mes2 = "2018/03";

if($mes1 > $mes2) {

    echo "Mês 1 é maior que mês 2";

}elseif($mes2 > $mes1) {

    echo "Mês 2 é maior que mês 1";

}

My doubt is about good practice, compare only the year and month so this correct or I would need to use some resource of the php to say that it is a date? my fear is that start to error in some version of php future.

NOTE: my doubt is precisely because of the lack of the "day" in this comparison, all the comparisons I saw they have year, month and day, mine does not have the day

I know there are several posts that may look similar to but they are not.

  • which would cost you to concatenate the day into the variable and make it a date and work correctly with dates in PHP?

  • I don’t need the day, the comparison needs to be only between the months, I need it to try to determine the difference of months that one date has the other

  • I’m not saying you need the day, I’m saying concatenate qq day into variavl to turn it into a date. It might be qq for the two variables. dai compares as date in php

3 answers

0

The comparison will work in any version because you are comparing strings, and the most significant character is on the left, while the least significant character is on the right.

Is it good practice? No, dates are usually converted into integers (timestamp) and then compared for greater flexibility.

  • I’m trying to figure out the difference between months that one date has the other, the problem is that it returns me an error, you know what might be? code: $mes1 = new Datetime( '2018-03-15' ); $mes2 = new Datetime( '2018-02-22' ); $range = $mes1->diff( $mes2 ); error: Error Object of class Dateinterval could not be converted to string on line number 11

0

Youthful,

Your solution is not wrong, but PHP has functions ready to speed up and make things transparent to, for example, then deal with the data by inserting in a Mysql of life..

I made an example, I hope it helps you:

<?php
   $mes1 = '2001-03-21';
   $mes2 = '2014-05-21';

   $data = new DateTime($mes1); // Data de Nascimento
   $nova_data = $data->diff(new DateTime($mes2));
   $calculo = $nova_data->format('%m');

   $total_meses = $calculo;

   echo $total_meses;

?>
  • Thanks for the help, but I had a little problem, in this comparison for example I expected to receive the difference of 15 months, but I get only 3: $mes1 = '2018-11-21'; $mes2 = '2020-02-21';

0

Concaten any day on both dates, turn them into valid dates in PHP and compare with strtotime.

$dia="-01";

$mes1 = "2018/02".$dia;
$mes2 = "2018/03".$dia;


$mes1 = strtotime(str_replace('/', '-', $mes1));

$mes2 = strtotime(str_replace('/', '-', $mes2));



if($mes1 > $mes2) {

    echo "Mês 1 é maior que mês 2";

}elseif($mes2 > $mes1) {

    echo "Mês 2 é maior que mês 1";

}

https://ideone.com/U4tkHI

Browser other questions tagged

You are not signed in. Login or sign up in order to post.