LaserBeak
LaserBeak

Reputation: 3285

Trying in vain to covert javascript date time string to PHP DateTime

As sent from client: 'Mon Mar 14 2016 15:00:00 GMT+1100 (AEDT)'

$btime = $jd["bookingtime"];

$btime = substr($btime, 0, strpos($btime, '('));

$jobdetail->bookingtime = date('Y-m-d h:i:s', strtotime($btime));

Records are stored in MySQL database as such: 2016-03-14 15:00:00

Upvotes: 0

Views: 69

Answers (1)

Zimmi
Zimmi

Reputation: 1599

As your PHP is set to UTC default time zone you can set it with date_default_timezone_set() and find your timezone. Have a look here for australian timezones.

If you have access to your php.ini file, you can set in it the parameter date.timezone to the timezone you need. If the setting is not set, it will be UTC by default. AFAIK there should be a warning echoed when using datefunction in newer versions of php if there is no timezone set.

Else you can store it in UTC, as you mentionned, and convert to localtime according to your needs. If users are from many different timezones, UTC would be easier.

Date are automagically converted in the timezone you setted with date.timezone or with the function date_default_timezone_set() :

$d = 'Mon Mar 14 2016 15:00:00 GMT+1100';

$date = date('Y-m-d h:i:s', strtotime($d));
var_dump($date);                        // echoes '2016-03-14 04:00:00'
var_dump(date_default_timezone_get());  // echoes 'UTC'

date_default_timezone_set ('Europe/Zurich');
var_dump(date_default_timezone_get());      // echoes 'Europe/Zurich'
$date = date('Y-m-d h:i:s', strtotime($d)); // echoes '2016-03-14 05:00:00'
var_dump($date);

Upvotes: 1

Related Questions