简体   繁体   中英

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.

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

仅在确定月份和日期都是两个字符长的情况下才有效。

Came in a little late, but here's one that doesn't care what format the other date is in (eg “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';
}

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

这将以相同的格式检索日期:

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

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.

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
}

嗨,你可以比较这样的日期

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM