Glynn Phillips
Glynn Phillips

Reputation: 21

PHP - checking if two dates match but ignoring the year

I have an array which will output a date. This date is outputted in the mm/dd/yyyy format. I have no control over how this outputted so I cant change this.

Array
(
    [date] => 04/06/1989
)

I want to use php to check if this date matches the current date (today), but ignoring the year. So in the above example I just want to check if today is the 6th April. I am just struggling to find anything which documents how to ignore the years.

Upvotes: 1

Views: 2131

Answers (7)

biozes
biozes

Reputation: 41

You can use for compare duple conversion if you have a date.

$currentDate = strtotime(date('m/d',time())); --> returns current date without care for year.

//$someDateTime - variable pointing to some date some years ago, like birthday. $someDateTimeUNIX = strtotime($someDateTime) --> converts to unix time format.

now we convert this timeunix to a date with only showing the day and month: $dateConversionWithoutYear = date('m/d',$someDateTimeUNIX ); $dateWithoutRegardForYear = strtotime($dateConversionWithoutYear); -->voila!, we can now compare with current year values.

for example: $dateWithoutRegardForYear == $currentDate , direct comparison

Upvotes: 0

prototyp
prototyp

Reputation: 1

Use this:

$my_date = YOUR_ARRAY[date];

$my_date_string = explode('/', $my_date);

$curr_date = date('m,d,o'); 
$curr_date_string = explode(',', $date);

if (($my_date_string[0] == $curr_date_string[0]) && ($my_date_string[1] == $curr_date_string[1]))
{
    DO IT
}

This way, you convert the dates into strings (day, month, year) which are saved in an array. Then you can easily compare the first two elements of each array which contains the day and month.

Upvotes: 0

Jorge Pinho
Jorge Pinho

Reputation: 298

this will retrieve the date in the same format:

$today = date('m/d');

Upvotes: 0

Herbert
Herbert

Reputation: 5778

Came in a little late, but here’s one that doesn’t care what format the other date is in (e.g. “Sep 26, 1989”). It could come in handy should the format change.

if (date('m/d') === date('m/d', strtotime($date))) {
    echo 'same as today';
} else {
    echo 'not same as today';
}

Upvotes: 1

Awais Qarni
Awais Qarni

Reputation: 18016

hi you can just compare the dates like this

 if(date('m/d',strtotime($array['date']])) == date('m/d',strtotime(date('Y-m-d H:i:s',time()))) )

Upvotes: -1

jprofitt
jprofitt

Reputation: 10964

You can convert the other date into its timestamp equivalent, and then use date() formatting to compare. Might be a better way to do this, but this will work as long as the original date is formatted sanely.

$today = date('m/Y', time());
$other_date = date('m/Y', strtotime('04/06/1989'));
if($today == $other_date) {
    //date matched
}

Upvotes: -1

JJJ
JJJ

Reputation: 33153

if( substr( $date, 0, 5 ) == date( 'm/d' ) ) { ...

Works only if it's certain that the month and date are both two characters long.

Upvotes: 2

Related Questions