jhombalz
jhombalz

Reputation: 9

How to search birthday from December to January in MySQL?

This is for my OJT project. I need to search birthday from MySQL database in DATE type. I already got the query from January to December. The December to January is the problem.

This is what i've done code so far:

<?php
require 'connect.php';
?>
<?php
    if(isset($_POST['birthday_from']) && isset($_POST['birthday_to'])){

 $start_date=$_POST['birthday_from'];
    $end_date=$_POST['birthday_to'];

     //$s = strtolower($end_date);
    $start_year = date('m-d', strtotime($start_date));
    $end_year = date('m-d', strtotime($end_date));

$search_year = date('Y', strtotime($start_date));
$search_year2 = date('Y', strtotime($end_date));


    $sql=" select * from members where (DATE_FORMAT(`birthday`, '%m-%d') BETWEEN '{$start_year}' AND '{$end_year}') order by DATE_FORMAT(`birthday`, '%m-%d') asc ";
        $result = mysql_query($sql);
        $num_row = mysql_num_rows($result);
        if($num_row >= 1){
                echo '<div class="alert alert-info" style="margin:auto;width:900px;"><b><center>BIRTHDAY RESULT</center></b></div>';
            while($rows = mysql_fetch_array($result)){
                $date = date('Y-m-d')-$rows['birthday'];
                echo '<b><span style="margin-left:20px;">Name:</span></b> '.strtoupper($rows['firstname'].' '.$rows['middlename'].' '.$rows['lastname'].' '.$rows['suffix']).'<br/>';
                echo '<b><span>Birthday:</span></b> '.$rows['birthday'].'<br/>';
                echo '<b><span style="margin-left:30px;">Age: </span></b>'.$date.'<br/><br />';
            }


    }
    }
?>

Upvotes: 0

Views: 274

Answers (2)

Solo.dmitry
Solo.dmitry

Reputation: 770

  1. You do not check the case when start date > end date. It is happens.
  2. You named var $start_year = date('m-d', strtotime($start_date)), but there is no any Year in value. It is bad practice.
  3. You do not use vars search_year, search_year2 after defining and assigning.
  4. I do not understand what You want, can You give the example with several rows in table and result what You want to get but You cannot?

Upvotes: 2

Rebecca Dessonville
Rebecca Dessonville

Reputation: 640

Looks like you're using the wrong variables for your BETWEEN statement.

BETWEEN '{$start_year}' AND '{$end_year}'

should be:

BETWEEN '{$seach_year}' AND '{$search_year2}'

Upvotes: 0

Related Questions