Hafiz Hanif
Hafiz Hanif

Reputation: 471

Comparing day and month in php

Good evening!

Problem: The problem is to compare 2 dates in PHP. I want to only compare day and month, excluding the year. I want the code to first check the month, if the month is same or less than current month. If it's true, move on to check the day. If the day is equal to, or less than current day, execute a custom code.

What I've tried: Here is where I got so far -

<?php
$oldDate = "26/02/1815";
$latestDate = explode("/", $oldDate);
$year = $latestDate[2];
$month = $latestDate[1];
$day = $latestDate[0];

$newDate = $month.'/'.$day.'/'.$year;
$nowDate = date('m/d/Y');

$nownowDate = explode("/", $nowDate);
$nowYear = $nownowDate[2];
$nowMonth = $nownowDate[0];
$nowDay = $nownowDate[1];

if ($nowMonth <= $month) {
    if ($nowDay <= $day) {
        echo<<<NEXTDATE
        <li class="next"><?php echo link_to_next_item_show(); ?></li> //This is the custom code
NEXTDATE;
    } 
} 
?>

I feel that there is something wrong with my IFs statement.

Upvotes: 0

Views: 253

Answers (3)

Hafiz Hanif
Hafiz Hanif

Reputation: 471

Thank you all for trying your best at solving this problem. Maybe I didn't make it clear, that's why some of you were struggling, trying to understand what I was talking about.

I've tried to solve this problem using switch, so here is the answer that works for me.

<?php 

$oldDate = metadata('item', array('Dublin Core', 'Date')); 
$latestDate = explode("/", $oldDate);
$year = $latestDate[2];
$month = $latestDate[1];
$day = $latestDate[0];

$newDate = $month.'/'.$day.'/'.$year;
$nowDate = date('m/d/Y');

$nownowDate = explode("/", $nowDate);
$nowYear = $nownowDate[2];
$nowMonth = $nownowDate[0];
$nowDay = $nownowDate[1];

switch (true):

  case ($month == $nowMonth):
    if ($day < $nowDay) {
      echo '<li class="next">' . link_to_next_item_show() . '</li>';
    } else {
      echo " ";
    }
    break;

  case ($month < $nowMonth):
    echo '<li class="next">' . link_to_next_item_show() . '</li>';
    break;

  case ($month > $nowMonth):
    echo " "; 
    break;
  default :
    echo " ";
    break;
endswitch;

?>

Thank you to @developerwjk for the correction on using php tag inside heredoc. Now I know what's wrong with the code. I'm providing this as an answer so that other people will benefit from this, if they are trying to compare between two dates (comparing between day and month only, regardless of its year). Hope this is useful for others in the future.

Upvotes: 0

developerwjk
developerwjk

Reputation: 8659

You cannot put <?php tags inside a heredoc. So, instead of using a heredoc you could do:

echo '<li class="next">' . link_to_next_item_show() . '</li>';

Upvotes: 0

Matt
Matt

Reputation: 5428

From: Elegant way to get the count of months between two dates?

$timezone = new DateTimeZone('America/New_York'); 
$d1 = new DateTime("1815-02-26", $timezone);
$d2 = new DateTime("2015-01-01", $timezone);

var_dump($d1->diff($d2)->m); // int(4)
var_dump($d1->diff($d2)->d); // int(4)

if(($d1->diff($d2)->m) && ($d1->diff($d2)->d)){
   echo "run code here";
}

Upvotes: 2

Related Questions