adamriley Posted April 4, 2010 Share Posted April 4, 2010 hi i am new to php and this code is not working what i want happen is when the $date is higher than 1 and the $month higher than 4 then display the homepage else display the countdown but it displays the countdown even when the date is past 1st of the 4th month ------------------------------------------------------------------------ index.php ------------------------------------------------------------------------- <?php require_once($_SERVER['DOCUMENT_ROOT'].'/other/includes/main.php'); // get varibles require_once($_SERVER['DOCUMENT_ROOT'].'/other/includes/Login_for_php_files_admin_only.php'); if ($date < '1' && $month < '04'){ // ($DATE IS THE 1st one in file) /* CLOSE PHP HERE BECAUSE WE WANT TO OUTPUT HTML */ ?> <?php include($_SERVER['DOCUMENT_ROOT'].'/other/includes/Homepage.php'); ?> <?php }else{ /* OPEN PHP FOR THE ELSE THEN CLOSE IT FOR HTML AGAIN */ ?> <?php include($_SERVER['DOCUMENT_ROOT'].'/Countdown/index.php'); ?> <?php } // OPEN PHP AGAIN FOR THE CLOSING BRACE ?> ----------------------------------------------------------------------------------------------- main.php ----------------------------------------------------------------------------------------------- <?php // time and dates $date = date("j"); // date | e.g 28 $month = date("m"); // month e.g 01 = jan , 12 = dec $Year = date("Y"); // Year e.g 2010 $min = date("i"); // | E.g ??:34:?? $hour = date("G"); // | E.g 18:??:?? $sec = date("s"); // | E.g ??:??:44 ?> Link to comment https://forums.phpfreaks.com/topic/197559-code-not-working/ Share on other sites More sharing options...
the182guy Posted April 4, 2010 Share Posted April 4, 2010 when the $date is higher than 1 and the $month higher than 4 then display the homepage else display the countdown If this is what you want then use greater than > not less than < Also: you're trying to do numeric calculations with string variables, you should get the variables as numeric before doing a calculation. Try this: change main.php to <?php // time and dates $date = (int)date("j"); // date | e.g 28 $month = (int)date("n"); // n = 1,2,3,28,29 m = 01,02,03 $Year = date("Y"); // Year e.g 2010 $min = date("i"); // | E.g ??:34:?? $hour = date("G"); // | E.g 18:??:?? $sec = date("s"); // | E.g ??:??:44 ?> Then change the line in index.php to if ($date > 1 && $month > 4){ // ($DATE IS THE 1st one in file) Link to comment https://forums.phpfreaks.com/topic/197559-code-not-working/#findComment-1036838 Share on other sites More sharing options...
Pikachu2000 Posted April 4, 2010 Share Posted April 4, 2010 You're using the wrong comparison operators. If you want the date to be greater than you need to use greater than comparisons: > not less than : < Link to comment https://forums.phpfreaks.com/topic/197559-code-not-working/#findComment-1036839 Share on other sites More sharing options...
adamriley Posted April 4, 2010 Author Share Posted April 4, 2010 ok thanks for your reply but it still includes the countdown not the homepage Link to comment https://forums.phpfreaks.com/topic/197559-code-not-working/#findComment-1036840 Share on other sites More sharing options...
Pikachu2000 Posted April 4, 2010 Share Posted April 4, 2010 Why are you in and out of PHP so much? There is no need to do so just to include a file. The only time you would need to do that is to include raw HTML, outside of an echo() in your script. <?php echo '<p>This is echo\'d in the php script tags.</p>'; ?> <p>This is printed outside the PHP tags</p> Still, I don't see any reason why that won't work. Have you echo()'d the variables to make sure they hold the values you expect them to hold? <?php require_once($_SERVER['DOCUMENT_ROOT'].'/other/includes/main.php'); // get varibles require_once($_SERVER['DOCUMENT_ROOT'].'/other/includes/Login_for_php_files_admin_only.php'); // echo out your $date and $month variables here to see what values they hold . . . echo "Date: " . $date . " Month: " .$month; if ( $date > 1 && $month > 4 ) { include($_SERVER['DOCUMENT_ROOT'].'/other/includes/Homepage.php'); } else { include($_SERVER['DOCUMENT_ROOT'].'/Countdown/index.php'); } ?> Link to comment https://forums.phpfreaks.com/topic/197559-code-not-working/#findComment-1036841 Share on other sites More sharing options...
adamriley Posted April 4, 2010 Author Share Posted April 4, 2010 If i echo the $date and $month i get this ----------------------------------------------------------- Date: 4 Month: 4 Link to comment https://forums.phpfreaks.com/topic/197559-code-not-working/#findComment-1036845 Share on other sites More sharing options...
the182guy Posted April 4, 2010 Share Posted April 4, 2010 If i echo the $date and $month i get this ----------------------------------------------------------- Date: 4 Month: 4 So in your script your saying the month must be greater than 4. But the month is 4 (so it is not greater than 4), this is why it includes the countdown. Did you want to use greater than or equal to? Which is >= instead of just > if ( $date >= 1 && $month >= 4 ) { Link to comment https://forums.phpfreaks.com/topic/197559-code-not-working/#findComment-1036847 Share on other sites More sharing options...
Pikachu2000 Posted April 4, 2010 Share Posted April 4, 2010 Which means it is working. Since $date = 4, and 4 is greater than 1, the first part of the conditional passes. Since $month = 4, and 4 is NOT greater than 4, the second part, and consequently, the entire conditional fails, sending you to the else {} statement. On that note, I think I just realized what it is you are really trying to do here. If you want to go to Homepage.php if the date is later than April 1, you can change your conditional to if( $date >= 1 && $month >= 4 ) { HOWEVER, this will revert to sending the user to the countdown page after Dec. 31. EDIT: In retrospect, there really isn't any need to even check the $date variable in the conditional, since date('j') will ALWAYS be greater than or equal to 1. To check month and year, it can be written if( $month >= 4 && $year >= 2010 ) { Link to comment https://forums.phpfreaks.com/topic/197559-code-not-working/#findComment-1036852 Share on other sites More sharing options...
adamriley Posted April 4, 2010 Author Share Posted April 4, 2010 Ok thanks very much it know works like i want it to Link to comment https://forums.phpfreaks.com/topic/197559-code-not-working/#findComment-1036857 Share on other sites More sharing options...
Pikachu2000 Posted April 4, 2010 Share Posted April 4, 2010 Make sure you see my edit above . . . Link to comment https://forums.phpfreaks.com/topic/197559-code-not-working/#findComment-1036860 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.