-5

I am new to PHP so I have an issue with how to check given date is available next month or not

$startEventDate = "2021-03-31";
  • Does this answer your question? [Correctly determine if date string is a valid date in that format](https://stackoverflow.com/questions/19271381/correctly-determine-if-date-string-is-a-valid-date-in-that-format) – siddiq Mar 12 '21 at 06:25
  • @AlexRobbio available in next month? in terms of? – Kevin Mar 12 '21 at 06:37
  • Your question is unclear but if you have the date cant you check the month and using that information see if the date is in range? ie if you know the month is 04 then you can check that the day is between 1 and 30 inclusive – william_ Mar 12 '21 at 06:46
  • @AlexRobbio if next month is 04 then in this month (march) 31st date does exist – william_ Mar 12 '21 at 08:35

2 Answers2

0

I don't understand your question completely, but in my guess, you are looking for this

<?php  
//Get the next month and year like this
$startEventDate = date("2021-03-01");
$newDate = strtotime ( '+1 month' , strtotime ( $startEventDate ) ) ;


$nextMonthYear = date('Y', $newDate);
$nextMonth = date('m', $newDate);

$nextmonthdate = date($nextMonthYear."-".$nextMonth."-31"); // append your date to the next month and year

Now you can test if the $nextmonthdate is valid. There is a lot of ways to do that.

siddiq
  • 1,693
  • 3
  • 17
  • 44
  • is it still working when you give date like this `$startEventDate = date("2021-03-31");` – Alex Robbio Mar 12 '21 at 06:58
  • When I echo this variable `$nextmonthdate` then output is `1970-01-31` – Alex Robbio Mar 12 '21 at 07:03
  • I have updated the code. Check with the updated code. Also, it is better to use the first day of the month when you want to find the next month and year of the current month. – siddiq Mar 12 '21 at 07:16
0

If you want to check if the selected date falls between a start and end date, you can do something like: (PHP 5 >= 5.2.0, PHP 7, PHP 8)

    $startEventDate = new DateTime('2021-03-31');
    $start = new DateTime();
    $end = new DateTime();
    $end->modify('+1 month'); // 1 month from today


    if ($startEventDate > $start && $startEventDate <= $end) {
        $result = 'avaialble';
    } else {
        $result = 'not avaialble';
    }

    echo sprintf(
        'The selected date %s is %s between %s and %s',
        $startEventDate->format('Y-m-d'),
        $result,
        $start->format('Y-m-d'),
        $end->format('Y-m-d')
    );

More specific to your question:

check given date is available in next month or not.

You can modify the code like

    $startEventDate = new DateTime('2021-03-31');
    $start = new DateTime('first day of next month');
    $end = new DateTime('last day of next month');
    ....

ash__939
  • 1,614
  • 2
  • 12
  • 21