0

I want to display all the records during August of the current year up to December of the current year. While it will not display on January up to June, Instead the Records of January up to June will be displayed. How will I catch the date using IF statement?

Here is my code:

            $year = date("Y");
            $sem1_start = date("M-d-Y", strtotime("August, 01".$year));
            $sem1_end = date("M-d-Y", strtotime("December, 31".$year));

            $sem2_start = date("M-d-Y", strtotime("January, 01".$year));
            $sem2_end = date("M-d-Y", strtotime("June, 15".$year));

            //sample date from database
            $date = '2015-10-15';
            $date2 = date("M-d-Y", strtotime($date));


            if($date2 >= $sem1_s && $date2 <= $sem1_e){
                //Display all records
            }
            else if($date2 >= $sem2_s && $date2 <= $sem2_e){
                 //Display all records
            }

I wanted to automatically display the records of the current semester (August-December) and when January comes, it will hide the records but will display the records of the second semester which is January up to June.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
its.L.A
  • 25
  • 3

2 Answers2

0

You could filter your sql result with the between statement. Something like

$query = "select * from db_table where datefield BETWEEN $startdate and $enddate order by datefield asc;

That way your results will always fall in range and are sorted to boot

sillysicko
  • 126
  • 7
  • better do not use the between statement. Is the start date include? is the end date included? Better use `datefield > $startdate and datefield < $enddate ` – inetphantom Nov 26 '15 at 18:38
  • Not so. Now you create two internal check on non integer fields, while otherwise you would use a standard build in function which is highly optimized for these cases. It will work though. But with large datasets it might be considerable slower. – sillysicko Nov 26 '15 at 18:42
  • Not so. have a look on [this question](http://stackoverflow.com/questions/2692593/between-operator-vs-and-is-there-a-performance-difference) – inetphantom Nov 26 '15 at 18:45
  • Maybe so. But that is with integer values. We are now taking about date values, which are basically formatted strings if you are not using timestamps. String searches are always slower then integer searches. – sillysicko Nov 26 '15 at 18:51
  • datefield > $startdate and datefield < $enddate, I did this but It can't catch the dates.. Why? – its.L.A Nov 27 '15 at 06:27
0

I think, in the database I have a field to save valuable time

$time = time();

When I want to search from August of the current year to March 12 of the current year I will do the following

$timeSearchStart = strtotime(date('Y') . '-8-1 00:00:00');
$timeSearchEnd = strtotime(date('Y') . '-12-31 23:59:59');
$query = "SELECT * FROM `table` WHERE `fieldSaveTime` > '" . $timeSearchStart . "' && `fieldSaveTime` < '" . $timeSearchEnd  . "'";
Canh Nguyen
  • 333
  • 1
  • 18
  • I use this $timeSearchStart = strtotime(date('Y') . '-8-1 00:00:00'); to assign the date but my problem is my IF statement can't catch the date on my database – its.L.A Nov 27 '15 at 06:26
  • @its.L.A you must use the time() function to save time in fieldSaveTime or you change same it – Canh Nguyen Nov 27 '15 at 09:59