ToniWidmo
ToniWidmo

Reputation: 509

Accepting international datetime input in php

I'm currently adding support for internationalisation to a system written in PHP. All dates are now stored as UTC, and displayed according to individual user localisation preferences.

However when a user inputs a date time (such as to specify a certain time window), the date time they input gets interpreted as a UTC datetime, not their local datetime. For the internationalisation to be complete the system needs to assume that a datetime entered by the user refers to their local time.

How do I convert a date string (ie 'YYYY-MM-DD HH:MM') into a unix timestamp for the correct localisation?

Upvotes: 0

Views: 571

Answers (1)

deceze
deceze

Reputation: 522015

Assuming you know the timezone of the user, which he presumably chose in the preferences somewhere:

$timezone = new DateTimeZone($usersTimezone);
$datetime = new DateTime('2012-01-18 20:00:00', $timezone);
echo $datetime->getTimestamp();

This requires a recent version of PHP with DateTime.

Upvotes: 2

Related Questions