I have to set up a time command for my friend (using twitch's nightbot) and can't figure out how to print the current time.
I've tried using this:
<?php
$date = date_create(null, timezone_open('America/Los_Angeles'));
$tz = date_timezone_get($date);
echo date(" H:i", time());
?>
This however returns the current server time (I'm fairly certain) instead of the timezone I selected.
I've used several different things but none of them work. They either display nothing, or display the wrong time.
Try using the DateTime()
class - http://php.net/manual/en/datetime.construct.php
$date = new DateTime(null, new DateTimeZone('America/Los_Angeles'));
echo $date->format('H:i');
note the null
- from the docs :: Enter NULL here to obtain the current time when using the $timezone parameter.
Are you using PHP 5.2 or greater? You can use the DateTime class to help you out. This answer to another related question will probably help: https://stackoverflow.com/a/14595648/1153203
Here is a DateTime example from linked answer:
$datetime = new DateTime($dbTimestamp, $timezone);
echo $datetime->format('Y-m-d H:i:s');
$datetime->setTimezone(new DateTimeZone('Pacific/Nauru'));
echo $datetime->format('Y-m-d H:i:s');