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 if2016-10 is between2016-09 and2016-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

Answer

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

Answer

Solution:

useY-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

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.