简体   繁体   中英

Mysql convert varchar to date 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM