Caal Saal VI
Caal Saal VI

Reputation: 302

Convert datepicker date time to php date time

Currently I'm using jquery datepicker that support date and time.

The return value is string

return value

"Fri Apr 10 2015 12:00:00 GMT+0800 (Malay Peninsula Standard Time)"

I wan to convert the above string to php date time to store into database .

I just need date time and timezone .

Upvotes: 2

Views: 1994

Answers (1)

yergo
yergo

Reputation: 4980

First you have to remove information, that breaks conversion:

preg_replace('/( \(.*)$/','','Fri Apr 10 2015 12:00:00 GMT+0800 (Malay Peninsula Standard Time)');

Results in:

'Fri Apr 10 2015 12:00:00 GMT+0800'

what you can convert to timestamp using strtotime():

$datetime = "Fri Apr 10 2015 12:00:00 GMT+0800 (Malay Peninsula Standard Time)";
$datetime =  preg_replace('/( \(.*)$/','',$datetime);
$timestamp = strtotime($datetime);
$date = date('Y-m-d H:i:s \G\M\TP', $timestamp);

I'm GMT+2 so I receive 2015-04-10 06:00:00 GMT+02:00. Further formatting depents on your desired date() formatting.

If you want to keep original time, heres the trick:

$datetime = "Fri Apr 10 2015 12:00:00 GMT+0800 (Malay Peninsula Standard Time)";
$datetime =  preg_replace('/( \(.*)$/','',$datetime);

$date = new DateTime($datetime);
$zone = $date->getTimezone();

echo $date->format('Y-m-d H:i:s') . ' GMT' .  $zone->getName();

gives 2015-04-10 12:00:00 GMT+08:00.

Upvotes: 3

Related Questions