Kevin.a
Kevin.a

Reputation: 4296

Check if date is certain month

I have two dates, one from an API which I can't change. And one I create myself which is a month. I want to check if the date i get from the api is in the same month i provide.

Example:

$month = '12'; //december
$date = '2019-12-09 13:34:23' // date from api (cant change the way it comes in)

In this case the date is in the month december (like my variable) so i would like to built something that returns true if this is the case, but sadly i dont know where to start

Upvotes: 2

Views: 1201

Answers (4)

jspit
jspit

Reputation: 7703

With explode:

if( explode('-',$date)[1] == $month ) {
  echo 'check ok';
}

With DateTime

if( date_create($date)->format('m') == $month ) {
  echo 'check ok';
}

Upvotes: 1

Heba Fareed
Heba Fareed

Reputation: 437

You can try date_parse() :

$date = date_parse($date);
if ($month == $date['month']) {

}

Upvotes: 1

Vicky Gill
Vicky Gill

Reputation: 734

$dateValue = strtotime($q);                     

$yr = date("Y", $dateValue); 
$mon = date("m", $dateValue); 
$date = date("d", $dateValue); 

you can do something like this then you can match with if condition.

Upvotes: 4

Professor Abronsius
Professor Abronsius

Reputation: 33813

You could try like this perhaps:

$month = '12';
$date = '2019-12-09 13:34:23';

if( date('M',strtotime($date)) == date('M', mktime( 0, 0, 0, $month, 1, date('Y') ) ) )echo 'same';

Upvotes: 1

Related Questions