0

I would like to have array in numbering for days and months from starting day 1 or month 1 till todate.

For example: Today is 05-Oct-2012

Array for days: Output (1,2,3,4,5)

Array for months: Output (1,2,3,4,5,6,7,8,9,10)

Those array will be use in axis-x for charts.

Alouty
  • 15
  • 1
  • 1
  • 8
  • SOF for more than 2 pages. google too but most are counted days in mths but not output till todate. – Alouty Oct 05 '12 at 10:19

3 Answers3

1

try this ,

PHP

<?php
$date= date('Y-m-d');
$dayarray=range(1,date('d',strtotime($date)));
$monthharray=range(1,date('m',strtotime($date)));

JAVASCRIPT

var now= new Date();

var daysArray = [];
for (var i=1; i<=now.getDate(); i++)
daysArray.push(i);

var monthsArray = [];
for (var i=1; i<=now.getMonth()+1; i++)
monthsArray.push(i);
bipen
  • 36,319
  • 9
  • 49
  • 62
  • How to define it if inside template file for smarty php? Inside {php}{/php}? I put the js code but it output text 'array'... – Alouty Oct 08 '12 at 01:18
1

You can use the Date object together with this useful Array.range() implementation to get the arrays;

var date = new Date("05-Oct-2012");
var days = Array.range(1, date.getDate());
console.log(days);
var months = Array.range(1, date.getMonth() + 1);  // Zero-based, so add one.
console.log(months);

Full jsfiddle here.

Community
  • 1
  • 1
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
0

Use the methods of the Date objects:

var today = new Date();

var days = [];
for (var i=1; i<=today.getDate(); i++)
    days.push(i);

var months = [];
for (var i=1; i<=today.getMonth()+1; i++)
    months.push(i);
Bergi
  • 630,263
  • 148
  • 957
  • 1,375