# php time between 2 dates Y-m

708

I have this code that says if days between 2 dates,
but i need it to work with Y-m instead of Y-m-d how to achieve this? example: i need to check if`2016-10` is between`2016-09` and`2016-12`

my code:

``````\$paymentDate = date('Y-m-d', strtotime(\$date));
\$contractDateBegin = date('Y-m-d', strtotime(\$dfrom));
\$contractDateEnd = date('Y-m-d', strtotime(\$dtom));

if ((\$paymentDate > \$contractDateBegin) && (\$paymentDate < \$contractDateEnd)){

echo "is between";

}else{

echo "NO GO!";

}
``````
423

#### Solution:

Extract Month and Year and compare it.

``````\$paymentDateY = date('Y', strtotime(\$date));
\$paymentDateM = date('n', strtotime(\$date));

\$contractDateBeginY = date('Y', strtotime(\$dfrom));
\$contractDateBeginM = date('n', strtotime(\$dfrom));

\$contractDateEndY = date('Y', strtotime(\$dtom));
\$contractDateEndM = date('n', strtotime(\$dtom));

if (\$paymentDateY >= \$contractDateBeginY && \$paymentDateY <= \$contractDateEndY
&& \$paymentDateM >= \$contractDateBeginM && \$paymentDateM <= \$contractDateEndM){
echo "is between";
}
else
{
echo "NO GO!";
}
``````
885

#### Solution:

use`Y-m` is oK: Demo

``````\$paymentDate=date('Y-m', strtotime(\$date));;
\$contractDateBegin = date('Y-m', strtotime(\$dfrom));
\$contractDateEnd = date('Y-m', strtotime(\$dtom));

if ((\$paymentDate > \$contractDateBegin) && (\$paymentDate < \$contractDateEnd)){
echo "is between";
}else{
echo "NO GO!";
}
``````

People are also looking for solutions to the problem: php - Replace an Underscore with a blank space

Source