ramone_johnny Posted April 11, 2013 Share Posted April 11, 2013 Hey guys, Im wanting to have this bit of code rewritten into PHP. It basically checks the date pulled from the DB and if its only 2 days old, it displays a "new listing" image. <%share_newlisting = DateDiff("d", rstDBEdit("share_datecreated"), now()) if share_newlisting < 2 then%> <div style="margin-top:5px"><img src="/images/new_listing.gif" width="81" height="15" align="absbottom"></div> <%end if%> Link to comment https://forums.phpfreaks.com/topic/276839-convert-this-code-from-asp-to-php-date-related/ Share on other sites More sharing options...
lemmin Posted April 11, 2013 Share Posted April 11, 2013 More information is needed to do this. What type of database are you using and how is it structured? Link to comment https://forums.phpfreaks.com/topic/276839-convert-this-code-from-asp-to-php-date-related/#findComment-1424192 Share on other sites More sharing options...
ramone_johnny Posted April 11, 2013 Author Share Posted April 11, 2013 Sorry, its a mysql database. rstDBEdit("share_datecreated") is a date field. Link to comment https://forums.phpfreaks.com/topic/276839-convert-this-code-from-asp-to-php-date-related/#findComment-1424194 Share on other sites More sharing options...
ramone_johnny Posted April 11, 2013 Author Share Posted April 11, 2013 I tried this as a test, but obviously, it's not right. if(strtotime($rstDBEdit["share_datecreated"])<strtotime('-7 days')){ echo "this is a new ad"; Link to comment https://forums.phpfreaks.com/topic/276839-convert-this-code-from-asp-to-php-date-related/#findComment-1424196 Share on other sites More sharing options...
ramone_johnny Posted April 11, 2013 Author Share Posted April 11, 2013 What about this? $timestamp = strtotime($rstDBEdit["share_datecreated"]);$oneweekago = strtotime("-1 week");if($oneweekago<=$timestamp) {echo "this is a new ad";} ...can this be written better? Link to comment https://forums.phpfreaks.com/topic/276839-convert-this-code-from-asp-to-php-date-related/#findComment-1424201 Share on other sites More sharing options...
lemmin Posted April 11, 2013 Share Posted April 11, 2013 There is still not enough information to do this, but I can give you an idea: mysql_connect($db, $user, $pass); //Connect to db mysql_select_db('yourdb'); //Select db $r = mysql_query('SELECT * FROM yourtable'); //query for listings while ($row = mysql_fetch_assoc($r)) //loop through listings { $days = 2; //set days $seconds = 60 * 60 * 24 * $days; //calculate seconds in days if ((time()-$row['share_datecreated']) < $days) //check stamp //New listing } This assumes that your share_datecreated field is a timestamp and that you have no criteria for which listings to grab from the database. Link to comment https://forums.phpfreaks.com/topic/276839-convert-this-code-from-asp-to-php-date-related/#findComment-1424202 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.