0

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.

Hafiz Hanif
  • 361
  • 1
  • 3
  • 20
  • Are you getting any form of error? I can't say I am familiar with the <<< syntax, but your if statements look OK. – CT14.IT Mar 02 '15 at 16:44
  • `I feel that there is something wrong with my IFs statement.` Why? You need to describe the actual problem, not just state that you have a vague feeling. – developerwjk Mar 02 '15 at 16:44
  • @developerwjk The problem is: in the custom code, there is a block of html + php code that needed to be echoed once the condition is true, but I can't get the custom code to display, as if something's wrong with the IF statement. I can't figure it out. – Hafiz Hanif Mar 02 '15 at 17:14
  • 2
    @CT14.IT it's called heredoc http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc – Hafiz Hanif Mar 02 '15 at 17:15
  • In the example you've provided, the if statements are fine. If I change oldDate to e.g. "26/04/2015" I get the "Custom code" output. I do not think anyone here can help until we actually see the actual code that's the problem. – CT14.IT Mar 02 '15 at 17:19
  • @CT14.IT I've added the custom code. I think there's a problem validating the day. I'm trying here as well. – Hafiz Hanif Mar 02 '15 at 17:38

3 Answers3

2

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";
}
Community
  • 1
  • 1
Matt
  • 5,315
  • 1
  • 30
  • 57
0

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>';
developerwjk
  • 8,619
  • 2
  • 17
  • 33
0

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.

Hafiz Hanif
  • 361
  • 1
  • 3
  • 20