doulupian8725 2014-03-19 14:12
浏览 36
已采纳

从用户输入转换时的PHP日期格式(代码点火器)

I am taking a value entered by a user in a jQuery UI datepicker and converting it to a to a php date for insertiion in a database like so:

$date_from  = post('date_valid_from');    
$date_from  = date('Y-m-d', strtotime($date_from));

This works fine if the input is formatted in US format (mm/dd/yyyy), however as my system will be used by people in the UK it needs to be input in the UK format (dd/mm/yyyy). When converting the UK dates to a php date, the month and day are switched around (Feb 1 2014 - 01/02/2014 - becomes January 2 2014 and Feb 28 2014 - 28/02/2014 - becomes January 1 1970).

Is there anyway I can overwrite the default date format to work with UK format?

(This project is using Code Igniter if there is a CI config setting I can set)

  • 写回答

2条回答 默认 最新

  • dougua3705 2014-03-19 14:14
    关注

    Use DateTime() instead. It's more flexible than date() and strtotime():

    $date_from = DateTime::createFromFormat('d/m/Y', $date_from);
    $date_from = $date_from->format('Y-m-d');
    

    If you want to support both formats (mm/dd/yyyy and dd/mm/yyyy) just swap out the first parameter of DateTime::createFromFormat(). Using a variable would make that easy to do.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入