Let me explain the scenario.
I have "Month(Any month from 01 TO 12)" and "Year(For Example 17 for 2017" in my local variables.
I want to compare it with Current Month and Year.
.
$month = 04;
$year = 17;
if ($month.'-'.$year > current month & year)
{
echo "not expired";
}
else
{
echo "expired";
}
3. For example:
$old_month = 03; // March
$old_year = 17; // 2017
$new_month = 05; // May
$new_year = 17; // 2017
//how to compare and determine which month&year is old one or new one ?
Searched a lot in google and SO posts, but couldn't find any solution. Please help me.
NB: I have only month and year in my hand to compare it with current month and year.
Atlast the following code worked fine for me.
$currentDate=date('d/m/Y'); // taking current date
$expirationDay = 31;
$expirationMonth = 12;
$expirationYear = 2017;
$date2 = $expirationYear.'-'.$expirationMonth.'-'.$expirationDay;
$date2 = date_create($date2);
$date2 = date_format($date2,"d/m/Y");
if($currentDate <= $date2)
{
echo "not expired";
}
elseif($currentDate > $date2)
{
echo "expired";
}
@Praveen George i hope you want to do it like below try this with date():
<?php
$searchingDate = '05-17'; // your variable which contain month and year
if($searchingDate == date("m-y")){ // date("m-y") return current month and year in format like('05-17) if both are equal then it match
echo "match";
}
else{
echo "did not match";
}
Use DateTime()
to compare dates to each other. It's cleaner and easier to read as DateTime obejcts are comparable.
I used DateTime::createFromFormat
since the dates you have from the API aren't going to be in a standard format for PHP and this makes it easier for PHp to handle.
$date = \DateTime::createFromFormat('my', $month.$year);
$now = new \DateTime('midnight');
if ($date > $now) {
echo "not expired";
} else {
echo "expired";
}