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

devonian's avatar

Incorrect Date Value

Hi all, bit stumped on small problem, I'm trying to disable dates over the Christmas period. For some reason the last date in December is returning the incorrect month. Please see my code below:

disabledDates = [
    new Date(2018,3,2),
    new Date(2018,2,30),
    new Date(2018,4,28),
    new Date(2018,7,27),
  ];

  @php
  $arrival = strtotime('-1 day', strtotime(env('CHRISTMAS_ARRIVAL_DATE')));
  $return = strtotime('+1 day', strtotime(env('CHRISTMAS_RETURN_DATE')));
  @endphp

  christmasDates = [
  @while($arrival <= $return)
    @php
    $month = date('n', $arrival) == 1 ? 0 : date('n', strtotime('-1 month', $arrival));
    @endphp
    new Date({{ date('Y', $arrival) }},{{ $month }},{{ date('d', $arrival) }}),
    @php
    $arrival = strtotime('+1 day', $arrival);
    @endphp
  @endwhile
  ];

  $.merge(disabledDates, christmasDates);

Output

//Dates disabled...
  disabledDates = [
    new Date(2018,3,2),
    new Date(2018,2,30),
    new Date(2018,4,28),
    new Date(2018,7,27),
  ];

  
  christmasDates = [
          new Date(2018,11,17),
              new Date(2018,11,18),
              new Date(2018,11,19),
              new Date(2018,11,20),
              new Date(2018,11,21),
              new Date(2018,11,22),
              new Date(2018,11,23),
              new Date(2018,11,24),
              new Date(2018,11,25),
              new Date(2018,11,26),
              new Date(2018,11,27),
              new Date(2018,11,28),
              new Date(2018,11,29),
              new Date(2018,11,30),
              new Date(2018,12,31),
              new Date(2019,0,01),
              new Date(2019,0,02),
              new Date(2019,0,03),
              new Date(2019,0,04),
              new Date(2019,0,05),
              new Date(2019,0,06),
              new Date(2019,0,07),
              new Date(2019,0,08),
              new Date(2019,0,09),
              new Date(2019,0,10),
        ];

  $.merge(disabledDates, christmasDates);

I'm sure it's a simple fix, just struggling to get my mind around it, having a bit of a brain fart! Thanks folks.

0 likes
13 replies
devonian's avatar

Sorry to ask again, just wanted to give this a nudge, please can someone help me out here, been pulling my hair out all afternoon! :(

realrandyallen's avatar

I think what's happening is during your loop one of your dates is being calculated as:

new Date(2018, 11, 31);

Because there aren't 31 days in November javascript is switching the month to December which has 31 days, you can see this happening:

const test = new Date(2018, 11, 31);
console.log(test);

// Mon Dec 31 2018 00:00:00 GMT-0500 (Eastern Standard Time)
2 likes
devonian's avatar

@REALRANDYALLEN - Hey realrandyallen,

Thanks so much for coming back to me - much appreciated! Yeah I agree, I think this is what's happening, any suggestions on how I can fix this?

Thank you again!

devonian's avatar

Sorry, further my previous post, November dates are unaffected. It's the 31st December which isn't being blocked out...

realrandyallen's avatar

Check if the date is valid before adding it:

christmasDates = [
    @while($arrival <= $return)
        @if (date('n', $arrival) == 1)
            continue
        @endif
        
        @php
            $date = date('Y-m-d', strtotime('-1 month', $arrival));
            $check = DateTime::createFromFormat('Y-m-d', $date);
        
            if ($check && $check->format('Y-m-d') === $date) {
                print "new Date(" . date('Y', $arrival) . ", " . date('n', strtotime($date)) . ", " . date('d', $arrival) . "),";
            }

            $arrival = strtotime('+1 day', $arrival);
        @endphp
    @endwhile
];

Not the cleanest solution but it should work, I can't test it though

devonian's avatar

@REALRANDYALLEN - Thanks for coming back to me, unfortunately I'm getting the following error:

syntax error, unexpected 'if' (T_IF)
devonian's avatar

Can anyone else help? This is a minor issue, any help would be much appreciated...

realrandyallen's avatar

@DEVONIAN - What are your arrival and return dates in your env file? I’ll try to test this either tonight or in the morning

Cronix's avatar

Not to do with the issue, but please see this big warning about using env() outside of config files: https://laravel.com/docs/5.7/configuration#configuration-caching

You won't be able to cache the config on your production box, which makes the app run faster. You should only use env() in a config file, and use the config() helper to get the values instead of env().

Cronix's avatar

@drfraker Except he's using js date objects, not php.

However, there are packages for momentjs that will easily create a date range like you can with carbon. Then you'd just need

const range = moment.range(new Date(2018, 11, 17), new Date(2012, 12, 10));

https://github.com/rotaready/moment-range

Please or to participate in this conversation.