Hi, I want to have a date calculator. I already tried to make something, but I didn't succeed. This is what I'm trying to make http://buxtopayments.com/ An estimated payment calculator for my website. As you can see you need to be able to enter a date in numeric format, then click the button. The script needs to calculate the number of working day and actual days between the entered date and the date + 30 workingdays. I hope somebody can help me with this script. If you need more information, just ask here Thanks!!
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <p>Please enter date in the format day/month/year:</p> Day: <input type="text" name="date_day" size="2" maxlength="2" value="<?php echo date("d"); ?>"> Month: <input type="text" name="date_month" size="2" maxlength="2" value="<?php echo date("n"); ?>"> Year: <input type="text" name="date_year" size="4" maxlength="4" value="<?php echo date("Y"); ?>"> <input type="submit" name="submit" value="Calculate number of days"> </form> <?php if (!empty($_POST['submit'])) { $startDate = $_POST['date_year']."-".$_POST['date_month']."-".$_POST['date_day']; $startTime = strtotime($startDate); $currentTime = $startTime; for ($i=0;$i<30;$i++) { $currentTime += (60*60*24); while (date("N",$currentTime) == 7 || date("N",$currentTime) == 6) { $currentTime += (60*60*24); } } echo "<p>The date 30 working days away from $startDate is ".date("Y-n-d",$currentTime).".</p>"; echo "<p>The total number of days from $startDate to ".date("Y-n-d",$currentTime)." is ".(($currentTime-$startTime)/(60*60*24)).".</p>"; } ?> PHP: It doesn't deal with holidays, just weekends but this will give you the date+30 working days and the total number of actual days between those dates.
Wow, thank you very much. Now I can continue my script Gr8 job! Rep+ Edit: Maybe one more modification if possible. Can you add this how many days remaining? Example: Person checks date 12 03 2008 and gets these results: - 30 days away is Monday 28th of April 2008 which is 42 actual days - You have to wait 14 more days.
You're welcome. Thanks for the rep+. This update should give the output you want: <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <p>Please enter date in the format day/month/year:</p> Day: <input type="text" name="date_day" size="2" maxlength="2" value="<?php echo (!empty($_POST['date_day'])) ? $_POST['date_day'] : date("d"); ?>"> Month: <input type="text" name="date_month" size="2" maxlength="2" value="<?php echo (!empty($_POST['date_month'])) ? $_POST['date_month'] : date("n"); ?>"> Year: <input type="text" name="date_year" size="4" maxlength="4" value="<?php echo (!empty($_POST['date_year'])) ? $_POST['date_year'] : date("Y"); ?>"> <input type="submit" name="submit" value="Calculate number of days"> </form> <?php if (!empty($_POST['submit'])) { $startDate = $_POST['date_year']."-".$_POST['date_month']."-".$_POST['date_day']; $startTime = strtotime($startDate); $currentTime = $startTime; $todayTime = strtotime(date("Y-n-d")); for ($i=0;$i<30;$i++) { $currentTime += (60*60*24); while (date("N",$currentTime) == 7 || date("N",$currentTime) == 6) { $currentTime += (60*60*24); } } echo "<p>30 working days away is ".date("l jS \of F Y",$currentTime)." which is ".intval(($currentTime-$startTime)/(60*60*24))." actual days.</p>"; $daysLeft = intval(($currentTime-$todayTime)/(60*60*24)); if ($daysLeft > 0) { echo "<p>You have to wait ".$daysLeft." more days.</p>"; } else { echo "<p>Your wait is over.</p>"; } } ?> PHP:
Yes, works great again! Sorry to be annoying again, but I need a few extras. Is it possible to include a holiday list in an array or so. For example the script calculates there are 42 working days left, but there are 3 holidays in it, it must return 39 as result. So I need: - The actual amount of days (with weekend and holidays included) -> this is correct now I think - The amount of workingdays (without holidays) -> needs to be done - The exact payment day -> already done! - Put the amount of holidays between the two dates in a variable so I can give the visitor the information about the holidays. - Is it possible that in the inputform when you press the 'Calculate number of days' button the entered numbers stay? Now I automaticly switches back to the current date. When the visitor isn't watching results, thats fine, but when the user entered a date, he needs to see the date in the inputforms. I hope you can do it! You've been a great help so far!!
This should help: http://us.php.net/manual/en/function.date.php#80389 You'll need to make your own holidays array though, which should be pretty simple.
One more update... You just need to add an array for the holidays and check for them in the while loop. Here's the code: <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <p>Please enter date in the format day/month/year:</p> Day: <input type="text" name="date_day" size="2" maxlength="2" value="<?php echo (!empty($_POST['date_day'])) ? $_POST['date_day'] : date("d"); ?>"> Month: <input type="text" name="date_month" size="2" maxlength="2" value="<?php echo (!empty($_POST['date_month'])) ? $_POST['date_month'] : date("n"); ?>"> Year: <input type="text" name="date_year" size="4" maxlength="4" value="<?php echo (!empty($_POST['date_year'])) ? $_POST['date_year'] : date("Y"); ?>"> <input type="submit" name="submit" value="Calculate number of days"> </form> <?php $holidays = Array("2008-4-16","2008-4-17"); $numberHolidays = 0; if (!empty($_POST['submit'])) { $startDate = $_POST['date_year']."-".$_POST['date_month']."-".$_POST['date_day']; $startTime = strtotime($startDate); $currentTime = $startTime; $todayTime = strtotime(date("Y-n-d")); for ($i=0;$i<30;$i++) { $currentTime += (60*60*24); while (date("N",$currentTime) == 7 || date("N",$currentTime) == 6 || in_array(date("Y-n-d",$currentTime), $holidays)) { if (in_array(date("Y-n-j",$currentTime), $holidays)) $numberHolidays++; $currentTime += (60*60*24); } } echo "<p>30 working days away is ".date("l jS \of F Y",$currentTime)." which is ".intval(($currentTime-$startTime)/(60*60*24))." actual days.</p>"; echo "<p>This includes $numberHolidays holiday dates</p>"; $daysLeft = intval(($currentTime-$todayTime)/(60*60*24)); if ($daysLeft > 0) { echo "<p>You have to wait ".$daysLeft." more days.</p>"; } else { echo "<p>Your wait is over.</p>"; } } ?> PHP: I think it already does everything else you requested.
how to calculate difference of date.. if date will be in this format 2008/03/31(YYYY/MM/DD) 2008/04/20(YYYY/MM/DD) This code s not working. <?php function dateDiff($dformat, $endDate, $beginDate) { $date_parts1=explode($dformat, $beginDate); $date_parts2=explode($dformat, $endDate); $start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]); $end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]); return $end_date - $start_date; } $date1="07/11/2003"; $date2="09/04/2004"; print "If we subtract " . $date1 . " from " . $date2 . " we get " . dateDiff("/", $date2, $date1) . "."; ?>