Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Feb 2006
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question why i cant compare the integer??

    hi everyone, i hav a problem here.....

    Code:
    <?php
    $answer = datediff("1984-05-02","now","y");
    if($answer > 5){
    echo "half year";
    }else{
    echo "not yet";
    }
    ?>
    datediff() actually is a function that will print the age. so the age is 23. but for if else statement, 23 > 5 will print "not yet"...it should print "half year" rite??? but,when i change to if($answer = 23), it will print "half year"....

    can u all pls help me??

  • #2
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Try posting the datediff function as well.
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,456
    Thanks
    8
    Thanked 1,084 Times in 1,075 Posts
    if($answer = 23) is always true ...

    if($answer == 23) may or may not be true.

  • #4
    New Coder
    Join Date
    Jul 2006
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    echo out $answer and see what you're getting.

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,009
    Thanks
    2
    Thanked 312 Times in 304 Posts
    datediff() actually is a function that will print the age
    If it prints the age to the browser, it probably returns nothing and $answer will be a NULL. The comparison will be if(NULL > 5).
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #6
    Regular Coder
    Join Date
    Feb 2006
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the code of datediff() as follow:

    Code:
    	function datediff($start_date,$end_date="now",$unit="D")
    		{
    			$unit = strtoupper($unit);
    			$start=strtotime($start_date);
    			if ($start === -1) {
    				print("invalid start date");
    			}
    			
    			$end=strtotime($end_date);			
    			if ($end === -1) {
    				print("invalid end date");
    			}
    			
    			if ($start > $end) {
    				$temp = $start;
    				$start = $end;
    				$end = $temp;
    			}
    			
    			$diff = $end-$start;
    			
    			$day1 = date("j", $start);
    			$mon1 = date("n", $start);
    			$year1 = date("Y", $start);
    			$day2 = date("j", $end);
    			$mon2 = date("n", $end);
    			$year2 = date("Y", $end);
    			
    			switch($unit) {
    				case "D":
    					print(intval($diff/(24*60*60)));
    					break;
    				case "M":
    					if($day1>$day2) {
    						$mdiff = (($year2-$year1)*12)+($mon2-$mon1-1);
    					} else {
    						$mdiff = (($year2-$year1)*12)+($mon2-$mon1);
    					}
    					print($mdiff);
    					break;
    				case "Y":
    					if(($mon1>$mon2) || (($mon1==$mon2) && ($day1>$day2))){
    						$ydiff = $year2-$year1-1;
    					} else {
    						$ydiff = $year2-$year1;
    					}
    					print($ydiff);
    					break;
    				case "YM":
    					if($day1>$day2) {
    						if($mon1>=$mon2) {
    							$ymdiff = 12+($mon2-$mon1-1);
    						} else {
    							$ymdiff = $mon2-$mon1-1;
    						}
    					} else {
    						if($mon1>$mon2) {
    							$ymdiff = 12+($mon2-$mon1);
    						} else {
    							$ymdiff = $mon2-$mon1;
    						}
    					}
    					print($ymdiff);
    					break;
    				case "YD":
    					if(($mon1>$mon2) || (($mon1==$mon2) &&($day1>$day2))) {
    						$yddiff = intval(($end - mktime(0, 0, 0, $mon1, $day1, $year2-1))/(24*60*60));						
    					} else {
    						$yddiff = intval(($end - mktime(0, 0, 0, $mon1, $day1, $year2))/(24*60*60));
    					}
    					print($yddiff);
    					break;
    				case "MD":
    					if($day1>$day2) {
    						$mddiff = intval(($end - mktime(0, 0, 0, $mon2-1, $day1, $year2))/(24*60*60));						
    					} else {
    						$mddiff = intval(($end - mktime(0, 0, 0, $mon2, $day1, $year2))/(24*60*60));
    					}
    					print($mddiff);
    					break;
    				default:
         			print("{Datedif Error: Unrecognized \$unit parameter. Valid values are 'Y', 'M', 'D', 'YM'. Default is 'D'.}");
    				
    			}
    
    		}
    is it i need to return something here???sorry,i'm a newbie in php.
    can u all pls help me????

  • #7
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    yeah, rather than printing the age, you need to return it
    e.g.
    PHP Code:
    case "D":
         return 
    intval($diff/(24*60*60)); 
    (you don't need to break, as returning will end the function)
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #8
    Regular Coder
    Join Date
    Feb 2006
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i have tried the "return" method that you mentioned above.....but the problem still remain the same.....any other way??????
    one more thing,i have also echo the $answer, but can display anything...is it means nth was pass to this page?????

    help me pls...
    Last edited by tanhaha_how; 02-07-2007 at 04:11 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •