Tentative delivery date calculation in PHP

Standard

Here is a simple function you can use if you need your PHP application to display a tentative delivery/completion date to your customer. It excludes the weekends in calculation so only business days are considered. For example, if your e-commerce site ships a product in three business days and customer orders on Thursday then this function will return tentative delivery day as Tuesday, not Sunday.

Explanation:

In our case, the delivery is made on 3rd business day to customer and we had to show the tentative delivery day in the order email to customer. If order is made either on Monday or Tuesday, it will be delivered within the same week. However if it is made on any day Wednesday through Sunday then we need to add two additional days (Saturday and Sunday) in tentative delivery day. Further, the delivery date might even fall in 3rd week and in that case two weekends need to be taken care of.

The function checks if the total of current day of week and number of days it take for shipping is less than 5, e.g. shipping can be made in same week. If it takes 6 to 14 days then it adds two extra days in tentative delivery date to accommodate weekend. ┬áNew delivery date is further checked to ensure it doesn’t fall in second weekend and adjusted accordingly.

There are some repeatable parts which can be written as a separate function however for clarity, it’s shown as a single function.

Limitations

This was written for a 3 business day delivery hence it’s not generic solution and has some limitations mentioned below.

  • This function should work fine up to a maximum 7 days delivery period but won’t work beyond that.
  • If the weekend is on any day(s) other than Saturday and Sunday, as in few Asian countries where Friday is the weekend, this won’t work as is.
  • This function considers Saturday and Sunday as weekend days so if you have only Sunday as weekend then this won’t work as it is. However the fix should be simple.

If you find any issues with the code or have suggestions to improve it, please share your feedback.

Happy coding!

2 thoughts on “Tentative delivery date calculation in PHP

Leave a Reply