Jump to content

Calculate Days Left On Specific Time


svict4

Recommended Posts

This problem shouldn't be too hard, more of a logic problem than anything, and for the life of me I can't figure out what to do, kinda hit a mental blank :(

 

So, I've got this script set for some date in the future which calculates how many business days (and custom holidays) are left until said date. Everything is hunky dory. However, i want to calculate how many days left, from 3PM... not midnight. Here is the code that I've lovingly ripped off SO:

 

<?php

$today = date("Y-m-d");

//The function returns the no. of business days between two dates

function getWorkingDays($startDate,$endDate){

// do strtotime calculations just once

$endDate = strtotime($endDate);

$startDate = strtotime($startDate);

 

 

//The total number of days between the two dates. We compute the no. of seconds and divide it to 60*60*24

//We add one to inlude both dates in the interval.

$days = ($endDate - $startDate) / 86400 + 1;

 

$no_full_weeks = floor($days / 7);

$no_remaining_days = fmod($days, 7);

 

//It will return 1 if it's Monday,.. ,7 for Sunday

$the_first_day_of_week = date("N", $startDate);

$the_last_day_of_week = date("N", $endDate);

 

//---->The two can be equal in leap years when february has 29 days, the equal sign is added here

//In the first case the whole interval is within a week, in the second case the interval falls in two weeks.

if ($the_first_day_of_week <= $the_last_day_of_week) {

if ($the_first_day_of_week <= 6 && 6 <= $the_last_day_of_week) $no_remaining_days--;

if ($the_first_day_of_week <= 7 && 7 <= $the_last_day_of_week) $no_remaining_days--;

}

else {

// (edit by Tokes to fix an edge case where the start day was a Sunday

// and the end day was NOT a Saturday)

 

// the day of the week for start is later than the day of the week for end

if ($the_first_day_of_week == 7) {

// if the start date is a Sunday, then we definitely subtract 1 day

$no_remaining_days--;

 

if ($the_last_day_of_week == 6) {

// if the end date is a Saturday, then we subtract another day

$no_remaining_days--;

}

}

else {

// the start date was a Saturday (or earlier), and the end date was (Mon..Fri)

// so we skip an entire weekend and subtract 2 days

$no_remaining_days -= 2;

}

}

 

//The no. of business days is: (number of weeks between the two dates) * (5 working days) + the remainder

//---->february in none leap years gave a remainder of 0 but still calculated weekends between first and last day, this is one way to fix it

$workingDays = $no_full_weeks * 5;

if ($no_remaining_days > 0 )

{

$workingDays += $no_remaining_days;

}

 

 

return $workingDays;

}

$holidays=array("2012-10-22 12:00AM");

?>

 

​Which is invoked by this

 

<?php echo getWorkingDays($today,"2012-11-16",$holidays) ?>

 

Now, how would I calculate the days remaining from 3PM...? Any help would be much appreciated :D

Link to comment
Share on other sites

Hey, I don't see how that would change the result if the clock were to tick till 3pm though.....

 

lets say that it is 2pm and the code outputs 27 business days.... wait two hours, now it should say 26 business days....

 

i assume that i need to alter the code to calculate hours as well? (adding in hours to the input for current date/time and the end date/time... but there must be more to that as well?)

Link to comment
Share on other sites

Don't bother counting hours, just do:

if ( date('H') < 15 ) { 
 echo $days + 1;
} else {
 echo $days;
}

Your question is just "I have the right number most of the day, but after 3pm I want it to be one less." Don't bother adding hours calculations into your function, just make it one less after 3pm.

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.