1

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.

Rajib Bin Alam
  • 353
  • 1
  • 4
  • 16
  • 6
    `DateTime::createFromFormat`: https://stackoverflow.com/a/73122287/1427345 – Jacob Mulquin Aug 10 '22 at 12:13
  • 1
    Does this answer your question? [Convert number to month name in PHP](https://stackoverflow.com/questions/18467669/convert-number-to-month-name-in-php) – Nico Haase Aug 10 '22 at 12:28

5 Answers5

3

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 ...

3

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.

jspit
  • 7,276
  • 1
  • 9
  • 17
1

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');

enter image description here

Waad Mawlood
  • 727
  • 6
  • 10
0

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

Shibon
  • 1,552
  • 2
  • 9
  • 20
0

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

Rajib Bin Alam
  • 353
  • 1
  • 4
  • 16