unknown1 Posted November 4, 2009 Share Posted November 4, 2009 Hello all, I have created a script that takes dates from my database and adds 90 days to the date and gives and expiry date... now I'm trying to count the total number of days between the date in database to the 90 days mark. I have reviewed the date() function and tried many things... but still can't figure it out. Can some explain how to do this please. <?php include('config.php'); $sql_rdate="SELECT * FROM table WHERE status='1' "; $rs_dt=mysql_query($sql_rdate) or die (mysql_error()); while($row= mysql_fetch_assoc($rs_dt)){ $expire=date('y-m-d',strtotime($row['rDate'].' + 90 days')); //echo "$expire ".$row['Url']. "<br>"; $today=date('y-m-d'); if($today == $expire){ echo"Your listing ".$row['Url']." expired on $expire<br>"; }else{ echo"Your listing ".$row['Url']." will expire on $expire <br>"; } } ?> Quote Link to comment Share on other sites More sharing options...
joel24 Posted November 4, 2009 Share Posted November 4, 2009 i'm assuming you mean you want to find how many days between now and the 90 day expiry date? not between date in the db and the 90 day mark.. which would be 90days? make the database date into a unix timestamp usking the mktime() function.. http://php.net/manual/en/function.mktime.php //get date from db, i just put in a mock date, this is in mysql db date format $date = "2009-10-06"; $date = explode("-", $date); $day = $date[2]; $month = $date[1]; $year = $date[0]; $timestamp= mktime(0,0,0,$day, $month, $year); if (($timestamp - time()) < 0) { //it has expired } else { //get days - divide timestamp (seconds) by 60 to get minutes, etc $days = ($timestamp - time()) / 60 / 60 / 24; echo "there are $days days left till expiry"; } also if the date in the DB still needs 90 added to it, you can change the line $timestamp= mktime(0,0,0,$day, $month, $year); to $timestamp= mktime(0,0,0,$day + 90, $month, $year); Quote Link to comment Share on other sites More sharing options...
unknown1 Posted November 4, 2009 Author Share Posted November 4, 2009 Thanks!! Quote Link to comment Share on other sites More sharing options...
unknown1 Posted November 4, 2009 Author Share Posted November 4, 2009 Not working... dates are all messed up.... include('config.php'); $sql_rdate="SELECT * FROM table WHERE status='1' "; $rs_dt=mysql_query($sql_rdate) or die (mysql_error()); while($row= mysql_fetch_assoc($rs_dt)){ $date = $row['rDate']; $date = explode("-", $date); $day = $date[2]; $month = $date[1]; $year = $date[0]; $expired=mktime(0,0,0,$day + 90, $month, $year); $timestamp= mktime(0,0,0,$day, $month, $year); if (($timestamp == $expired) { echo"Your listing ".$row['Url']." expired on $expire<br>"; } else { / /get days - divide timestamp (seconds) by 60 to get minutes, etc $days = ($timestamp - time()) / 60 / 60 / 24; echo "there are $days days left till expiry"; } } ?> Not sure what I'm doing wrong... but all the dates are not right. Quote Link to comment Share on other sites More sharing options...
joel24 Posted November 4, 2009 Share Posted November 4, 2009 what exactly are you trying to find? the amount of days between now and the expiry... ? if (($timestamp == $expired) that will only be true for one second in all of eternity. the mktime function shows how many seconds have passed since jan 1st 1970... AKA a unix timestamp... its a number like 328947239847 the time() function returns the current unix timestamp so if you changed that to <?php include('config.php'); $sql_rdate="SELECT * FROM table WHERE status='1' "; $rs_dt=mysql_query($sql_rdate) or die (mysql_error()); while($row= mysql_fetch_assoc($rs_dt)){ $date = $row['rDate']; $date = explode("-", $date); $day = $date[2]; $month = $date[1]; $year = $date[0]; $expired=mktime(0,0,0,$day + 90, $month, $year); if (time() >= $expired) { $expiry = date("d/m/y", $expired); echo"Your listing ".$row['Url']." expired on $expiry<br>"; } else { / /get days - divide timestamp (seconds) by 60 to get minutes, etc $days = ($timestamp - time()) / 60 / 60 / 24; echo "there are $days days left till expiry"; } } ?> Quote Link to comment Share on other sites More sharing options...
unknown1 Posted November 4, 2009 Author Share Posted November 4, 2009 Okay, I tried to change my code to work the same and it doesn't calculate the days right.... I get -14552.7141088 days left till expiry but this is not correct.... how is it possible that they have -14552.7141088 days left till expiry when total days is only 90 days?? Anywanys, something is worng here... does anyone have any ideas?? Quote Link to comment Share on other sites More sharing options...
joel24 Posted November 5, 2009 Share Posted November 5, 2009 sorry i was using 2 different variables for the timestamp ($expired and $timestamp) try this n see if it works. also, copy and paste what it prints in here, so I can see if the dates are going in correctly from the DB. <?php include('config.php'); $sql_rdate="SELECT * FROM table WHERE status='1' "; $rs_dt=mysql_query($sql_rdate) or die (mysql_error()); while($row= mysql_fetch_assoc($rs_dt)){ $date = $row['rDate']; $date = explode("-", $date); $day = $date[2]; $month = $date[1]; $year = $date[0]; $expired=mktime(0,0,0,$day + 90, $month, $year); //check date is going in correctly print_r($date); echo "<br />"; //check timestamps echo "exp: $expired<br />now: " . time() . '<br />'; if (time() >= $expired) { $expiry = date("d/m/y", $expired); echo"Your listing ".$row['Url']." expired on $expiry<br>"; } else { / /get days - divide timestamp (seconds) by 60 to get minutes, etc $days = ($expired - time()) / 60 / 60 / 24; echo "there are $days days left till expiry"; } } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.