laravel雄辩地从字符串获取日期格式

kmynzznz  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(320)

在我的表中,强制列为string(例如:“20170502”)。
我想从这个字符串中得到一天中的一周,所以我尝试转换为日期格式,但这不起作用。

$event = VEventMp::where('event_seq', '=', $mypage_data->event_seq)
    ->select(
        'ENFORCE_PLACE',
        'ENFORCE_FROM',
        \DB::raw("DATE_FORMAT(STR_TO_DATE(ENFORCE_FROM,'%y/%m/%d'), '%m月%d日') 
         AS day"),
        \DB::raw('(CASE WHEN WEEKDAY(day) = 0 THEN "月"
        WHEN WEEKDAY(start_time) = 1 THEN "火"
        WHEN WEEKDAY(start_time) = 2 THEN "水"
        WHEN WEEKDAY(start_time) = 3 THEN "木"
        WHEN WEEKDAY(start_time) = 4 THEN "金"
        WHEN WEEKDAY(start_time) = 5 THEN "土"
        WHEN WEEKDAY(start_time) = 6 THEN "日" END) AS weekday')
    ->first();

我哪里出错了?
谢谢您!

ovfsdjhp

ovfsdjhp1#

首先,如果日期格式 20170502 那么你必须在打电话给 STR_TO_DATE ,将其更改为:

STR_TO_DATE(ENFORCE_FROM,'%Y%m%d')

其次,不能在 SELECT 查询的一部分,因此您需要更改 CASE 表达式到

(CASE WHEN WEEKDAY(STR_TO_DATE(ENFORCE_FROM,'%Y%m%d')) = 0 THEN "月"
      WHEN WEEKDAY(STR_TO_DATE(ENFORCE_FROM,'%Y%m%d')) = 1 THEN "火"
      WHEN WEEKDAY(STR_TO_DATE(ENFORCE_FROM,'%Y%m%d')) = 2 THEN "水"
      WHEN WEEKDAY(STR_TO_DATE(ENFORCE_FROM,'%Y%m%d')) = 3 THEN "木"
      WHEN WEEKDAY(STR_TO_DATE(ENFORCE_FROM,'%Y%m%d')) = 4 THEN "金"
      WHEN WEEKDAY(STR_TO_DATE(ENFORCE_FROM,'%Y%m%d')) = 5 THEN "土"
      WHEN WEEKDAY(STR_TO_DATE(ENFORCE_FROM,'%Y%m%d')) = 6 THEN "日" END) AS weekday

相关问题