How to create an array with months

Asked

Viewed 1,666 times

1

Good afternoon, I am providing maintenance on a code that features a graph, the X axis presents the last 11 months and the current month, who wrote it entered the months manually and wanted to automate, I thought to do it as follows:

$meses = array(date(m)-11, date(m)-10, date(m)-9, date(m)-8, date(m)-7, date(m)-6, date(m)-5, date(m)-4, date(m)-3, date(m)-2, date(m)-1, date(m));

the problem is that doing so the months will be shown in number form and I need it to appear in date(M) format, but the function does not accept subtraction 'date(M)-1';

  • This graph is being generated as ?

  • highcharts.js and jquery.js with / month values added manually.

  • 1

    Put the Chart code

  • it is all compacted in a single line and unreadable :(

  • 1

    But in some file has the code that mounts the graph.

  • here: $('#container'). highcharts({ Chart: { zoomType: 'xy' }, title: { text: 'Objective chart <?php echo date(’d/m/Y'); ? >' }, Xaxis: [{ Categories: ['Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], crosshair: true }], yAxis: [{ // Primary yAxis }, { // Secondary yAxis Opposite: true }], Legend: { layout: 'vertical', align: 'left', x: 100, verticalAlign: 'top', y: 100, floating: true, backgroundColor: (Highcharts.Theme && Highcharts.theme.legendBackgroundColor) || '#FFFF' },

  • continuation series: [{ name: 'Completed, type: 'column', data: [0, 0, 0, 1, 2, 2, 3, 0], }, { name: 'Goal', type: 'Spline', date: [1, 4, 4, 5, 8, 8, 11, 15, 15], }]

Show 2 more comments

2 answers

0

$meses = array(1 => "Janeiro", 2 => "Fevereiro", 3 => "Março", 4 => "Abril", 5 => "Maio", 6 => "Junho", 7 => "Julho", 8 => "Agosto", 9 => "Setembro", 10 => "Outubro", 11 => "Novembro", 12 => "Dezembro");

afterward:

 echo $meses[date("n")];

0


It is enough that after calculated the month, convert it into text:

function convert_date($before){
     return DateTime::createFromFormat('!m', (date('m')-$before))->format('F');
}

Then just use it that way:

$meses = array(
     convert_date(11),
     convert_date(10),
     convert_date(9),
     convert_date(8),
     convert_date(7),
     convert_date(6),
     convert_date(5),
     convert_date(4),
     convert_date(3),
     convert_date(2),
     convert_date(1)
 );
  • this function convert_date is giving error in my code, qnd I call it gives runtime error :(

  • PHP installed on my machine which is an old and incompatible version, 5.2, I circled in a php sandbox online and it worked the way I need, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.