wwfc_barmy_army Posted September 15, 2006 Share Posted September 15, 2006 Hello.I am still learning PHP and have been building a site over the last week.I have done well so far, but i've got to a bit where i can't find an answer anywhere.Currently i have a date field that is stored like mm-dd-yyyy, i would like to make it so if the date is less then 7 days old then it will display a 'new' image.I know i need to use an If statement but i'm not 100% sure how to get it to just display it if it's less than 7 days old.Can anyone give me any advice, links or code?Thanks.Peter. :) Quote Link to comment Share on other sites More sharing options...
obsidian Posted September 15, 2006 Share Posted September 15, 2006 there's not a very easy way to search by a date field unless you have it in the actual MySQL date datatype of 'yyyy-mm-dd'. as it is, you can probably do what you're after within your loop to display all items. try something like this:[code]<?php// assuming you have queried all your records and your query result is in the $res variable// set the time 7 days ago$sevenDays = strtotime('today -7 days');while ($x = mysql_fetch_array($res)) { list($mon, $day, $year) = explode('-', $x['dateField']); if (strtotime("$year-$mon-$day") > $sevenDays) { // show your image here since it's new! } // list your item here like normal}?>[/code]hope this makes sense Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 15, 2006 Author Share Posted September 15, 2006 Thanks for the reply.I tried using that but i get this message:Strict Standards: strtotime() [function.strtotime]: It is not safe to rely on the system's timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/London' for '1.0/DST' instead in C:\public_html\RPG\site.php on line 27I'm using this code (includes the changes i made):[code]<?php$sevenDays = strtotime('today -7 days');while ($x = mysql_fetch_array($result)) { list($mon, $day, $year) = explode('-', $x['dateadded']); if (strtotime("$year-$mon-$day") > $sevenDays) { // show your image here since it's new! echo "<img src='/images/new.gif'>"; }}?>[/code]Any ideas?Thanks a LOT in advance!Cheers.Pete. Quote Link to comment Share on other sites More sharing options...
markbett Posted September 15, 2006 Share Posted September 15, 2006 take youre dates and convert them to stringoftime subtract the date in question from time() which is the time at that exact moment... what you are left with is the time difference in seconds between now and the date the record has associated to it.... to figure out if it is 7 days divide by 86400 (the number of seconds in a day (ohh that couldbe wrong it might be 84600) and you will end up with a number of days.... if that number is less then or equal to 7 then it happened in the last 7 days..... Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 15, 2006 Author Share Posted September 15, 2006 Hello.I dont' quite understand what you are saying. What can i do with the code that i have already?Thanks.Peter. Quote Link to comment Share on other sites More sharing options...
markbett Posted September 16, 2006 Share Posted September 16, 2006 $time = time(); $time_diff = round(($stop_time-$time)/86400);compare time_diff to 7 Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 16, 2006 Author Share Posted September 16, 2006 Will this work even though the date is stored (using CURDATE()) into the database as MM/DD/YYYY? Quote Link to comment Share on other sites More sharing options...
markbett Posted September 16, 2006 Share Posted September 16, 2006 you need to convert all your date to string of time like i said Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 16, 2006 Author Share Posted September 16, 2006 I am not sure how to do this, like i said i'm reasonable new to PHP, i currently use 'CURDATE()' to add the date to the database, how do i change it to use as a string of time? Quote Link to comment Share on other sites More sharing options...
markbett Posted September 16, 2006 Share Posted September 16, 2006 http://www.w3schools.com/php/func_date_strtotime.asp Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 16, 2006 Author Share Posted September 16, 2006 No offence but this doesn't help someone new to php like me. Could anyone else help me out a bit more?Thanks.Pete. Quote Link to comment Share on other sites More sharing options...
pkSML Posted September 16, 2006 Share Posted September 16, 2006 Try this.[code]<?phpwhile ($x = mysql_fetch_array($result)) { list($mon, $day, $year) = explode('-', $x['dateadded']); $day = date('z', mktime(0, 0, 0, $mon, $day, $year)) + 1; // day of year for datestamp $today = date("z") + 1; // today's day of year if (($today - $day) < 7) { // show your image here since it's new! echo "<img src='/images/new.gif'>"; }}?>[/code] Quote Link to comment Share on other sites More sharing options...
markbett Posted September 16, 2006 Share Posted September 16, 2006 which works great for looking at how far apart DAYS are but you dont take into account the year or month...so in your code comparing jan 3 with june 5 would result in true when it should be false which is why you should compare them as unixtime Quote Link to comment Share on other sites More sharing options...
pkSML Posted September 16, 2006 Share Posted September 16, 2006 This code is fixed. I hope the syntax is proper. date(z) does not need to take into account the month because it just returns the day of year.This updated code makes sure that the year is the same.[code]<?phpwhile ($x = mysql_fetch_array($result)) { list($mon, $day, $year) = explode('-', $x['dateadded']); $day = date('z', mktime(0, 0, 0, $mon, $day, $year)) + 1; // day of year for datestamp $thisyear = date("Y"); $today = date("z") + 1; // today's day of year if ((($today - $day) < 7) && ($year == $thisyear)) { // show your image here since it's new! echo "<img src='/images/new.gif'>"; }}?>[/code] Quote Link to comment Share on other sites More sharing options...
markbett Posted September 16, 2006 Share Posted September 16, 2006 as i stated earlier the code you keep trying to push is wrong because it just wont work... what happens when something is posted on the 31st and today is the 1st? you do need to account for days months and years so you need to use the stringtotime... why you both refuse to accept this fact is beyond me... its fast simple and less lines of code Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 16, 2006 Author Share Posted September 16, 2006 [quote author=pkSML link=topic=108166.msg435291#msg435291 date=1158368531]This code is fixed. I hope the syntax is proper. date(z) does not need to take into account the month because it just returns the day of year.This updated code makes sure that the year is the same.[code]<?phpwhile ($x = mysql_fetch_array($result)) { list($mon, $day, $year) = explode('-', $x['dateadded']); $day = date('z', mktime(0, 0, 0, $mon, $day, $year)) + 1; // day of year for datestamp $thisyear = date("Y"); $today = date("z") + 1; // today's day of year if ((($today - $day) < 7) && ($year == $thisyear)) { // show your image here since it's new! echo "<img src='/images/new.gif'>"; }}?>[/code][/quote]I ran this, but it doesn't show anything even though the selected record has the dateadded as 3 days ago.Any ideas?Thanks.Peter Quote Link to comment Share on other sites More sharing options...
pkSML Posted September 16, 2006 Share Posted September 16, 2006 wwfc_barmy_army, could you provide an example record of $mon, $day, $year?What I'm thinking: Maybe SQL returns $year as 06 instead of 2006. This would cause the script to never show the NEW graphic. If this is the case, just change the line [b]$thisyear = date("Y");[/b] to [b]$thisyear = date("y");[/b] (just change capital Y to little y). Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 16, 2006 Author Share Posted September 16, 2006 I tried changing the Y to y but it still doesn't show anything.I don't know where '$mon, $day, $year' is coming from. Quote Link to comment Share on other sites More sharing options...
pkSML Posted September 16, 2006 Share Posted September 16, 2006 OK, let's try fixing the original code (hope you don't mind :)).The problem was occurring with the timezone function. Let's set the timezone.[code]<?phpdate_default_timezone_set("US/Eastern");$sevenDays = strtotime('today -7 days');while ($x = mysql_fetch_array($result)) { list($mon, $day, $year) = explode('-', $x['dateadded']); if (strtotime("$year-$mon-$day") > $sevenDays) { // show your image here since it's new! echo "<img src='/images/new.gif'>"; }}?>[/code]My timezone is Eastern, but you can find your timezone at [url=http://us2.php.net/manual/en/timezones.america.php]http://us2.php.net/manual/en/timezones.america.php[/url] if you live in America. Otherwise, find it at [url=http://us2.php.net/manual/en/timezones.php]http://us2.php.net/manual/en/timezones.php[/url].[b]Edit:[/b]About the previous posts, markbett is right: strtotime is better to use than day of year. The only problem that occurs with day of year is when it's time to change years (you'd not see the new graphic for the first seven days of a new year). Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 16, 2006 Author Share Posted September 16, 2006 I don't mind :PI've put that in, but like the last even though the dateadded is less then 7 days ago it doesn't show the image.I've just realised that the date is shown like this: 2006-09-16 , i orginally thought it was different. Quote Link to comment Share on other sites More sharing options...
pkSML Posted September 16, 2006 Share Posted September 16, 2006 OK, that makes a huge difference and explains why it wasn't working right.[code]<?phpdate_default_timezone_set("US/Eastern");$sevenDays = strtotime('today -7 days');while ($x = mysql_fetch_array($result)) { list($year, $mon, $day) = explode('-', $x['dateadded']); if (strtotime("$year-$mon-$day") > $sevenDays) { // show your image here since it's new! echo "<img src='/images/new.gif'>"; }}?>[/code]BTW, the variables $year, $mon, and $day come from $x['dateadded']. The line that begins with list() basically splits the string at every dash mark (using the explode function), and assigns the variables in the order of the list.You might even be able to use more concise code:[code]<?phpdate_default_timezone_set("US/Eastern");$sevenDays = strtotime('today -7 days');while ($x = mysql_fetch_array($result)) { if (strtotime("$x['dateadded']") > $sevenDays) { // show your image here since it's new! echo "<img src='/images/new.gif'>"; }}?>[/code]Do either of these work? Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 16, 2006 Author Share Posted September 16, 2006 Ok, well i tried the first code but still no image is shown, the second code gives an error.An example of the dateadded field is 2006-09-13. This one should work as it is only 3 days old.Any ideas?Thanks!!Peter. Quote Link to comment Share on other sites More sharing options...
pkSML Posted September 16, 2006 Share Posted September 16, 2006 Is the dateadded field separated with dashes or slashes?What error message did you get previously? Quote Link to comment Share on other sites More sharing options...
wwfc_barmy_army Posted September 16, 2006 Author Share Posted September 16, 2006 [quote author=pkSML link=topic=108166.msg435488#msg435488 date=1158412926]Is the dateadded field separated with dashes or slashes?[/quote]Exactly as above, so dashes (2006-09-13)[quote]What error message did you get previously?[/quote]For the [b]second[/b] code i get this error:[quote]Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\public_html\RPG\site.php on line 30[/quote]Thanks.Peter. Quote Link to comment Share on other sites More sharing options...
pkSML Posted September 16, 2006 Share Posted September 16, 2006 For diagnostic's sake, give me the output of this code. It just lists the variables and their values.[code]<?phpdate_default_timezone_set("US/Eastern");$sevenDays = strtotime('today -7 days');while ($x = mysql_fetch_array($result)) { list($year, $mon, $day) = explode('-', $x['dateadded']); if (strtotime("$year-$mon-$day") > $sevenDays) { // show your image here since it's new! echo "<img src='/images/new.gif'>"; } else { print "<PRE>"; print '$year = '.$year."\n"; print '$mon = '.$mon."\n"; print '$day = '.$day."\n"; print "\$x['dateadded'] = ".$x['dateadded']."\n"; print '$sevenDays = '.$sevenDays."\n"; print 'strtotime("$year-$mon-$day") = '.strtotime("$year-$mon-$day")."\n"; print "</PRE>"; }}?>[/code] 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.