I'm trying to get estimated delivery dates for various products but rather than working it out for each product it adds the previous delivery time onto the next product before working it out.

This is what I've got so far

public function getDeliveryDate() {*
* * * *$ndd = $this->getPhysicalOrderProducts();*
* * * * $arrive = $this->getPostDate();*
* * * * $arrive->add(new DateInterval('P'.$ndd[0]->getDeliveryTime().'D'));*
* * * * return $arrive;*
* * * }

*
I want it get a product, add the DeliveryTime to PostDate then return that as $date I then want it to go on to the next product and do the same thing. At the moment what's happening is it's getting the date, adding the delivery time. Then with the next date it's adding the previous delivery time to the result of $date from the previous product.
*
Is there anyway to get it to recalculate it from fresh for every product?

    That's because when you add the DateInterval you're adding it to the object returned by [font=monospace]$this->getPostDate()[/font]. If you're using the same object every time (i.e, [font=monospace]getPostDate()[/font] isn't creating a new one every time) then all those date intervals will be getting added to the same object.

    If that is what you're doing, then either use a [man]DateTimeImmutable[/man] object, or assign a [man]clone[/man] of the DateTime to [font=monospace]$arrive[/font]

      Thank you for your help sorry to ask just a dumb question but how do I do that? I've very new to PHP so am a little unsure of how to do that.

        "That" referring to.... which of the things I linked to?

          Weedpacket;11042305 wrote:

          "That" referring to.... which of the things I linked to?

          Sorry, I changed the code to this:

          public function getDeliveryDate() { 
                 $ndd = $this->getPhysicalOrderProducts(); 
                  $arrive = clone $this->getPostDate(); 
                  $arrive->add(new DateInterval('P'.$ndd[0]->getDeliveryTime().'D')); 
                  return $arrive; 
                }
          

          I added clone but it then sets the getDeliveryDate() to the same as the first date. So, for example, if I add 3 days to first product and 1 to the next it should add 3 days to the original date for the first product and then 1 day to the original date for the second product. But when I tested it it added 3 days to the original date for both products.

            Write a Reply...