Jump to content

Can't get through if-statement


thebasix

Recommended Posts

$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!

Link to comment
https://forums.phpfreaks.com/topic/296409-cant-get-through-if-statement/
Share on other sites

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 />";
}

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.