thebasix Posted May 19, 2015 Share Posted May 19, 2015 $month = ""; #First part of printing javascript code echo "{\"elapsed\": \""; #This line works and I can print it $tmp = date("F", $date); #I cannot print $month. I can print $tmp before if-statement, but I cant print $month inside... if($tmp !== $month){ $month = $tmp; echo $month; } #Second part of printing javascript code echo "\", \"value\":" . $profit_sum . "},"; Why cant I get inside if-statement? I simply cant print out $month. Any suggestions? Regards to all! Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted May 20, 2015 Share Posted May 20, 2015 The part you do not show in code is where the $date variable comes from or what it's value could be. Going by your code the $date value should be a timestamp. If tried this would work. date_default_timezone_set('America/New_York');//set server timezone $month = ""; $date = time(); //timestamp $tmp = date("F",$date);//formatted date from timestamp showing full month if($tmp !== $month){ $month = $tmp; echo $month."<br />"; }else{ echo "Was the month $month<br />"; } If your date is already a formatted date then would want to use strtotime() on it. date_default_timezone_set('America/New_York');//set server timezone $month = ""; $date = date('Y-m-d h:i:s A');//string formatted date $tmp = date("F",strtotime($date));//converted the string formatted date to timestamp if($tmp !== $month){ $month = $tmp; echo $month."<br />"; }else{ echo "Was the month $month<br />"; } Quote Link to comment Share on other sites More sharing options...
thebasix Posted May 20, 2015 Author Share Posted May 20, 2015 (edited) Still cant make it work. I added some more code. This is all related to date. Basically I have for-loop that goes through database and searches for $date and it does something ( I wont write that code) and at the end it increase $date by 1 day. When new month appears (january, feb, mar) for the first time, I want to print it out. That's the point of the if statement. $date = "2015-01-01"; $month = ""; for(...){ #HERE Im getting * from database that matches the current date, starting from 1-1-2015 echo "{\"elapsed\": \""; $tmp = date("F", $date); if($tmp !== $month){ $month = $tmp; echo $month; } echo "\", \"value\":" . $profit_sum . "},"; #increasing the date by 1 day #will search for next date $date = strtotime("+1 day", strtotime($date)); $date = date("Y-m-d", $date); }#for loop ends Edited May 20, 2015 by thebasix Quote Link to comment Share on other sites More sharing options...
AP81 Posted May 20, 2015 Share Posted May 20, 2015 Have you tried just echoing out the variables to see what the actual values are? i.e. echo "tmp = $tmp <br />"; echo "month = $month <br />"; if($tmp !== $month){ Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 20, 2015 Share Posted May 20, 2015 As QuickOldCar stated previously If your date is already a formatted date then would want to use strtotime() on it. Yet, you have this $date = "2015-01-01"; . . . $tmp = date("F", $date); 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.