Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pordonez's avatar

Laravel get start date of month to current date of month

Hi All,

i am using chartjs, i want to put the dates of the month on the label. and show the records of those dates. for example today is 14, i want to put date from october 1 to october 14, then tomorrow it will be to october 15.

can any one help me on how to accomplish this?. I am searching for 2 days now.

Thanks,

0 likes
6 replies
D9705996's avatar

You can create your dates using a for loop

$day = new Date().getDate();  // e.g. 14

for (i = 1; i <= $day; i++) { 
    console.log(new Date().setDate($i)):
}

Obviously you need to do something other than console.log thd date bug should help.

https://www.w3schools.com/js/js_dates.asp

pordonez's avatar

@D9705996 is it possible to pass the loop result to a variable?. i need to use it on the chartjs as label.

D9705996's avatar

It looks like chartjs expects an array for it labels so you could do

let days = [];

let day = new Date().getDate();  // e.g. 14

for (let i = 1; i <= day; i++) { 
    days.push(new Date().setDate(i).toLocaleDateString()):
}

// then in your chart definition

data: {
        labels: days
...

I have added a toLocaleDateString() which should help format the date for you. Customise to your needs

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

pordonez's avatar

@D9705996 I tried this method using Laravel controller and the label is only showing the current date.

D9705996's avatar
D9705996
Best Answer
Level 51

@pordonez - the code provided is JavaScript so shouldn't be in your controller, you would put it in your view.

Can you share you code?

1 like

Please or to participate in this conversation.