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
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 =="")
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();