0

Variable $now won't display unless it is dumped. It's populated. It doesn't display the "now is empty" string at all. It won't display what is inside $now unless it is var_dump or var_exported.

$ipData = json_decode( $ttt, true);
$now="";
if ($ipData['timezone']) {
    $tz = new DateTimeZone( $ipData['timezone']);
    $now = new DateTime( 'now', $tz); // DateTime object corellated to user's timezone
} else {
   // we can't determine a timezone - do something else...
}



if($now==""){echo "now is empty";}

echo "<br />
<br />
".$now->timezone.$now->date;
var_dump($now);

}

if(isset($now)){echo "It is set.";} It's definitely set, though I set it to "" before I actually called it, so no matter what it was initialized.

Even removing the $now=""; shows that it is initialized. var_dump($now); echos this:

object(DateTime)#30 (3) { ["date"]=> string(19) "2013-05-06 22:52:29" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "America/New_York" } After var_dump $now->timezone is equal to America/New_York

  • var_dump($now); echos this: object(DateTime)#30 (3) { ["date"]=> string(19) "2013-05-06 22:52:29" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "America/New_York" } After var_dump $now->timezone is equal to America/New_York – user2352040 May 07 '13 at 02:53

2 Answers2

1

DateTime returns new DateTime object and not a string for echo. If you like to echo date part of the $now , you should use format, like below

echo $now->format('Y-m-d H:i:s'); 

and if you like echo the timezone of the $now you should use gettimezone and getname, like below

echo $now->getTimezone()->getName(); 

Then both of them should be like below

 echo $now->format('Y-m-d H:i:s').$now->getTimezone()->getName();
Amir
  • 4,089
  • 4
  • 16
  • 28
0

Make sure you are actually entering that IF condition. You can try isset($var) to see if the var was initialized, or empty($var) to see if it has something inside. [Instead of =="")

yBrodsky
  • 4,981
  • 3
  • 20
  • 31
  • if(isset($now)){echo "It is set.";} It's definitely set, though I set it to "" before I actually called it, so no matter what it was initialized. Even removing the $now=""; shows that it is initialized. var_dump($now); echos this: object(DateTime)#30 (3) { ["date"]=> string(19) "2013-05-06 22:52:29" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "America/New_York" } After var_dump $now->timezone is equal to America/New_York – user2352040 May 07 '13 at 02:53