Laravel Carbon, retrieve today's date with weekday?

后端 未结 3 1004

I am using carbon to compare 2 dates with today\'s date, however I also have another field in a database called weekday which contains values like:

\'MO\' \'TU\' \'W

相关标签:
3条回答
  • 2021-01-01 14:14

    If you're in an English-speaking locale, you can get that short weekday format by doing some processing on Carbon's l format, which returns weekday names:

    strtoupper(substr($today->format('l'), 0, 2)); // Returns 'MO', 'TU', etc
    

    It can be even shorter if you have access to Carbon 2 (available on 2018-08-31):

    strtoupper($today->isoFormat('dd'));
    
    0 讨论(0)
  • 2021-01-01 14:17

    I'm not sure that Carbon has such formatting, but what you could do is get the wekkday from a map of days and the current week day constant:

    $weekMap = [
        0 => 'SU',
        1 => 'MO',
        2 => 'TU',
        3 => 'WE',
        4 => 'TH',
        5 => 'FR',
        6 => 'SA',
    ];
    $dayOfTheWeek = Carbon::now()->dayOfWeek;
    $weekday = $weekMap[$dayOfTheWeek];
    
    0 讨论(0)
  • $weekday = Carbon::parse($today)->format('1');

    0 讨论(0)
提交回复
热议问题