I want to get the end of the month date, but it doesn't result in the intended result.

Asked 2 years ago, Updated 2 years ago, 41 views

When I tried to get the beginning and end of the month dates from the request parameters year=2022, month=6, the results were as follows:

What should I do if I want to get the correct month-end date?

Current State Code:

$start_date=date($year. "-".$month. "-01");
$end_date=date($year. "-".$month. "-t");

log::debug("Check End Date");
log::debug($end_date);

execution results:

 [2022-05-23 21:51:25] local.DEBUG: 2022-6-31  

php laravel

2022-09-30 14:52

1 Answers

$year. "-".$month. "-t" is 2022-6-t, so it's natural.

The first argument for date is the format, the second argument is the Unix timestamp, and the default for not giving the second argument is the Unix timestamp for the current time.Now it's May 2022, so formatting it with t means 31, and the return value for date is 2022-6-31.

The second argument of date must be a Unix timestamp of $year/month.Therefore, the following is true:

<?php
$year=2022;
$month = 6;
$start_date=date($year. "-".$month. "-01");
$end_date=date("Y-n-t", mktime(0,0,0,$month,1,$year"));
echo$end_date;


2022-09-30 14:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.