Jump to content

PHP & MSSQL :: Time Computation


deyvie

Recommended Posts

Hello Everyone,

 

I am currently making a script to compute how much time in minutes has elapsed between two timestamps, but the catch is I only need to compute for the time between 9:00AM to 6:00PM.

 

I was already able to compute for the time difference if I do not consider the window from 9:00AM to 6:00PM. Does anyone have an idea on how I can incorporate the window?

 

Ex.

Start Time: July 19 2007 5:00PM

End Time  : July 20 2007 9:23AM

 

Time Elapsed: 83 Minutes

 

Thank you!

deyvie

Link to comment
Share on other sites

Yeah, do it like this...

 

$startstamp=123456789;
$endstamp=987654321;
$startmorning=mktime(9, 0, 0,date(j), date(n), date(Y));
$endevening=mktime(18, 0, 0, date(j), date(n), date(Y));
if ($startmorning > $startstamp) {
$startstamp=$startmorning;
}
if ($endevening < $endstamp) {
$endstamp=$endevening;
}
$validhours=$endstamp-$startstamp;

 

So... what it's doing is working out the value of 9am and 6pm each day as a timestamp, and if the user's timestamp starts earlier than the 9am one, it takes the 9am one as the start of the day, and for the evening one if the user's one is bigger, it takes 6pm as the end of the day. It then calculates the difference between the start and end times and stores them in the timestamp $validhours.

 

Should work but i haven't tested it as i'm at work. Obviously lots to do today!

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.