简体   繁体   中英

How to compare two dates without years in php?

I would like to compare the two dates ,one date is which the 02 aug 1921 , and the second date is present date.How to compare the old date and present date day and month .if the past date 02 sept and present date 02 sept equals it should echo. I had the script which compares the past and present date with the year.How to compare without the year.?

$r1=$row['dateofcon']; 

//dateofcon was 02 aug 1921.

echo $r1; ?>
$cmg1 = strtotime("$r1");
$day1=date('l',$cmg1);
$now1 = time();
$timeleft1 = $cmg1-$now1;
$daysleft1 = round((($timeleft1/24)/60)/60); //probably...
if($daysleft1 == 0) { 
echo "Happy Conday";}

Probably the most straight forward way is strtotime and date.

$date1 = '1 Aug 2012';
$date2 = '1 Aug 1912';
if (date('m-d', strtotime($date1)) === date('m-d', strtotime($date2)) {

}

Why don't you split the dates into year, month and day? and then:

if (($month1 == $month2) && ($day1 == $day2)) {
  echo 'hi';
}

If you are on PHP 5.3 you can use the new DateTime class:

$oDate1 = new DateTime( $datetime1 );
$oDate2 = new DateTime( $datetime2 );

if( $oDate1->format('m-d') == $oDate2->format('m-d')
  echo 'yes';

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