-1

I'm having some trouble figuring out how to get an array item with a $variable - 1.

What I am trying to do:

I have an array of days in a month

$monthsArray = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

I have a variable which is the month

$monthOfDeparture = mb_substr($departureDate, 3, 4);

which has the value of 01 for January etc.

I am trying to get the correct month by

$theMonth = $monthsArray[$monthOfDeparture - 1]

What I actually get is the month of departure ie: March (31) instead of February (28) if $monthOfDeparture = 03.

Any help would be welcome.

Providing code: note the $arrivalDate and $departureDate look like this 23-02-21 (dd-mm-yy)

$arrivalDateNumber = mb_substr($arrivalDate, 0, 1);
$departureDateNumber = mb_substr($departureDate, 0, 1);
                    
$monthOfArrival = mb_substr($arrivalDate, 3, 4);
$monthOfDeparture = mb_substr($departureDate, 3, 4);
                    
$monthsArray = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
                    
if( $departureDateNumber < $arrivalDateNumber) {
    $depNumTwo = $monthsArray[$monthOfDeparture - 1];
    $lodgingNights = $depNumTwo - $arrivalDateNumber + $departureDateNumber; 
    $pay = $lodgingNights * $price_per_night;
}else{
    $lodgingNights = $departureDateNumber - $arrivalDateNumber;
    $pay = $lodgingNights * $price_per_night;
}

Edit

There was an error my logic. What I wanted to do was:


// if the number of days in departure is greater then the number of days in arrival 
if( $departureDateNumber < $arrivalDateNumber) {
    //get the number of days in the month of arrival
    $arrNumTwo = $monthsArray[$monthOfArrival];
    //the user will stay: total number of days in arrival month - starting date of arrival + days in the next month
    $lodgingNights = $arrNumTwo - $arrivalDateNumber + $departureDateNumber; 
    $pay = $lodgingNights * $price_per_night;
}else{
    $lodgingNights = $departureDateNumber - $arrivalDateNumber;
    $pay = $lodgingNights * $price_per_night;
}

The problem I am having now is that I get Notice: Undefined index: 02-2 in my path on line $arrNumTwo = $monthsArray[$monthOfArrival]; I guess that is becouse of this code segment $monthOfArrival = mb_substr($arrivalDate, 3, 4); although I don't know why it dosesn't just take the 3rd and 4th character.

Edit nmb xy. Figured out I misnderstood how substr works. Needed to use $monthOfArrival = mb_substr($arrivalDate, 3, 2);

WeAreDoomed
  • 248
  • 1
  • 14
  • Please share more details, like sample code that helps to reproduce your problem – Nico Haase Feb 17 '21 at 11:49
  • `'03' - 1` is `2`, and the value at index `2` in your array _is_ `31`. You simply want minus two, not minus one. – CBroe Feb 17 '21 at 11:50
  • So "correct month" would be "previous month"? – El_Vanja Feb 17 '21 at 12:04
  • yes, the correct month would be previous month (becouse the array starts wtih 0 instead of 1) – WeAreDoomed Feb 17 '21 at 12:13
  • 3
    _“becouse the array starts wtih 0 instead of 1”_ - only if you want it to, it is easy to make it start from a different index, by explicitly specifying which one to use for the first item. `$monthsArray = [1 => 31, 28, 31, …]` would get you the whole thing indexed starting with 1. – CBroe Feb 17 '21 at 12:18
  • I didn't know that. Thank you. I did try it out, changed `$monthsArray = [ 1 => 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];` and `$depNumTwo = $monthsArray[$monthOfDeparture];` but now I get *Notice: Undefined index: 03-2 in* mypath – WeAreDoomed Feb 17 '21 at 12:23
  • @CBroe that should be an answer not just comment. - index is a clue in this case and `1=>31` solves it easy. – biesior Feb 17 '21 at 12:24
  • Cast your month value to an integer. Though, I still don't understand how reindexing would fix this. The `-1` solution with the array starting at 0 would be the same as not modifying the value with the array starting at 1. The logic you have stated in your question seems to indicate you're not looking for the month of departure, but rather the month before. Can you clarify what your expected value would be for `03`? – El_Vanja Feb 17 '21 at 12:33
  • Now I'm even more confused. why do I get 03-1. Now that I am looking up the number in the index I get 03-1. Why is that? – WeAreDoomed Feb 17 '21 at 12:33
  • Though, observing the purpose of your code after the update, you would benefit by dropping this whole code block and rewriting everything using `DateTime`. – El_Vanja Feb 17 '21 at 12:36
  • btw you're not accounting for leap years, 29 days in feb – Lawrence Cherone Feb 17 '21 at 12:36
  • @LawrenceCherone I know. I'll get to it later =) – WeAreDoomed Feb 17 '21 at 12:39
  • Avoid all of these problems by applying the `DateTime` logic @Martijn suggested in his answer. Arrays, undefined index notices, leap days... all gone if you switch to that. – El_Vanja Feb 17 '21 at 12:44
  • What is `$arrivalDate`? is it `YYYY-MM-DD`? Why do you need multibyte substr() support? I think don't cut your input date at all, feed it to a date function or class and then get the `t` value. – mickmackusa Feb 17 '21 at 13:02
  • it's `dd-mm-yy` – WeAreDoomed Feb 17 '21 at 13:04

1 Answers1

1

While not an answer to your placed question, this might be an answer worth your while:

$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $theMonth, $year);

*You might need to do a +2000 for the $year

Docs for cal_days_in_month. This has the added bonus of working in leap years.


Alternatively you can use DateTime (which I recommend). Datetime seems a bit intimidating in the beginning, but has a lot of features which make your life a lot easier. Example:

$arrival = DateTime::createFromFormat("d-m-y", $arrivalDate);
$departure = DateTime::createFromFormat("d-m-y", $departureDate);

You can compare those with eachother (eg if( $departure < $arrival) {}) and you can get the diff a lot easier:

$diff = $arrival->diff($departure)->days;
Martijn
  • 15,791
  • 4
  • 36
  • 68