toolman Posted October 14, 2017 Share Posted October 14, 2017 Hi, I'm trying to create a simple bit of PHP that will display today's date +3 days. This is what I have: echo "Order today to receive by " . date("l j F Y")+3; I am new to PHP, but what I am trying to do is to add 3 days to a date is the day is a week day and add 5 days if the day is a weekend. How would I achieve this? Quote Link to comment https://forums.phpfreaks.com/topic/305358-add-3-days-to-date/ Share on other sites More sharing options...
requinix Posted October 14, 2017 Share Posted October 14, 2017 Use date() to determine whether today is a weekday or not, then use strtotime to create a date representing however-many days in the future you want. Quote Link to comment https://forums.phpfreaks.com/topic/305358-add-3-days-to-date/#findComment-1552703 Share on other sites More sharing options...
Gandalf64 Posted October 14, 2017 Share Posted October 14, 2017 Or you could use DateTime() -> <?php $variableDate = "October 8, 2017"; /* * DateTime & DateTimezone are classes built into PHP. */ $myDate = new DateTime($variableDate, new DateTimeZone("America/Detroit")); /* * N is a numeric representation -> 1 (for Monday) through 7 (for Sunday) */ if ( $myDate->format("N") < 6) { $myDate->modify("+3 days"); // modify is a method (function) that does what it says { similar to strtotime } } else { $myDate->modify("+5 days"); } echo $myDate->format("l, F j, Y") . "<br>"; // Display it in the format of your choosing: Quote Link to comment https://forums.phpfreaks.com/topic/305358-add-3-days-to-date/#findComment-1552704 Share on other sites More sharing options...
toolman Posted October 15, 2017 Author Share Posted October 15, 2017 Thank you Quote Link to comment https://forums.phpfreaks.com/topic/305358-add-3-days-to-date/#findComment-1552713 Share on other sites More sharing options...
cloetensbrecht Posted October 19, 2017 Share Posted October 19, 2017 echo "Order today to receive by " . date("l j F Y", (time() + (86400 * date("N", time()) < 6 ? 5 : 3))); Quote Link to comment https://forums.phpfreaks.com/topic/305358-add-3-days-to-date/#findComment-1552862 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.