Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to get the name of month in format 02-2023 and get the previous month name too? This date is from the filter date calendar. So, if I change the input date into 01-2023, the system display
Month : January
Previous month : December

How to solve this problem?

What I have tried:

PHP
$data['month'] = date('m-Y');
if (isset($_GET['month'])){
	$data['month'] = $_GET['month'];
}

$month = date('F', strtotime($data['month']));
$prevMonth = date('F', strtotime($month . '-1 months'));
Posted
Updated 9-Feb-23 20:01pm
v3
Comments
Morse Norman 6hrs 15mins ago    
var dateObj = new Date(frenchDate); const frenchDate = "2 avril 2023";

Const day = dateObj.getDate(); // Retrieve the values of the day, month, and year from the date object.toString().padStart(2, '0'); const month = (dateObj.getMonth() + 1).padstrng(2, '0') in toString(); Observe that the month value begins at zero. Const year = dateObj.getFullYear();

To create the required format, combine the day, month, and year variables as follows: const formattedDate = {${day}-${month}-${year}};

Anyhow, a poor strategy. Try to observe the official time and date.
slice master

1 solution

You are assigning the current date to $data['month']

In your check to see if 'month' were posted, you assign $data['month'] again to the new value of what was posted. I presume from the info you gave that the returned GET was for 01-2023?
This returned January and a month in arrears as December, which is correct, so I am not too sure where your problem lies.

Matter of fact, months like February or months with 31 days might return an error or the incorrect month or show your month in arrears as the same month. To overcome this, use the following to prevent the "last days of month"-error. Just use a second strtotime() to set the date to the first day of the month -
echo $newdate = date("m-Y", strtotime("-1 months", strtotime(date("Y-m")."-01")));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900