Jump to content


Photo

Help,..with count up days script


  • Please log in to reply
4 replies to this topic

#1 piznac

piznac
  • Members
  • PipPipPip
  • Advanced Member
  • 261 posts

Posted 17 April 2006 - 02:55 PM

Ok,..little help needed.

I need to have a script that will count up days from a specific date excluding weekends and holidays. And display the results on a webpage.

Example:

30 days worked since last accident.

I need this to work automatically and be able to reset it to the current date when an accident occurs (reset manually, with the click of a button would be best. I had thought about the date being stored in a mysql DB and using that as a variable, so you could update the date via web form and it would change the data. But not sure if that would work). I have no idea where to start on this one. I know a way to do it in VB coding, due to a co worker having to do the same thing on a spreadsheet. But that will not work here. Does anyone know of a php function that will do this? any help would be great from an entire script to a push in the right direction.

Thanks

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 17 April 2006 - 04:56 PM

The easiest way (may not be the most efficient) would be to convert the accident date to a UNIX timestamp and count from there until today, counting all days (excluding Saturdays & Sundays)

For example:
<?php
$accident_date = 'March 8, 2006';
$acc_ts = time($accident_date); // Get UNIX Timestamp (number of seconds since 1/1/1970)
$today = time();
$days = 0;
for ($test = acc_ts; $test <= $today; $test += 86400) { // 86400 = number of seconds in a day
     $dy = date('l',$test); // Thats a lowercase L, not an uppercase i or number 1. Gives day of week
     if ($dy != 'Saturday' &&  $dy != 'Sunday') $days++;
}
echo 'Days since last accident: ' . $days;
?>

Note: this code hasn't been tested.

Ken


#3 piznac

piznac
  • Members
  • PipPipPip
  • Advanced Member
  • 261 posts

Posted 17 April 2006 - 08:03 PM

[!--quoteo(post=365586:date=Apr 17 2006, 12:56 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 17 2006, 12:56 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
The easiest way (may not be the most efficient) would be to convert the accident date to a UNIX timestamp and count from there until today, counting all days (excluding Saturdays & Sundays)

For example:
<?php
$accident_date = 'March 8, 2006';
$acc_ts = time($accident_date); // Get UNIX Timestamp (number of seconds since 1/1/1970)
$today = time();
$days = 0;
for ($test = acc_ts; $test <= $today; $test += 86400) { // 86400 = number of seconds in a day
     $dy = date('l',$test); // Thats a lowercase L, not an uppercase i or number 1. Gives day of week
     if ($dy != 'Saturday' &&  $dy != 'Sunday') $days++;
}
echo 'Days since last accident: ' . $days;
?>

Note: this code hasn't been tested.

Ken
[/quote]

Thanks for your help Ken. Ok I don't really understand this bit of code. Now I know you said it wasnt tested but its displaying 9468 days. How did it come to that conclusion? Im confused. Just a little more help please


[!--quoteo(post=365649:date=Apr 17 2006, 03:56 PM:name=piznac)--][div class=\'quotetop\']QUOTE(piznac @ Apr 17 2006, 03:56 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Thanks for your help Ken. Ok I don't really understand this bit of code. Now I know you said it wasnt tested but its displaying 9468 days. How did it come to that conclusion? Im confused. Just a little more help please
[/quote]




#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 17 April 2006 - 08:24 PM

This will fix it:
<?php
$accident_date = 'March 8, 2006';
$acc_ts = strtotime($accident_date); // Get UNIX Timestamp (number of seconds since 1/1/1970)
$today = time(); // Get the UNIX timestamp for today
$days = 0;
for ($test = $acc_ts; $test <= $today; $test += 86400) { // 86400 = number of seconds in a day
     $dy = date('l',$test); // Thats a lowercase L, not an uppercase i or number 1. Gives day of week
     if ($dy != 'Saturday' &&  $dy != 'Sunday') $days++; // if the day isn't a Saturday or Sunday count it.
}
echo 'Days since last accident: ' . $days;
?>

I had used the wrong function to get the UNIX timestamp for the accident_date and I left off the $ in the for loop.

Ken

#5 piznac

piznac
  • Members
  • PipPipPip
  • Advanced Member
  • 261 posts

Posted 17 April 2006 - 08:36 PM

[!--quoteo(post=365661:date=Apr 17 2006, 04:24 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 17 2006, 04:24 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
This will fix it:
<?php
$accident_date = 'March 8, 2006';
$acc_ts = strtotime($accident_date); // Get UNIX Timestamp (number of seconds since 1/1/1970)
$today = time(); // Get the UNIX timestamp for today
$days = 0;
for ($test = $acc_ts; $test <= $today; $test += 86400) { // 86400 = number of seconds in a day
     $dy = date('l',$test); // Thats a lowercase L, not an uppercase i or number 1. Gives day of week
     if ($dy != 'Saturday' &&  $dy != 'Sunday') $days++; // if the day isn't a Saturday or Sunday count it.
}
echo 'Days since last accident: ' . $days;
?>

I had used the wrong function to get the UNIX timestamp for the accident_date and I left off the $ in the for loop.

Ken
[/quote]

That is just awesome. Thanks so much!!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users