duanji1482 2014-10-09 18:57
浏览 108
已采纳

Mysql将varchar转换为日期php

I have a field in my database that stores a date as a VARCHAR that looks like this:

      2014-10-09T07:10:46

I'm trying to convert that into a date and display it as %Y-%M by doing the following;

      'Month' => array(
        'chart' => 'line',
        'fields' => array(
        array(
         'field' => 'date_format(str_to_date(cd.field_closed_date_value,\'%Y-%M-%dT%H:%i:%S\'), \'%Y-%m\')',
         'alias' => 'month',
         'label' => 'Month',
         'type' => 'string',
         'sort' => 'ASC',
        ),
       ),
      ),

The original code that worked was like this:

'Day' => array(
'chart' => 'line',
'fields' => array(
  array(
    'field' => 'date_format(from_unixtime(n.created), \'%Y-%m-%d\')',
    'alias' => 'day',
    'label' => 'Day',
    'type' => 'string',
    'sort' => 'ASC',
  ),
),
),

But that code uses the created date and I'm needing to use the closed date, which isn't in the same format. I know I've probably got this really wrong, I'm trying to work with what was here when I took on this project, any help is appreciated.

  • 写回答

1条回答 默认 最新

  • douping7105 2014-10-09 19:28
    关注

    Just use the date() and the strtotime() function like this:

    date("Y-M",strtotime('2014-10-09T07:10:46')) // returns "2014-Oct"
    

    strtotime() accepts ISO date format and returns a UNIX timestamp, date() can then convert the timestamp into a custom format.

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

报告相同问题?

悬赏问题

  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)