dtgsl60240 2015-04-10 11:22
浏览 43
已采纳

将datepicker日期时间转换为php日期时间

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 .

  • 写回答

1条回答 默认 最新

  • dow5001 2015-04-10 11:28
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失