Jump to content

Help adding a day to a specific date


honus

Recommended Posts

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???";

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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???";

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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.  :shrug:

 

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. 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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???";

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.