Reputation: 25
I have many records in my database with different dates but my code doesn't catch the dates and it doesn't show up. What's the problem?
Here is my code
$year = date("Y");
$sem1_s = date("M-d-Y", strtotime(date('Y') . '-8-1 00:00:00'));
$sem1_e = date("M-d-Y", strtotime(date('Y') . '-12-31 00:00:00'));
$sem2_s = date("M-d-Y", strtotime(date('Y') . '-01-1 00:00:00'));
$sem2_e = date("M-d-Y", strtotime(date('Y') . '-06-30 00:00:00'));
$date = '2015-10-15';
$date2 = date("M-d-Y", strtotime(date('Y') . '-3-1 00:00:00'));
if($date2 >= $sem1_s && $date2 <= $sem1_e){
echo $date2;
echo "First Sem";
}
else if($date2 >= $sem2_s && $date2 <= $sem2_e){
echo $date2;
echo "Second Sem";
}
else{
echo "error";
}
Upvotes: 2
Views: 86
Reputation: 782
Try this in your if
statement as I mentioned earlier in the comment below your question, problem is with your if
statement. so change that to this:
if (strtotime($date2) >= strtotime($sem1_s) && strtotime($date2) <= strtotime($sem1_e))
Upvotes: 1
Reputation: 1418
Because you need to convert dates to UNIX timestamp for comparison.
The below will return the desired result,
if(strtotime($date2) >= strtotime($sem1_s) && strtotime($date2) <= strtotime($sem1_e)){
echo $date2;
echo "First Sem";
}
else if(strtotime($date2) >= strtotime($sem2_s) && strtotime($date2) <= strtotime($sem2_e)){
echo $date2;
echo "Second Sem";
}
else{
echo "error";
}
Upvotes: 0
Reputation: 1
I think you cannot use greater than or less than sign in a string you should convert your variables as a strtotime.
if(strtotime($date2) >= strtotime($sem1_s) && strtotime($date2) <= strtotime($sem1_e)){
echo $date2;
echo "First Sem";
}
else if(strtotime($date2) >= strtotime($sem2_s) && strtotime($date2) <= strtotime($sem2_e)){
echo $date2;
echo "Second Sem";
}
else{
echo "error";
Upvotes: 0
Reputation: 400
Try changing your if statement like this
$year = date("Y");
$sem1_s = date("M-d-Y", strtotime(date('Y') . '-8-1 00:00:00'));
$sem1_e = date("M-d-Y", strtotime(date('Y') . '-12-31 00:00:00'));
$sem2_s = date("M-d-Y", strtotime(date('Y') . '-01-1 00:00:00'));
$sem2_e = date("M-d-Y", strtotime(date('Y') . '-06-30 00:00:00'));
$date = '2015-10-15';
$date2 = date("M-d-Y", strtotime(date('Y') . '-3-1 00:00:00'));
if($date2 >= $sem1_s || $date2 <= $sem1_e){
echo $date2;
echo "First Sem";
}
else if($date2 >= $sem2_s || $date2 <= $sem2_e){
echo $date2;
echo "Second Sem";
}
else{
echo "error";
}
Upvotes: 0