Click here to Skip to main content
16,007,111 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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
[no name] 28-May-24 23:36pm    
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
JimmyJane 13hrs ago    
To get the name of the month in the format "02-2023" and the previous month's name, you can use the following PHP code:

$data['month'] = date('m-Y'); // Get the current month

if (isset($_GET['month'])) {
$data['month'] = $_GET['month']; // If a month is provided via GET, use that month
}

// Get the current month's name
$month = date('F', strtotime($data['month']));

// Get the previous month's name
$prevMonth = date('F', strtotime($data['month'] . ' -1 month'));

echo "Month: " . $month . "
";
echo "Previous month: " . $prevMonth;

Explanation:
Get the current month: Use date('m-Y') to retrieve the current month.
Check for month from GET: If a month is provided via the GET method, it will replace the current month.
Get the month's name: Use date('F', strtotime(...)) to convert the month format into the month's name.
Get the previous month's name: Use strtotime($data['month'] . ' -1 month') to calculate the previous month.

You can try this code and adjust rice purity test io according to your needs!
[no name] 28-Jun-24 4:34am    
To begin with, you must download the spreadsheet containing the stock price and return data from Canvas's Assignments final grade calculator page.
[no name] 8-Jul-24 0:48am    
To achieve this in a programming context, you can use tiny fishing various methods depending on the programming language you're using.
Member 16364738 11-Sep-24 1:02am    
How can I get the name of the month and previous month from a date entered in the format 'DD-MM-YYYY'? I need to display the name of the current month and previous month, for example if I enter '15-02-2023', papa's scooperia should display 'Month: February' and 'Previous month: January

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