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
https://forums.phpfreaks.com/topic/60723-php-mssql-time-computation/
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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.