Jump to content

Date in 3 days time minus weekends


CanMan2004

Recommended Posts

Hi all

I currently print on my page, todays date, the code I use is

[code]<?php $date = date('Y,m,d'); ?[/code]

How can you get php date to display the date in 3 days time, but skipping weekends, so if it was Friday the 5th, it would show Wednesday the 10th, but if it was Monday the 1st, it would show Thursday the 5th.

Does that make sense?

I am coding something, I just want other people suggestions as my code is long and doesnt always work out correct.

Any help would be fab

Cheers in advance

Dave
Link to comment
Share on other sites

[code]
<?php //sounds challenging :P
$date = strtotime($date);
$week = date('D', $date);
if ($week == "Thu" or $week == "Fri") {$date = $date + 432000;}// since thu and fri are special case
else {$date = $date + 259200;}//rest are fine
?>
[/code]
but it doesnt consider input values of Sat and Sun, since they are not the results of your output as well. so only works through inputs of mon-fri
took me a while to think  :P
Ted
Link to comment
Share on other sites

sorry... forgot wed....
[code]
<?php //sounds challenging :P
$date = strtotime($date);
$week = date('D', $date);
if ($week == "Wed" or $week == "Thu" or $week == "Fri") {$date = $date + 432000;}// since wed, thu and fri are special case
else {$date = $date + 259200;}//rest are fine
?>
[/code]
I think that would work....
Link to comment
Share on other sites

if you need a clue of what i am trying to do here:
Since you want to skip the weekends, therefore, only concerns Wed, Thu, and Fri.
Others such as Mon and Tue only go up to Thu and Fri.
So for Wed, Thu and Fri, I add 5 days instead of three days, therefore, the two days (sat and sun) of the weekends are counted into the addition.
Does that make it clear?
Ted
Link to comment
Share on other sites

I would try something like this (again, maybe not the most optimal, but at least it's reliable):
[code]
<?php
function getThreeDays($date) {
$day = 60 * 60 * 24;
$newDate = strtotime($date . " + 3 days");
// Sunday is the result, add one day
if (date('w', $newDate) == 0) $newDate += $day;
// Saturday is the result, add two days
elseif (date('w', $newDate) == 6) $newDate += (2 * $day);

return date('Y-m-d', $newDate);
}
?>
[/code]

Tested, and it seems to work well. Feed it a YYYY-MM-DD format, and it returns the same with the correct date.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.