× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

[PHP] Using date() and strtotime() to Set a Delivery or Shipping Date

Megan Morsie
Apr 30, 2016
<p><strong>Scenario:</strong> Let’s say your business ships products out every Monday, but the cut off date for ordering for the next delivery date is Thursday. This snippet checks the day of the week with <strong>date(‘w’)</strong>. <a href="http://php.net/manual/en/function.date.php#refsect1-function.date-parameters">(It can also be accomplished by using l, D, or N.)</a>Then, if it’s past a certain day of the week, it will set <strong>$delivery</strong> to be the following Monday.</p> <pre><span style="color:rgb(0, 0, 136)">if</span><span style="color:rgb(0, 0, 0)"> </span><span style="color:rgb(102, 102, 0)">(</span><span style="color:rgb(0, 0, 0)"> date</span><span style="color:rgb(102, 102, 0)">(</span><span style="color:rgb(0, 136, 0)">'w'</span><span style="color:rgb(102, 102, 0)">)</span><span style="color:rgb(0, 0, 0)"> </span><span style="color:rgb(102, 102, 0)">&gt;</span><span style="color:rgb(0, 0, 0)"> </span><span style="color:rgb(0, 102, 102)">4</span><span style="color:rgb(102, 102, 0)">)</span><span style="color:rgb(0, 0, 0)"> </span><span style="color:rgb(102, 102, 0)">{</span><span style="color:rgb(0, 0, 0)"> </span><span style="color:rgb(136, 0, 0)">// Note: Sunday = 0</span><span style="color:rgb(0, 0, 0)"> </span><span style="color:rgb(136, 0, 0)">// Wait til next Monday!</span><span style="color:rgb(0, 0, 0)"> $delivery </span><span style="color:rgb(102, 102, 0)">=</span><span style="color:rgb(0, 0, 0)"> date</span><span style="color:rgb(102, 102, 0)">(</span><span style="color:rgb(0, 136, 0)">'l, F jS, Y'</span><span style="color:rgb(102, 102, 0)">,</span><span style="color:rgb(0, 0, 0)"> strtotime</span><span style="color:rgb(102, 102, 0)">(</span><span style="color:rgb(0, 136, 0)">'next Monday + 1 week'</span><span style="color:rgb(102, 102, 0)">,</span><span style="color:rgb(0, 0, 0)"> time</span><span style="color:rgb(102, 102, 0)">()));</span><span style="color:rgb(0, 0, 0)"> </span><span style="color:rgb(102, 102, 0)">}</span><span style="color:rgb(0, 0, 0)"> </span><span style="color:rgb(0, 0, 136)">else</span><span style="color:rgb(0, 0, 0)"> </span><span style="color:rgb(102, 102, 0)">{</span><span style="color:rgb(0, 0, 0)"> </span><span style="color:rgb(136, 0, 0)">// They get it this coming Monday!</span><span style="color:rgb(0, 0, 0)"> $delivery </span><span style="color:rgb(102, 102, 0)">=</span><span style="color:rgb(0, 0, 0)"> date</span><span style="color:rgb(102, 102, 0)">(</span><span style="color:rgb(0, 136, 0)">'l, F jS, Y'</span><span style="color:rgb(102, 102, 0)">,</span><span style="color:rgb(0, 0, 0)"> strtotime</span><span style="color:rgb(102, 102, 0)">(</span><span style="color:rgb(0, 136, 0)">'next Monday'</span><span style="color:rgb(102, 102, 0)">,</span><span style="color:rgb(0, 0, 0)"> time</span><span style="color:rgb(102, 102, 0)">()));</span><span style="color:rgb(0, 0, 0)"> </span><span style="color:rgb(102, 102, 0)">}</span><span style="color:rgb(0, 0, 0)"> echo </span><span style="color:rgb(0, 136, 0)">"Your Delivery Date: $delivery"</span><span style="color:rgb(102, 102, 0)">;</span></pre> <p> </p> <p>(Originally posted on my blog: <a href="http://megabyterose.com/strtotime/">http://megabyterose.com/strtotime/</a>)</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus