Jump to content

Comparing Dates


IAmBroom

Recommended Posts

So, the code below should be simple enough. I compare three dates: today (2/16/2014), a date just over a week from now (2/25/2014), and a date one month from now (3/16/2014). It should be Now < InOneWeek < OneMonthFromNow, but instead I bizarrely get the following output:

 

Current Date = 2014-02-16 = 1392557618Event Date = 2014-02-28 = 1393

570800Offset Date = 2014-03-16 = 13949768182014-02-28 < 2014-02-162014-02-16 >= 2014-02-282014-02-28 < 2014-03-162014-03-16 >= 2014-02-28

I've highlighted the problem. Code follows; I can't see any reason why the date values should line up as

 

2/25/2014 < 2/16/2014 < 3/16/2014

<?php   // Get the current date.   // DateTime defaults to current time.   $datCurr = new DateTime();   $strCurrDate = date_format($datCurr, "Y-m-d");   echo "Current Date = " . $strCurrDate . " = " . date_timestamp_get($datCurr) . "</br>";   // Convert the event date object to a string.   $datEvent = new DateTime("Feb 28, 2014");   $strEventDate = date_format($datEvent, "Y-m-d");   echo "Event Date = " . $strEventDate . " = " . date_timestamp_get($datEvent) . "</br>";   // Sets an interval of 1 month.   $intvlOffsetPeriod = new DateInterval('P1M');   $dat1OffsetPeriodFromNow = $datCurr->add($intvlOffsetPeriod);   $str1OffsetPeriodFromNow = date_format($dat1OffsetPeriodFromNow, "Y-m-d");   echo "Offset Date = " . $str1OffsetPeriodFromNow . " = " . date_timestamp_get($dat1OffsetPeriodFromNow) . "</br>";   echo "</br>";   if ( date_timestamp_get($datEvent) < date_timestamp_get($datCurr) ) {      echo "$strEventDate < $strCurrDate</br>";   }   if ( date_timestamp_get($datEvent) >= date_timestamp_get($datCurr) ) {      echo "$strEventDate >= $strCurrDate</br>";   }   if ( date_timestamp_get($datCurr) < date_timestamp_get($datEvent) ) {      echo "$strCurrDate < $strEventDate</br>";   }   if ( date_timestamp_get($datCurr) >= date_timestamp_get($datEvent) ) {      echo "$strCurrDate >= $strEventDate</br>";   }   echo "</br>";   if ( date_timestamp_get($datEvent) < date_timestamp_get($dat1OffsetPeriodFromNow) ) {      echo "$strEventDate < $str1OffsetPeriodFromNow</br>";   }   if ( date_timestamp_get($datEvent) >= date_timestamp_get($dat1OffsetPeriodFromNow) ) {      echo "$strEventDate >= $str1OffsetPeriodFromNow</br>";   }   if ( date_timestamp_get($dat1OffsetPeriodFromNow) < date_timestamp_get($datEvent) ) {      echo "$str1OffsetPeriodFromNow < $strEventDate</br>";   }   if ( date_timestamp_get($dat1OffsetPeriodFromNow) >= date_timestamp_get($datEvent) ) {      echo "$str1OffsetPeriodFromNow >= $strEventDate</br>";   }?>

Help me, Obi Wan Kenobi - you're my only hope!

Link to comment
Share on other sites

You're mixing procedural method with object-oriented methos. You should stick with one or another.

if ( $datEvent->getTimestamp() < $datCurr->getTimestamp() ) {    echo "$strEventDate < $strCurrDate</br>";}

Another problem you have is that the DateTime::add() method will manipulate the actual date you're adding to.

In the following line of code, both $dat1OffsetPeriodFromNow and $datCurr will have the same value.

$dat1OffsetPeriodFromNow = $datCurr->add($intvlOffsetPeriod);
Link to comment
Share on other sites

Another problem you have is that the DateTime::add() method will manipulate the actual date you're adding to.

In the following line of code, both $dat1OffsetPeriodFromNow and $datCurr will have the same value.

$dat1OffsetPeriodFromNow = $datCurr->add($intvlOffsetPeriod);

You found it! Thanks!!!

 

And... I promise to stick to one method. I just learned how to do all this last night... I'm a bit green!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...