drkstr Posted September 14, 2006 Share Posted September 14, 2006 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):[code]#global var used for month checkingdefine( '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; }}[/code]And here is the output of a sample set:checking ifobject(Date)#2 (6) { ["myDay"]=> int(31) ["myMonth"]=> string(3) "Aug" ["myYear"]=> int(2006) ["myHour"]=> int(9) ["myMin"]=> int(13) ["mySec"]=> int(21)}is greater thenobject(Date)#4 (6) { ["myDay"]=> int(0) ["myMonth"]=> string(5) "undef" ["myYear"]=> int(0) ["myHour"]=> int(0) ["myMin"]=> int(0) ["mySec"]=> int(0)}[color=red]Months: string Aug > string undef[/color]checking ifobject(Date)#4 (6) { ["myDay"]=> int(31) ["myMonth"]=> string(3) "Aug" ["myYear"]=> int(2006) ["myHour"]=> int(9) ["myMin"]=> int(25) ["mySec"]=> int(13)}is greater thenobject(Date)#2 (6) { ["myDay"]=> int(31) ["myMonth"]=> string(3) "Aug" ["myYear"]=> int(2006) ["myHour"]=> int(9) ["myMin"]=> int(13) ["mySec"]=> int(21)}checking ifobject(Date)#5 (6) { ["myDay"]=> int(1) ["myMonth"]=> string(3) "Sep" ["myYear"]=> int(2006) ["myHour"]=> int(8) ["myMin"]=> int(14) ["mySec"]=> int(31)}is greater thenobject(Date)#2 (6) { ["myDay"]=> int(31) ["myMonth"]=> string(3) "Aug" ["myYear"]=> int(2006) ["myHour"]=> int(9) ["myMin"]=> int(13) ["mySec"]=> int(21)}[color=red]Months: string Sep > string Aug[/color]checking ifobject(Date)#2 (6) { ["myDay"]=> int(7) ["myMonth"]=> string(3) "Sep" ["myYear"]=> int(2006) ["myHour"]=> int(21) ["myMin"]=> int(31) ["mySec"]=> int(2)}is greater thenobject(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 Quote Link to comment Share on other sites More sharing options...
Jenk Posted September 14, 2006 Share Posted September 14, 2006 convert to unixtimestamp (see mktime()), then:[code]return ($date1 > $date2);[/code] Quote Link to comment Share on other sites More sharing options...
drkstr Posted September 14, 2006 Author Share Posted September 14, 2006 neto! Thanks!...drkstr Quote Link to comment Share on other sites More sharing options...
drkstr Posted September 14, 2006 Author Share Posted September 14, 2006 Yup, that worked perfectly.updated code:[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); }}[/code]Thanks for the help!...drkstr Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.