Time Difference

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wrathyimp
Forum Newbie
Posts: 3
Joined: Wed Mar 29, 2006 6:17 am

Time Difference

Post 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]
flycast
Forum Commoner
Posts: 37
Joined: Wed Jun 01, 2005 7:33 pm

Post by flycast »

What are you trying to accomplish with this code?
wrathyimp
Forum Newbie
Posts: 3
Joined: Wed Mar 29, 2006 6:17 am

Time Difference between Market Open & Close

Post 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)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

check the Code Snippets section, there's a function posted toward the last few pages that finds the difference between two timestamps.
wrathyimp
Forum Newbie
Posts: 3
Joined: Wed Mar 29, 2006 6:17 am

Post by wrathyimp »

Time stamp difference is calculating fine, but when is comes to weekend calculation, it giving bugs.
Post Reply