Jump to content

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
Edited by thebasix
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.