jeudi 29 novembre 2018

PHP/Laravel 5.4/Carbon - sustainable way to parse week of year, considering for year to year week number variances

Any thoughts on how to parse week of year and consider for abnormalities like this year's 12/31/18 actually being week 1 of 2019. The reason is someone wrote a scheduler that stores dates like so: 2017-W37-1 (year, the letter W, actual week number and actual day number of the week). The way it was written, it has already stored over three hundred rows as 2018-W53, which does not even exist this year. I'm working with PHP/Laravel 5.4 and I've started a weekOfYear method to filter all incoming dates from the front end. Flow is front end has a date picker, sends a date to the index, store and destroy methods and interacts with the schedules table from there, which has a "date" column in the above format (actually a string obvs and not date). There is no fallback date, so it just pulls all the schedules for a week based on that one string. My method so far:

public function weekOfYear($date)
{
    $date = Carbon::parse($date);
    $formattedDate = $date->year.'-W'.$date->weekOfYear;

    if ($date->format('Y-m-d') === '2018-12-31') {
        $formattedDate = '2019-W01';
    }
    return $formattedDate;
}

The day number gets tagged on in further methods, but I'll probably add that here as well. This works for 2018, but I would rather parse the date in a more flexible way, despite the fact that 2019 is actually more straightforward.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2P7JOu7
via IFTTT

Aucun commentaire:

Enregistrer un commentaire