Page 1 of 1

Time Difference

Posted: Wed Mar 29, 2006 6:25 am
by wrathyimp
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi all,

I have this code from the net it just works great on week days but when is comes on the weekends, its not calculating correct. The following are the code:

Code: Select all

$now = strtotime(date("H:i"));
$open = strtotime(9:30);

// set up midnight and closing time according to the day of the week
switch (date(w)) {
   case 1:
   case 2:
   case 3:
   case 4:
   // if it is mon, tues, wed, or thurs, closing time and midnight are normal
      $close = strtotime(12:30);
      $midnight = 86400;
      break;
   // friday, add 48 hours to midnight and closing time is normal
   case 5:
      $close = strtotime(12:30);
      $midnight = 345600;
      break;
   // sat add 24 hours to midnight and closing time is zero
   case 6:
      $close = 0;
      $midnight = 172600;
      break;
   // sunday midnight is normal and closing time is zero
   case 0:
      $close = 0;
      $midnight = 86400;
}

switch (TRUE) {
   // if it is after the market closed calculate time till next open
   // setting close to zero for sat and sun covers the weekend
   // adding extra days to midnight covers friday and saturday
   case ($now > $close):
      $diff = ($midnight - $now) + $open;
      $hours = floor($diff / 3600);
      $minutes = floor(($diff % 3600) / 60);
      $state = 'Market Closed<br />' . $hours . 'hours ' . $minutes . 'minutes till the market opens again';
      break;
   // if it is before opening time then calculate time till open
   case ($now < $open):
      $diff = $open-$now;
      $hours = floor($diff / 3600);
      $minutes = floor(($diff % 3600) / 60);
      $state = 'Market Closed<br />' . $hours . 'hours ' . $minutes . 'minutes till the market opens again';
      break;
   // otherwise the market is open
   default:
      $diff = $close-$now;
      $hours = floor($diff / 3600);
      $minutes = floor(($diff % 3600) / 60);
      $state = 'Market Open<br />' . $hours . 'hours ' . $minutes . 'minutes till the market closes';
}
and also as i am in the middle east country, my weekends are on thursdays & fridays; the case for calculating friday, saturday & sunday will be wednesday, thursday & friday.

So please guide me to fix this code.

thank you.


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Mar 29, 2006 6:56 am
by flycast
What are you trying to accomplish with this code?

Time Difference between Market Open & Close

Posted: Wed Mar 29, 2006 7:19 am
by wrathyimp
I need to add a time difference been the market open and market close (related to stock exchange)

My market opens at 9:15 am and closes at 12:30.
I need to show the time difference between the current time to market open,
and also time left to market close after the market is open.
Also calculating weekends as the market will be closed for two days (thursday & Friday)

Posted: Wed Mar 29, 2006 9:15 am
by feyd
check the Code Snippets section, there's a function posted toward the last few pages that finds the difference between two timestamps.

Posted: Sat Apr 01, 2006 12:36 am
by wrathyimp
Time stamp difference is calculating fine, but when is comes to weekend calculation, it giving bugs.