honus Posted March 10, 2010 Share Posted March 10, 2010 I am trying display information based on a number of days from a specific date. I need the ability to change my start date and number of days that content displays. For example, lets say my project start date is 2-15-2010 and in 7 days i want to display content, and in 14 days i want to display different content. I am still pretty lame in coding but here is what i have so far...that does not work. Please help. $start = date('Y-m-d', strtotime('2010-02-15')); $sevendaystime = date('Y-m-d', strtotime('2010-02-15' +7 days')); $fourteendaystime = date('Y-m-d', strtotime('2010-02-15' +14 days')); $twentyeightdaystime = date('Y-m-d', strtotime('2010-02-15' +28 days')); if ($start==$sevendaystime) echo "7 days in"; elseif ($start==$fourteendaystime) echo "14 days in"; elseif ($start==$twentyeightdaystime) echo "28 days in"; else echo "how many days???"; Quote Link to comment https://forums.phpfreaks.com/topic/194836-help-adding-a-day-to-a-specific-date/ Share on other sites More sharing options...
TeddyKiller Posted March 10, 2010 Share Posted March 10, 2010 You screwed up the whole script. I'm not sure if it's right or not.. but this is a tidier version. $start = date('Y-m-d', strtotime('2010-02-15')); $sevendaystime = date('Y-m-d', strtotime('2010-02-15' +7 days)); $fourteendaystime = date('Y-m-d', strtotime('2010-02-15' +14 days)); $twentyeightdaystime = date('Y-m-d', strtotime('2010-02-15' +28 days)); if ($start==$sevendaystime) echo "7 days in"; elseif ($start==$fourteendaystime) echo "14 days in"; elseif ($start==$twentyeightdaystime) echo "28 days in"; else echo "how many days???"; If that doesn't solve the problem, just try removing "days" keep the number. Although you may even need the seconds rather than days. However.. I'm not sure. Quote Link to comment https://forums.phpfreaks.com/topic/194836-help-adding-a-day-to-a-specific-date/#findComment-1024469 Share on other sites More sharing options...
honus Posted March 10, 2010 Author Share Posted March 10, 2010 I removed the days but it did not make the code work. $start = date('Y-m-d', strtotime('2010-02-15')); $sevendaystime = date('Y-m-d', strtotime('2010-02-15' +7 )); $fourteendaystime = date('Y-m-d', strtotime('2010-02-15' +14 )); $twentyeightdaystime = date('Y-m-d', strtotime('2010-02-15' +28 )); if ($start==$sevendaystime) echo "7 days in"; elseif ($start==$fourteendaystime) echo "14 days in"; elseif ($start==$twentyeightdaystime) echo "28 days in"; else echo "how many days???"; Quote Link to comment https://forums.phpfreaks.com/topic/194836-help-adding-a-day-to-a-specific-date/#findComment-1024477 Share on other sites More sharing options...
TeddyKiller Posted March 10, 2010 Share Posted March 10, 2010 when you test the code, what exactly happens? "dont work" doesn't explain much. Quote Link to comment https://forums.phpfreaks.com/topic/194836-help-adding-a-day-to-a-specific-date/#findComment-1024479 Share on other sites More sharing options...
honus Posted March 11, 2010 Author Share Posted March 11, 2010 No matter what dates i enter i get the last result: else echo "how many days???"; I changed the start date to '2010-02-24' so that echo "14 days in"; should display, but instead I get the result stating there is no match. I want the '14 days in' text to be displayed after the 14 days from the start date has hit, until the following else statement of 28 days hits, then I want the text to change to that else statement. Does that make sence? Thanks for helping on this. Quote Link to comment https://forums.phpfreaks.com/topic/194836-help-adding-a-day-to-a-specific-date/#findComment-1024496 Share on other sites More sharing options...
honus Posted March 11, 2010 Author Share Posted March 11, 2010 Can someone take a look at this. I don't even know if I am close to being on the right track here. Quote Link to comment https://forums.phpfreaks.com/topic/194836-help-adding-a-day-to-a-specific-date/#findComment-1024742 Share on other sites More sharing options...
litebearer Posted March 11, 2010 Share Posted March 11, 2010 Old man here but... seems to me if the start date is some date AND the others are some increment of that date THEN the start date will NEVER equal the incremented dates. Your IF tests are asking if they are equal therefore you are getting the correct answer. I presume you are trying to determine if the current date is 7, 14 or 28 days past the start date. Does that point you in the right direction? Quote Link to comment https://forums.phpfreaks.com/topic/194836-help-adding-a-day-to-a-specific-date/#findComment-1024746 Share on other sites More sharing options...
honus Posted March 11, 2010 Author Share Posted March 11, 2010 It does...except i am so new at PHP coding that I don't know how to do that. If you can lead me in the right direction maybe I can take it from there. I dont' know how to right the code to do that. Even with the current code, if I use a start date that is exactly 7 days difference I still don't see the correct response - i see the last statment of there being no match. Quote Link to comment https://forums.phpfreaks.com/topic/194836-help-adding-a-day-to-a-specific-date/#findComment-1024774 Share on other sites More sharing options...
litebearer Posted March 11, 2010 Share Posted March 11, 2010 the variables startdate 7date 14date 28date today The thought process if today is greater than or equal to 7date if today is less than 14date then today is between 7 and 14 days from start elseif today is less than 28date then today is between 14 and 28 days from start else today is 28 or more days from start endif else today has not reached 7 days from start yet endif Quote Link to comment https://forums.phpfreaks.com/topic/194836-help-adding-a-day-to-a-specific-date/#findComment-1024778 Share on other sites More sharing options...
honus Posted March 11, 2010 Author Share Posted March 11, 2010 I tried to write it like this...but i still get the last response. Is strtotime the correct code to use? $start = date('Y-m-d', strtotime('2010-02-24')); $sevendaystime = date('Y-m-d', strtotime('2010-02-24' +7 )); $fourteendaystime = date('Y-m-d', strtotime('2010-02-24' +14 )); $twentyeightdaystime = date('Y-m-d', strtotime('2010-02-24' +28 )); if ($start >= $sevendaystime && $start < $fourteendaystime) echo "7 days in"; elseif ($start >= $fourteendaystime && $start < $twentyeightdaystime) echo "14 days in"; elseif ($start >= $twentyeightdaystime) echo "28 days in"; else echo "how many days???"; Quote Link to comment https://forums.phpfreaks.com/topic/194836-help-adding-a-day-to-a-specific-date/#findComment-1024779 Share on other sites More sharing options...
litebearer Posted March 11, 2010 Share Posted March 11, 2010 you are still comparing the start date to the other dates. pretend. startdate = 1; 7date = 8; 14date = 15; 28date = 29 as you can see 1 will NEVER fall between ANY of the other numbers. HOWEVERl if today is some number greater than or equal to 8 then it does have the ability to fall between 8 and 29 OR worst case be greater than 29. You need ANOTHER date to use for the comparison Quote Link to comment https://forums.phpfreaks.com/topic/194836-help-adding-a-day-to-a-specific-date/#findComment-1024788 Share on other sites More sharing options...
XeNoMoRpH1030 Posted March 11, 2010 Share Posted March 11, 2010 Almost. Strtotime just converts a date/time. What you need to do is add the time you are adding (which is done by hours I believe) outside of it like below. $start = date('Y-m-d', strtotime('2010-02-24')); $sevendaystime = date('Y-m-d', strtotime('2010-02-24') + 7 * 86400); $fourteendaystime = date('Y-m-d', strtotime('2010-02-24') + 14 * 86400 ); $twentyeightdaystime = date('Y-m-d', strtotime('2010-02-24') +28 * 86400 ); So, that gets you somewhat closer. I think you don't want to use "Y-m-d", but "U" which would give you the number of seconds. That way, you are comparing integers instead of strings. Quote Link to comment https://forums.phpfreaks.com/topic/194836-help-adding-a-day-to-a-specific-date/#findComment-1024789 Share on other sites More sharing options...
honus Posted March 11, 2010 Author Share Posted March 11, 2010 Xeno - i tried adding the seconds but still did not get the correct response. litebearer - I just don't know how to code out your statement: if today is greater than or equal to 7date if today is less than 14date then today is between 7 and 14 days from start elseif today is less than 28date then today is between 14 and 28 days from start else today is 28 or more days from start endif else today has not reached 7 days from start yet endif Quote Link to comment https://forums.phpfreaks.com/topic/194836-help-adding-a-day-to-a-specific-date/#findComment-1024794 Share on other sites More sharing options...
honus Posted March 11, 2010 Author Share Posted March 11, 2010 Litebear - I tried writing your statement but my page is blank. Can you look at my code? $start = date('Y-m-d', strtotime('2010-02-24')); $today = date('Y-m-d'); $sevendaystime = date('Y-m-d', strtotime('2010-02-24') + 7 * 86400); $fourteendaystime = date('Y-m-d', strtotime('2010-02-24') + 14 * 86400 ); $twentyeightdaystime = date('Y-m-d', strtotime('2010-02-24') + 28 * 86400 ); if ($today >= $sevendaystime); if ($today < $fourteendaystime) echo "7 days in"; elseif ($today < $twentyeightdaystime) echo "14 days in"; else ($today > $twentyeightdaystime) echo "more than 28 days"; endif; else echo "under 7 days"; endif; Quote Link to comment https://forums.phpfreaks.com/topic/194836-help-adding-a-day-to-a-specific-date/#findComment-1024825 Share on other sites More sharing options...
XeNoMoRpH1030 Posted March 11, 2010 Share Posted March 11, 2010 $start = strtotime('2010-02-24'); $today = date('U'); $sevendaystime = date('U', $start + 7 * 86400); $fourteendaystime = date('U', $start + 14 * 86400 ); $twentyeightdaystime = date('U', $start + 28 * 86400 ); if ($today >= $twentyeightdaystime) { echo "More than or equal to 28 days"; } elseif ($today >= $fourteendaystime) { echo "More than or equal to 14 days"; } elseif ($today >= $sevendaystime) { echo "More than or equal to 7 days"; } else { echo "Less than 7 days"; } As I said earlier, you can't just compare 2010-02-24 to another date like 2010-03-15. They are simply strings. It's like trying to say A > B. Quote Link to comment https://forums.phpfreaks.com/topic/194836-help-adding-a-day-to-a-specific-date/#findComment-1024827 Share on other sites More sharing options...
honus Posted March 11, 2010 Author Share Posted March 11, 2010 Thanks XeNoMoRpH1030 and Litebearer - That did it. Thank you! Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/194836-help-adding-a-day-to-a-specific-date/#findComment-1024834 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.