Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FloorMonth for last day of month, returns next month if month has 31 days #2581

Closed
atay opened this issue Mar 31, 2022 · 2 comments · Fixed by #2583
Closed

FloorMonth for last day of month, returns next month if month has 31 days #2581

atay opened this issue Mar 31, 2022 · 2 comments · Fixed by #2583
Assignees
Labels
Milestone

Comments

@atay
Copy link

atay commented Mar 31, 2022

Hello,

I encountered an issue with the following code:

<?php
require 'vendor/autoload.php';

use Carbon\Carbon;

function showDate($date) {
    echo "Date:" . $date->format("Y-m-d") . ", floorMonth: " . $date->copy()->floorMonth()->format("Y-m-d") . PHP_EOL;
}

$date1 = Carbon::parse("2021-03-30");
$date2 = Carbon::parse("2021-03-31");

showDate($date1);
showDate($date2);

Carbon version: 2.57.0 and 2.55.2

PHP version: 8.0.8 and 8.1.1

I expected to get:

Date:2021-03-30, floorMonth: 2021-03-01
Date:2021-03-31, floorMonth: 2021-03-01

But I actually get:

Date:2021-03-30, floorMonth: 2021-03-01
Date:2021-03-31, floorMonth: 2021-04-01

Thanks!

@atay atay changed the title Bug - FloorMonth for 31 March returns April 1st FloorMonth ( March 31) = April 1st Mar 31, 2022
@atay atay changed the title FloorMonth ( March 31) = April 1st FloorMonth (March 31) = April 1st Mar 31, 2022
@atay
Copy link
Author

atay commented Mar 31, 2022

Same issue with months that have 31 days:

<?php
require 'vendor/autoload.php';

use Carbon\Carbon;

function showDate($date) {
    echo "Date: " . $date->format("Y-m-d") . ", floorMonth: " . $date->copy()->floorMonth()->format("Y-m-d") . PHP_EOL;
}

for ($i = 1; $i <= 12; $i++) {
    showDate(Carbon::parse("2021-$i-01")->endOfMonth());
}

Output:

Date: 2021-01-31, floorMonth: 2021-02-01
Date: 2021-02-28, floorMonth: 2021-02-01
Date: 2021-03-31, floorMonth: 2021-04-01
Date: 2021-04-30, floorMonth: 2021-04-01
Date: 2021-05-31, floorMonth: 2021-06-01
Date: 2021-06-30, floorMonth: 2021-06-01
Date: 2021-07-31, floorMonth: 2021-08-01
Date: 2021-08-31, floorMonth: 2021-09-01
Date: 2021-09-30, floorMonth: 2021-09-01
Date: 2021-10-31, floorMonth: 2021-11-01
Date: 2021-11-30, floorMonth: 2021-11-01
Date: 2021-12-31, floorMonth: 2022-01-01 

@atay atay changed the title FloorMonth (March 31) = April 1st FloorMonth for last day of month, returns next month if month has 31 days Mar 31, 2022
@kylekatarnls kylekatarnls self-assigned this Mar 31, 2022
kylekatarnls added a commit to kylekatarnls/Carbon that referenced this issue Apr 3, 2022
@kylekatarnls kylekatarnls added this to the 2.58.0 milestone Apr 3, 2022
@kylekatarnls
Copy link
Collaborator

This will be fixed in the next release, it can be tested using composer require "nesbot/carbon:dev-master as 2.58.0"

Thanks 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants