Jump to content


Photo

*SOLVED* conditional expressions driving me bonkers


  • Please log in to reply
3 replies to this topic

#1 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 14 September 2006 - 04:19 PM

Is there a good PHP manual page that will explain conditional expressions in detail? I'm getting strange results when comparing two objects.

Here's the object (I included some debug output):
#global var used for month checking
define( 'MONTHS', 'undef Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec' );

class Date {

    public $myDay   = 0;
    public $myMonth = 'undef';
    public $myYear  = 0;
    public $myHour  = 0;
    public $myMin   = 0;
    public $mySec   = 0;

    #pass Null to constructor for an "empty" object
    public function __construct( $_arDateVars=array() ) {
      if(! is_null($_arDateVars)) {
        $this->myDay    = (int) $_arDateVars[0];
        $this->myMonth  = (string) $_arDateVars[1];
        $this->myYear   = (int) $_arDateVars[2];
        $this->myHour   = (int) $_arDateVars[3];
        $this->myMin    = (int) $_arDateVars[4];
        $this->mySec    = (int) $_arDateVars[5];
      }
    }

    #returns True if the passed in date is greater then $this date.
    #otherwise returns False
    public function greater( $_newDate ) {
      print "checking if\n"; var_dump($_newDate);
      print "is greater then \n"; var_dump($this);
      switch (True) {
        case ( $_newDate.myYear > $this->myYear ):
          print "Year: ". gettype($_newDate->myYear)." ".$_newDate->myYear." > ".
                          gettype($this->myYear)." ".$this->myYear."\n";
          return True;
        case strpos(MONTHS, $_newDate->myMonth) > strpos(MONTHS, $this->myMonth):
          print "Months: ". gettype($_newDate->myMonth)." ".$_newDate->myMonth." > ".
                          gettype($this->myMonth)." ".$this->myMonth."\n";
          return True;
        case ( $_newDate.myDay > $this->myDay ) :
          print "Days: ". gettype($_newDate->myDay)." ".$_newDate->myDay." > ".
                          gettype($this->myDay)." ".$this->myDay."\n";
          return True;
        case ( $_newDate.myHour > $this->myHour):
          print "Hours: ". gettype($_newDate->myHour)." ".$_newDate->myHour." > ".
                          gettype($this->myHour)." ".$this->myHour."\n";
          return True;
        case ( $_newDate.myMin > $this->myMin):
          print "Mins: ". gettype($_newDate->myMin)." ".$_newDate->myMin." > ".
                          gettype($this->myMin)." ".$this->myMin."\n";
          return True;
        case ( $_newDate.mySec > $this->mySec):
          print "Sec: ". gettype($_newDate->mySec)." ".$_newDate->mySec." > ".
                          gettype($this->mySec)." ".$this->mySec."\n";
          return True;
      }

    return False;
  }
}

And here is the output of a sample set:


checking if
object(Date)#2 (6) {
  ["myDay"]=>
  int(31)
  ["myMonth"]=>
  string(3) "Aug"
  ["myYear"]=>
  int(2006)
  ["myHour"]=>
  int(9)
  ["myMin"]=>
  int(13)
  ["mySec"]=>
  int(21)
}
is greater then
object(Date)#4 (6) {
  ["myDay"]=>
  int(0)
  ["myMonth"]=>
  string(5) "undef"
  ["myYear"]=>
  int(0)
  ["myHour"]=>
  int(0)
  ["myMin"]=>
  int(0)
  ["mySec"]=>
  int(0)
}
Months: string Aug > string undef
checking if
object(Date)#4 (6) {
  ["myDay"]=>
  int(31)
  ["myMonth"]=>
  string(3) "Aug"
  ["myYear"]=>
  int(2006)
  ["myHour"]=>
  int(9)
  ["myMin"]=>
  int(25)
  ["mySec"]=>
  int(13)
}
is greater then
object(Date)#2 (6) {
  ["myDay"]=>
  int(31)
  ["myMonth"]=>
  string(3) "Aug"
  ["myYear"]=>
  int(2006)
  ["myHour"]=>
  int(9)
  ["myMin"]=>
  int(13)
  ["mySec"]=>
  int(21)
}
checking if
object(Date)#5 (6) {
  ["myDay"]=>
  int(1)
  ["myMonth"]=>
  string(3) "Sep"
  ["myYear"]=>
  int(2006)
  ["myHour"]=>
  int(8)
  ["myMin"]=>
  int(14)
  ["mySec"]=>
  int(31)
}
is greater then
object(Date)#2 (6) {
  ["myDay"]=>
  int(31)
  ["myMonth"]=>
  string(3) "Aug"
  ["myYear"]=>
  int(2006)
  ["myHour"]=>
  int(9)
  ["myMin"]=>
  int(13)
  ["mySec"]=>
  int(21)
}
Months: string Sep > string Aug
checking if
object(Date)#2 (6) {
  ["myDay"]=>
  int(7)
  ["myMonth"]=>
  string(3) "Sep"
  ["myYear"]=>
  int(2006)
  ["myHour"]=>
  int(21)
  ["myMin"]=>
  int(31)
  ["mySec"]=>
  int(2)
}
is greater then
object(Date)#5 (6) {
  ["myDay"]=>
  int(1)
  ["myMonth"]=>
  string(3) "Sep"
  ["myYear"]=>
  int(2006)
  ["myHour"]=>
  int(8)
  ["myMin"]=>
  int(14)
  ["mySec"]=>
  int(31)
}

As you can see, it's only returning True for the cases when the month is greater.  What am I doing wrong?

Thanks for the help!
...drkstr

#2 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 14 September 2006 - 04:45 PM

convert to unixtimestamp (see mktime()), then:
return ($date1 > $date2);


#3 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 14 September 2006 - 04:50 PM

neto! Thanks!

...drkstr

#4 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 14 September 2006 - 05:55 PM

Yup, that worked perfectly.

updated code:
define( 'MONTHS', 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec' );

class Date {

  protected $myDay   = 0;
  protected $myMonth = 0;
  protected $myYear  = 0;
  protected $myHour  = 0;
  protected $myMin   = 0;
  protected $mySec   = 0;

  protected $myTimeStamp=0;     #Unix time stamp used for comparason;

  public function __construct( $_arDateVars=array() ) {
    $nMonth=0;

    if(! is_null($_arDateVars)) {
      $this->myDay    = (int) $_arDateVars[0];
      $this->myMonth  = (int) (strpos(MONTHS, $_arDateVars[1])+4)/4;  #get month number
      $this->myYear   = (int) $_arDateVars[2];
      $this->myHour   = (int) $_arDateVars[3];
      $this->myMin    = (int) $_arDateVars[4];
      $this->mySec    = (int) $_arDateVars[5];

      $this->myTimeStamp = mktime( $this->myHour, $this->myMin, $this->mySec,
                                    $this->myMonth, $this->myDay, $this->myYear );
    }
  }

  #returns True if the passed in date is greater then $this date.
  public function greater( $_newDate ) {
    return ($_newDate->myTimeStamp > $this->myTimeStamp);
  }
}

Thanks for the help!
...drkstr




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users