Calendar Logic with Laravel and php-Carbon – 日期解析器

huangapple go评论56阅读模式
英文:

Calendar Logic with Laravel and php-Carbon - Date parser

问题

I understand that you want a translation of the provided code, excluding the code itself. Here is the translated description of the code's purpose:

我正在尝试创建一个用于公司日程的日期解析器。我想将上个月的当前周和日复制到上个月的相同周和日。在日期在同一个月的情况下,我想将上个月复制到相同的周和日。我一直遇到问题,因为每个月的周结构都不同。而且,处理31号会返回错误的月份。因此,我尝试为此创建逻辑。但是在转换日期时,我不断发现新的问题。希望有人可以帮助我找到正确的解决方法。
最初,我尝试创建一个包含每个月的数组,并将其用作表格,但也遇到了问题。下面是我大部分已经运行但仍然存在问题的代码。希望这有意义。

更新:
仍然在努力理清这个逻辑。不确定关系是什么。尝试了下面的代码,但是一月、十月和十一月都返回错误的日期。此外,三月、四月、六月、七月、九月和十二月在29、30、31号时返回错误的日期。我确定我现在想得太多了,但却没有成功。

请注意,这只是代码的描述,没有包括实际代码部分。如果您需要对代码进行翻译或有其他问题,请告诉我。

英文:

I am trying to create a date parser for a company schedule. I want to copy the previous month current week, day to previous month same week day. In instances where the date is in the same month I want to copy the previous month to that same week and day. I keep running into issues because weeks structures are different month to month. Also, working with the 31st returns the wrong month. So I tried creating logic for that. However I keep finding new problems when converting the date. Hoping someone can help me get on the correct path.
Initially I tried creating an array of each month and using it as a table but ran into issues with that as well. Below is what I have mostly working but still has issues with several dates returning the wrong week. Hopefully this makes sense.

Calendar Logic with Laravel and php-Carbon – 日期解析器

 public function parser($day = "2 feb 2023") {

    $date = Carbon::parse($day);
    $dayOfWeek = $date->copy()->dayOfWeek;
    $numberOfWeek = $date->copy()->weekNumberInMonth;
    $newDate = $date->copy()->subMonth()->startOfWeek(Carbon::SUNDAY)->next($dayOfWeek);
    $lastOfMonthNewDate = $date->copy()->subDay(1)->subMonth(1)->startOfWeek()->next($dayOfWeek);


    if (($date->day == 31) && ($lastOfMonthNewDate == $date->isSameMonth($lastOfMonthNewDate))) {

        return $lastOfMonthNewDate->copy()->subMonth(1)->startOfWeek()->next($dayOfWeek);

    } else if ($date->day == 31) {

        return $lastOfMonthNewDate;

    } else  if ($newDate == $date->isSameMonth($newDate)) {

        return $newDate->copy()->subMonth(1)->startOfWeek()->next($dayOfWeek);

    } else {
            
            return $newDate;
    }
}

Update:
Still struggling with the logic on this. Not sure what the relation is. Tried the below code but, Jan,Oct,Nov all return the wrong dates. Also, Mar,April,Jun,Jul,Sep,Dec return wrong dates for 29,30,31. I am sure I am over thinking it now but no go.

    public function parser2($day){
    $date = Carbon::parse($day);
    $dayOfWeek = $date->copy()->dayOfWeek;
    $date4Week = $date->copy()->subWeek(4);
    $date5Week = $date->copy()->subWeek(5);
    $currentWeek = $date->copy()->weekNumberInMonth;
    $prevWeek = $date->copy()->subWeek(4)->weekNumberInMonth;


    if ($date->isSameMonth($date4Week) == 1) {

        if ($currentWeek != $date5Week->copy()->weekNumberInMonth) {
           return $date->subMonth();
        }else{
            return $date4Week->copy()->subWeek();
        }
    }else {

        if ($prevWeek != $currentWeek) {
            return $date5Week;
        }else{
            return $date4Week;
        } 
    }
}

答案1

得分: 1

All you should need to do is subtract 4 weeks. If you're still in the same month, subtract one more. You can extend the Carbon class to do this by creating a macro in a service provider class.

Run artisan make:provider CarbonMacroProvider to generate a blank provider class.

Add the new provider class to the providers array in config/app.php:

return [
    ...
    'providers' => [
        /*
         * Application Service Providers...
         */
        App\Providers\CarbonMacroProvider::class
        ...
     ],
    ...
];

Edit app/Providers/CarbonMacroProvider.php to look like this:

<?php

namespace App\Providers;

use Carbon\Carbon;
use Illuminate\Support\ServiceProvider;

class CarbonMacroProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        Carbon::macro("previousMonth", static function (): Carbon {
            /** @var Carbon $date **/
            $date = self::this();
            $last = $date->copy()->subWeeks(4);

            return $last->month === $date->month ? $last->subWeek() : $last;
        });
    }
}

Then in your app, you can do this:

echo Carbon::parse("20 july")->previousMonth()->format("Y-m-d H:i:s");
echo Carbon::parse("31 october")->previousMonth()->format("Y-m-d H:i:s");
echo Carbon::parse("2 november")->previousMonth()->format("Y-m-d H:i:s");

Output:

2023-06-22 00:00:00
2023-09-26 00:00:00
2023-10-05 00:00:00
英文:

All you should need to do is subtract 4 weeks. If you're still in the same month, subtract one more. You can extend the Carbon class to do this by creating a macro in a service provider class.

Run artisan make:provider CarbonMacroProvider to generate a blank provider class.

Add the new provider class to the providers array in config/app.php.

return [
    ...
    &#39;providers&#39; =&gt; [
        /*
         * Application Service Providers...
         */
        App\Providers\CarbonMacroProvider::class
        ...
     ],
    ...
];

Edit app/Providers/CarbonMacroProvider.php to look like this:

&lt;?php

namespace App\Providers;

use Carbon\Carbon;
use Illuminate\Support\ServiceProvider;

class CarbonMacroProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        Carbon::macro(&quot;previousMonth&quot;, static function (): Carbon {
            /** @var Carbon $date **/
            $date = self::this();
            $last = $date-&gt;copy()-&gt;subWeeks(4);

            return $last-&gt;month === $date-&gt;month ? $last-&gt;subWeek() : $last;
        });
    }
}

Then in your app you can do this:

echo Carbon::parse(&quot;20 july&quot;)-&gt;previousMonth()-&gt;format(&quot;Y-m-d H:i:s&quot;);
echo Carbon::parse(&quot;31 october&quot;)-&gt;previousMonth()-&gt;format(&quot;Y-m-d H:i:s&quot;);
echo Carbon::parse(&quot;2 november&quot;)-&gt;previousMonth()-&gt;format(&quot;Y-m-d H:i:s&quot;);

Output:

2023-06-22 00:00:00
2023-09-26 00:00:00
2023-10-05 00:00:00

huangapple
  • 本文由 发表于 2023年6月12日 10:02:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453258.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定