You can use createFromFormat
method of DateTime
or, better, DateTimeImmutable
while passing timezone as third parameter. This way you do not need to rely on default timezone, which is never a good idea
$datetime = DateTimeImmutable::createFromFormat('Y-m-d H:i', '2016-03-22 14:30', new DateTimeZone('Australia/Sydney'));
echo $datetime->getTimestamp();
echo $datetime->format(DateTime::ISO8601);
You can also convert it to another timezone, note that it produced new DateTimeImmutable and original left untouched:
echo $utcTzDatetime = $date->setTimezone(new DateTimeZone('UTC'));
echo $utcTzDatetime->format(DateTime::ISO8601);
echo $datetime->format(DateTime::ISO8601);
Upd:
If format is not fixed, you can let DateTime guess it:
new DateTimeImmutable($time, new DateTimeZone('Australia/Sydney'));
But be aware that if $time
string contains timezone or offset, eg '2016-03-22T14:30-0500', it will have priority over timezone parameter and will result in different timestamp!