Reputation: 389
how to get month name from month-year (08-2022) string in php
i want to get this Aug-2022 (format) from my value 08-2022
I had tried date('m-Y',strtotime(08-2022))
and date('08-2022')->format('m-Y')
but not working.
Upvotes: 1
Views: 2051
Reputation: 389
i have done with it->
<?php
$date = "08-2022"
$exploreDate = explode('-', $date);
echo date('F', mktime(0, 0, 0, $exploreDate [0], 10)) .' '. '-' .' '. $exploreDate [1];
?>
Answer: August-2022
Upvotes: 0
Reputation: 7693
Just put a "01-" in front of your value to make it a valid date.
$value = "08-2022";
echo date('M-Y',strtotime('01-'.$value));
//Aug-2022
try self: https://3v4l.org/ZiB70
or with DateTime:
$value = "08-2022";
echo date_create_from_format('!m-Y',$value)->format('M-Y');
Important NOTE:
That ! in the format is important for this case and is unfortunately missing in many examples for date_create_from_format. If that ! missing then the current day is set as the day. If this happens on the 31st of a month and value is, for example, '06-2022' then a date '31-06-2022' is generated which is then reported as 01-07-2022. With the ! the day is set to 1 and the time to 00:00.
Upvotes: 3
Reputation: 105
you have to pass date also. so you can try like this way ...
$data = "08-2022";
$month = date('M-Y', strtotime('01-' . $data));
hope it helps ...
Upvotes: 3
Reputation: 888
In This format get month name
echo date('Y-F-d');
echo "<br>";
echo date('Y-M-d');
echo "<br>";
echo date('Y-m-d');
Upvotes: 1
Reputation: 1574
You can use F & M like this
echo date("d-M", strtotime('08-2022'));
echo date("d-F", strtotime('08-2022'));
for more details https://www.php.net/manual/en/function.date.php
Upvotes: 0