hyster Posted November 30, 2016 Share Posted November 30, 2016 I'm trying to get an if statement to determine inbetween 2 dates. I can get it working if its older than -7 days (single argument) but when I try to add the 2nd condition I get syntax error $timestamp is coming from a json - unix timestamp $timestamp = $output2['data'][$acc_id]['last_battle_time']; $my_date = date('Y-m-d',$timestamp); if( strtotime($my_date) > strtotime('-7 day') && < strtotime('-14 day') ) { echo ' 7'; } Quote Link to comment Share on other sites More sharing options...
hyster Posted November 30, 2016 Author Share Posted November 30, 2016 I made a basic error :$ now I'm stuck with the following, noting happens but the code "executes" I'm trying to get when the person was last online in a weekly group, 7 - 14 - 21 -28 - over days days ago $timestamp = $output2['data'][$acc_id]['last_battle_time']; $my_date = date('Y-m-d',$timestamp); if( strtotime($my_date) >= strtotime('-1 day') && strtotime($my_date) <= strtotime('-7 day') ) { echo ' 7'; }elseif( strtotime($my_date) >= strtotime('-7 day') && strtotime($my_date) <= strtotime('-14 day') ) { echo ' 14'; }elseif( strtotime($my_date) >= strtotime('-14 day') && strtotime($my_date) <= strtotime('-21 day') ) { echo ' 21'; }elseif( strtotime($my_date) >= strtotime('-21 day') && strtotime($my_date) <= strtotime('-28 day') ) { echo ' 28'; } Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted November 30, 2016 Solution Share Posted November 30, 2016 try $timestamp = $output2['data'][$acc_id]['last_battle_time']; $dt1 = new DateTime(); $dt1->setTimestamp($timestamp); $dt2 = new DateTime(); echo ceil($dt1->diff($dt2)->days / 7) * 7; 1 Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 30, 2016 Share Posted November 30, 2016 Aside from Barand's response, I would add the following comments on your code. In the first code you posted, the conditions in the if() clause was wrong. Each condition joined with AND/OR clauses must be independent. if( strtotime($my_date) > strtotime('-7 day') && < strtotime('-14 day') ) { The red condition is incomplete. On your second posted code, the conditions are impossible! if( strtotime($my_date) >= strtotime('-1 day') && strtotime($my_date) <= strtotime('-7 day') ) {echo ' 7';} A date cannot be greater than yesterday (-1) AND also be less than 7 days ago. The operators need to be reversed. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.