标签: carbon

  • 获取已知年月第一天和最后一天的日期【PHP/Carbon】

    获取已知年月的第一天和最后一天的日期

    get the first day date and last day date of the month

    第一天是 1 不变,最后一天可能是 28,29,30,31

    PHP编程实现:

    1、使用 Composer 安装 Carbon

    composer require nesbot/carbon

    2、编写代码:

    <?php
    
    require 'vendor/autoload.php';
    
    use Carbon\Carbon;
    
    $year = 2023;
    $month = 2;
    
    // Get the first day of the month.
    $firstDayOfMonth = Carbon::create($year, $month, 1)->startOfMonth();
    
    echo 'First day of the month: ' . $firstDayOfMonth->format('Y-m-d') . PHP_EOL;
    
    // Get the last day of the month.
    $lastDayOfMonth = Carbon::create($year, $month, 1)->endOfMonth();
    
    // Print the first day and last day of the month.
    echo 'Last day of the month: ' . $lastDayOfMonth->format('Y-m-d') . PHP_EOL;
    
    ?>
    

    代码由 Google Bard 生成,

    提示词 (prompt words) :

    known year and month,get the first day date and last day date of the month in PHP use carbon package

    3、输出结果

    First day of the month: 2023-02-01
    Last day of the month: 2023-02-28