PHP time() in 'if statements' not passed to MySql query?

if($_SESSION['periode']){

    if($_SESSION['periode'] == 'Today'){
    $xdate = time() - (1 * 24 * 60 * 60);
    $ydate = time();
    }
    if($_SESSION['periode'] == 'Yesterday'){
    $xdate = time() - (2 * 24 * 60 * 60);
    $ydate = time() - (1 * 24 * 60 * 60);
    }
    if($_SESSION['periode'] == 'This week'){
    $xdate = time() - (7 * 24 * 60 * 60);
    $ydate = time() - (2 * 24 * 60 * 60);
    }
    if($_SESSION['periode'] == 'Older'){
    $xdate = 0;
    $ydate = time() - (7 * 24 * 60 * 60);
    }
    else{
    $xdate = 0;
    $ydate = time();
    }
}
else {
$xdate = 0;
$ydate = time();
}

$tweets = mysql_query("SELECT * FROM messages WHERE content LIKE '%$search%' 
AND content LIKE '%$region%' 
AND time BETWEEN $xdate AND $ydate ORDER BY id DESC LIMIT 10");

The problem: all messages are selected from the database and not only those between $xdate and $ydate.

If I manually fill in a unix time period in the else statement however (and don't define $_SESSION['periode']) the correct messages are picked out of the DB. What's going on??

This question and answers originated from www.stackoverflow.com
Question by (2/16/2011 1:32:52 PM)

Answer

if($_SESSION['periode'] == 'Older'){
    $xdate = 0;
    $ydate = time() - (7 * 24 * 60 * 60);
    }
else{
    $xdate = 0;
    $ydate = time();
    }

This code will always set $ydate to time() and $xdate to 0 if $_SESSION['periode'] is not 'Older', because this else is related to just above if. You need to use elseif rather than if

code must be

if($_SESSION['periode']){

    if($_SESSION['periode'] == 'Today'){
    $xdate = time() - (1 * 24 * 60 * 60);
    $ydate = time();
    }
    elseif($_SESSION['periode'] == 'Yesterday'){
    $xdate = time() - (2 * 24 * 60 * 60);
    $ydate = time() - (1 * 24 * 60 * 60);
    }
    elseif($_SESSION['periode'] == 'This week'){
    $xdate = time() - (7 * 24 * 60 * 60);
    $ydate = time() - (2 * 24 * 60 * 60);
    }
    elseif($_SESSION['periode'] == 'Older'){
    $xdate = 0;
    $ydate = time() - (7 * 24 * 60 * 60);
    }
    else{
    $xdate = 0;
    $ydate = time();
    }
}
else {
$xdate = 0;
$ydate = time();
}
Answer by

Find More Answers
Related Topics  php  mysql  time  if-statement
Related Questions
  • Formatting time in PHP rows

    I'm currently using this code: if(mysql_num_rows($result2) > 0) { $count=0; $day = 1; while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) { echo "<b>"; if ($da…
  • if statements in php using dates

    I want to write an IF statement based on two dates. I have a MySQL database (of which I have no control over) for tests and exams taken. One of the fields is labelled ClientTime and outputs the da…
  • Use IF in a mySQL query

    I'm trying to make a SELECT where I need to select a specified table by a column wich define the type of subject: table_buildings id name location source_type source_id 1 NY Appar…
  • PHP if Time or Date Greater than Show Echo

    This code does not want to compile. So I tried making the time text into a string. $swnow = date('10:00:01'); $vsp=$swnow; echo $vsp; if ($vsp > '10:00:01') {echo 'greater than 10';} I wa…
  • trying to save time with PHP if/elseif statements

    I have a rather big if statement: if (!$result_spam) { $confrim_spam = "FAILED"; } else if ($result_spam) { $confrim_spam = "PASSED"; } if (!$result_email_manage) { $confrim_email_manage = "FAI…
  • Wrong logic in If Statement?

    $repeat_times = mysql_real_escape_string($repeat_times); $result = mysql_query("SELECT `code`,`datetime` FROM `fc` ORDER by datetime desc LIMIT 25") or die(mysql_error()); $output .=""; $seconds…
  • php greater than certain time

    I am trying to make a simple function to output 2 different lines of text depending on the time of the day. I want it to say after 4pm - Next day delivery will be processed the following day. I h…
  • PHP Like thing similar to MySQL Like, for if statement?

    I want an if statement that uses same thingy like mysql something LIKE '%something%' I want to build an if statement in php. if ($something is like %$somethingother%) Is it possible? Th…
  • Are ampersands not allowed in php if-statements?

    This is how my if-statement starts: <?php if ( bbp_get_forum_title() == 'Business & Finance' ) : ?> If I write just 'Business' it works as expected. But I now that I placed an amper…
  • Regarding if statements in PHP

    I've seen some PHP statements that go something like if($variable) {} or if(function()) {} (if statements that don't compare two variables) and I know they roughly mean if a function execut…