Date calculation in php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • janna48
    New Member
    • Jul 2007
    • 11

    Date calculation in php

    Hey guys,

    Ive posted a question about making a popup that will come out two months before the expiry date. acoder has helped me with the popup thingy, but im facing this problem of calculating the date that will make the popup come out 2 months before the expiry date, in simpler words, its like a notification. if lets say today is the date that is exactly two months before the expiry date, when the user logs in, the popup will automatically come out. i really hope u guys can help me with this. thank you so much
  • Botondus
    New Member
    • Jul 2007
    • 9

    #2
    I'm not an expert when it comes to PHP, but this is one way of doing it, issue a warning X days before the expiry date:
    [PHP]
    $expiry_date = '2007-11-23'; // example
    $timestamp = strtotime($expi ry_date);
    $warning_days = 60;
    $seconds_diff = $warning_days * 24 * 3600; // taking 60 (days) * 24 (hours) * 3600 (seconds)
    $warning_timest amp = $timestamp - $seconds_diff;
    $warning_date = date('Y-m-d', $warning_timest amp);
    echo $warning_date;
    [/PHP]

    This will output 2007-09-24, that means a warning 60 days before the expiry date. If you really want a 2 month difference like 2007-11-23 <-> 2007-09-23 ( i don't think it's necessary for this) which takes into account the nr. of days of each month then you'll have to calculate the $warning_days yourself instead of assigning it an arbitary value.

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, janna48.

      Try using date() to convert the date into a string, then add 2 to the month representation, then use strtotime() to convert it back.

      Comment

      • Botondus
        New Member
        • Jul 2007
        • 9

        #4
        Originally posted by pbmods
        Heya, janna48.

        Try using date() to convert the date into a string, then add 2 to the month representation, then use strtotime() to convert it back.
        If you go at it this way, just make sure to deal with the special conditions like going over to new year and invalid dates ( 2007-12-31 -> 2007-14-31 -> 2008-02-28/29). It's fairly simple though.

        Comment

        • janna48
          New Member
          • Jul 2007
          • 11

          #5
          Thanks guys for replying. I'm now slowly getting the picture of it. Hmm, but still I think I really am a slow learner and I find it hard to understand the codes in php.

          This is the clearer picture of what I wanted to do but i failed. I wanted to do this admin system which to manage the customer data, that have forms to input the data in the database. The admin can insert, edit, delete the data and also view the report. I've finish making all these, but I wanted to do this alert thingy that can notify me which customer's contract will expire in two months time, each time i log in along with the company details.

          This coding work just fine, but, this is not what i really wanted.


          <?php

          $expiry_date = $_GET['expiry_date'];

          $query = "SELECT * FROM cust_domain WHERE TO_DAYS(NOW()) - TO_DAYS(expiry_ date) <= 30";
          $result = mysql_query($qu ery) or die ('Error in query: $query. ' . mysql_error());
          $row=mysql_fetc h_row($result);
          if ($row)
          {
          ?>

          <script type="text/javascript">
          alert('Contract will expire in two months');
          </script>

          <?php
          }
          ?>

          Comment

          • Botondus
            New Member
            • Jul 2007
            • 9

            #6
            Originally posted by janna48
            TO_DAYS(NOW()) - TO_DAYS(expiry_ date) <= 30";
            ?>
            This will almost always be true because TO_DAYS(NOW()) should be lower then TO_DAYS(expiry_ date) and you get a negative value which is almost always <= 30 (except when the contract is already expired). Invert it.


            Originally posted by janna48
            This coding work just fine, but, this is not what i really wanted.
            How is this code working fine exactly? What does it do and what did you expect it to do?

            Comment

            • janna48
              New Member
              • Jul 2007
              • 11

              #7
              Originally posted by Botondus
              This will almost always be true because TO_DAYS(NOW()) should be lower then TO_DAYS(expiry_ date) and you get a negative value which is almost always <= 30 (except when the contract is already expired). Invert it.
              ohh ya, it was supposed to be,
              "SELECT * FROM cust_domain WHERE TO_DAYS(NOW()) - TO_DAYS(expiry_ date) <= 60"
              Is it?but still, I think this code is useless for me.sigh.


              Originally posted by Botondus
              How is this code working fine exactly? What does it do and what did you expect it to do?
              What I meant by the code is working fine is the code produces no error and the pop up came out.
              What I expect it to do is, every time I log in, if there is a contract that will expire in two months time, in that particular day, then the popup will come out to notify me, including the company's details. if there is no contract that will expire, then it will do nothing, no popup will come out. Its like two months in prior reminder alert. And yeah, the expiry date value has to be obtained from the database.

              Comment

              • Botondus
                New Member
                • Jul 2007
                • 9

                #8
                Originally posted by janna48
                ohh ya, it was supposed to be,
                "SELECT * FROM cust_domain WHERE TO_DAYS(NOW()) - TO_DAYS(expiry_ date) <= 60"
                Is it?but still, I think this code is useless for me.sigh.
                Doh. No it's supposed to be like this:
                [CODE=sql]"SELECT * FROM cust_domain WHERE TO_DAYS(expiry_ date) - TO_DAYS(NOW()) = 60"
                [/CODE]

                Try something like this. Fill in the missing specific info like fields etc. With this code you should have in $contract all the info from those cust_domains whose contract will expire exactly 60 days from the current date.

                [PHP]
                <?php

                $expiry_date = $_GET['expiry_date'];

                $query = "SELECT * FROM cust_domain WHERE TO_DAYS($expiry _date) - TO_DAYS(NOW()) = 60";
                $result = mysql_query($qu ery) or die ('Error in query: $query. ' . mysql_error());
                $contracts= "";
                while($row=mysq l_fetch_assoc($ result)){
                $contracts .= "Field1: $row[field1]";
                $contracts .= "Field2: $row[field2]";
                ...
                $contracts .= "<br />";
                }
                ?>[/PHP]

                Comment

                • janna48
                  New Member
                  • Jul 2007
                  • 11

                  #9
                  Originally posted by Botondus

                  Try something like this. Fill in the missing specific info like fields etc. With this code you should have in $contract all the info from those cust_domains whose contract will expire exactly 60 days from the current date.

                  [PHP]
                  <?php

                  $expiry_date = $_GET['expiry_date'];

                  $query = "SELECT * FROM cust_domain WHERE TO_DAYS($expiry _date) - TO_DAYS(NOW()) = 60";
                  $result = mysql_query($qu ery) or die ('Error in query: $query. ' . mysql_error());
                  $contracts= "";
                  while($row=mysq l_fetch_assoc($ result)){
                  $contracts .= "Field1: $row[field1]";
                  $contracts .= "Field2: $row[field2]";
                  ...
                  $contracts .= "<br />";
                  }
                  ?>[/PHP]
                  Hmm.. I don't quite understand with the $contracts thingy. Ive tried your code but nothing came out. This is what Im using now, its similar to the code I have previously posted. I just want to include the value from database using php in that alert, but i don't know how.

                  [PHP]<?php



                  $expiry_date = $_GET['expiry_date'];

                  $query = "SELECT * FROM cust_domain WHERE TO_DAYS(expiry_ date) - TO_DAYS(NOW()) <= 60";
                  $result = mysql_query($qu ery) or die ('Error in query: $query. ' . mysql_error());
                  $row=mysql_fetc h_row($result);

                  $contracts= "";

                  while($row=mysq l_fetch_assoc($ result)){

                  $contracts .= "Field1: $row[field1]";
                  $contracts .= "Field2: $row[field2]";
                  $contracts .= "<br />";
                  }
                  if ($row)
                  {
                  ?>

                  <script type="text/javascript">
                  alert('Domain will ' <?= $contracts ?> 'expire in two months');
                  document.locati on.href='report Domain.php';
                  </script>

                  <?php
                  }
                  else
                  {
                  echo "<script>docume nt.location.hre f='reportDomain .php'</script>";
                  }
                  ?>[/PHP]

                  You probably laugh looking at my code. But I don't really have much knowledge in php and Im just trying my luck by changing/ adding here and there to the code hoping that it will work, sigh.


                  Ohh ya, and when i do like this:
                  [PHP]<?php



                  $expiry_date = $_GET['expiry_date'];

                  $query = "SELECT * FROM cust_domain WHERE TO_DAYS(expiry_ date) - TO_DAYS(NOW()) <= 60";
                  $result = mysql_query($qu ery) or die ('Error in query: $query. ' . mysql_error());
                  $row=mysql_fetc h_row($result);
                  if ($row)
                  {
                  ?>

                  <script type="text/javascript">
                  alert('Domain will expire in two months');
                  document.locati on.href='report Domain.php';
                  </script>

                  <?php
                  }
                  else
                  {
                  echo "<script>docume nt.location.hre f='reportDomain .php'</script>";
                  }
                  ?>[/PHP]

                  And I include this file to other page, the pop up kept on coming out after i clicked OK, non-stop. I thought after I clicked OK, it will be redirected to 'reportDomain.p hp'. I asked my friend and he said that it has to do with cookie. Is it true?

                  Comment

                  Working...