Jump to content

Help,..with count up days script


piznac

Recommended Posts

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
Link to comment
Share on other sites

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:
[code]<?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;
?>[/code]

Note: this code hasn't been tested.

Ken
Link to comment
Share on other sites

[!--quoteo(post=365586:date=Apr 17 2006, 12:56 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 17 2006, 12:56 PM) [snapback]365586[/snapback][/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:
[code]<?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;
?>[/code]

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) [snapback]365649[/snapback][/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]


Link to comment
Share on other sites

This will fix it:
[code]<?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;
?>[/code]

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
Link to comment
Share on other sites

[!--quoteo(post=365661:date=Apr 17 2006, 04:24 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 17 2006, 04:24 PM) [snapback]365661[/snapback][/div][div class=\'quotemain\'][!--quotec--]
This will fix it:
[code]<?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;
?>[/code]

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!!
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.