简体   繁体   中英

Minus 7 days from MYSQL date variable in PHP?

I have a script that counts how many days have past since the start date stored in my database. The theory is, a user signs up and has 7 days to complete registration, from the admin side of things, this script is used to monitor where a user is upto and how many days have past since they registered.

So in this example we give the user 7 days grace in which to complete. This echos out like '(number of days out of 7 have past'

Then after 7 days this should change to give the number of days overdue. so if a user has 7 days to complete and they take 8 days then this will take into account the first 7 days grace and then have an overdue date of 1 day.

so for instance it should echo out '1 day overdue', rather than what it is currently doing and saying 8 days overdue, i am trying to do this by minusing the first 7 days.

i have been told this will help however i am having trouble getting it to work with my date variable

$pre_date=date('Y-m-d', strtotime('-7 days'));

heres my compelte code, please can someone show me where i am going wrong

<?php include 'config.php';
     $data = mysql_query("SELECT *,
                            TIMESTAMPDIFF(DAY, date, CURDATE()) AS expire_date
                          FROM supplier_session
                          ORDER BY expire_date ASC") 
     or die(mysql_error()); 

     echo "<table class=\"table\" style=\"width:995px;  font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif;
     font-size:11px;\" >

<tr>
    <td style=\"width:100px;\">ID:</td><td>Company Name:</td><td>Company Reg No:</td><td>Application Started:</td><td style=\"width:200px;\">Application Duration:</td><td style=\"width:100px;\">Date:</td><td>Status:</td></tr>";


     while($row = mysql_fetch_array( $data )) { 
       $days = $row['expire_date'];
       $when = $days*0; 
       $str = $row['expire_date'];
       $str2 = substr($str, 0); // "quick brown fox jumps over the lazy dog."

       $pre_date=date('Y-m-d', strtotime('-7 days'));

       if ($when <= 31){
         echo "<tr><td style=\"width:100px;\"><p>".$row['id'] . "</p></td>"; 
         echo "<td style=\"width:150px;\"><p>".$row['company_name'] . "</p></td>"; 
         echo "<td style=\"width:150px;\"><p>".$row['company_reg_number'] . "</p></td>";
         echo "<td>"; echo date('d/m/Y',strtotime($row['date'])); echo "</td>";

         if ($days >= 8) {
            echo "<td style=\"width:200px;\"><p>{$pre_date} days overdue</td>";      
         }

        elseif ($when <= 7){
             echo "<td style=\"width:200px;\"><p>{$str2} of 7 days past</td>";
          }

        }

        echo "<tr>";
      }

      echo "</table>"; //Close the table in HTML
    ?>

I think u have some mistake in ur function

$pre_date=date('Y-m-d', strtotime('-7 days'));

For example u can try to use somth like that

$datetime = date("Y-m-d", strtotime( date( 'Y-m-d' )." +2 days"));

Result of function looks like:

2014-05-08

Also there more information what u need:

Click.

With unixtime looks like that. We take difference between 2 dates in second and divide it on number of seconds in day

<?php
date_default_timezone_set('UTC');
$now_time = time();
$date_before =  time()-(7*24*60*60);// 7 days; 24 hours; 60 mins; 60secs
echo($now_time);
echo('<br>');
echo($date_before);
echo('<br>');
$difference = $now_time - $date_before;
$result = intval($difference/(24*60*60)); //seconds in day
echo($difference);
echo('<br>');
echo($result);

also u can get time from date with:

$unixtime = strtotime('22-09-2008');

You've forgotten to pass the date variable to strtotime, so you are always substracting 7 days to the actual date and getting 8 as a result.

Try with a subraction of the actual date minus the expire date in order to get the number of days that have past.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM