Need help with date calculator

Discussion in 'PHP' started by webmasterplace, Apr 13, 2008.

  1. #1
    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!!
     
    webmasterplace, Apr 13, 2008 IP
  2. stoli

    stoli Peon

    Messages:
    69
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <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.
     
    stoli, Apr 13, 2008 IP
    webmasterplace likes this.
  3. webmasterplace

    webmasterplace Peon

    Messages:
    802
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
     
    webmasterplace, Apr 13, 2008 IP
  4. stoli

    stoli Peon

    Messages:
    69
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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:
     
    stoli, Apr 14, 2008 IP
  5. webmasterplace

    webmasterplace Peon

    Messages:
    802
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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!!
     
    webmasterplace, Apr 14, 2008 IP
  6. qeorge

    qeorge Peon

    Messages:
    206
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    qeorge, Apr 14, 2008 IP
  7. webmasterplace

    webmasterplace Peon

    Messages:
    802
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #7
    webmasterplace, Apr 15, 2008 IP
  8. stoli

    stoli Peon

    Messages:
    69
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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.
     
    stoli, Apr 15, 2008 IP
  9. ksamir2004

    ksamir2004 Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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) . ".";
    ?>
     
    ksamir2004, Apr 16, 2008 IP