Page 1 of 2

Get Date, add 7 days, convert date to m:dd:yy - how?

Posted: Mon May 23, 2011 11:16 am
by simonmlewis

Code: Select all

$now = new DateTime;
$now->modify('+7days');
$datesub = strtotime($now);
I really thought this would work.
Using the new Datetime function, then adding 7 days, then converting it back to the mm:dd:yyyy style, but it fails.

What am I doing wrong here?

I need to capture today's date, add 7 days, and then insert that new date into a database, so you have a Creation Date, and a date 7 days on.

Re: Get Date, add 7 days, convert date to m:dd:yy - how?

Posted: Mon May 23, 2011 11:41 am
by emelianenko
========================================
from the manual

Code: Select all


<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d') . "\n";
?>


Re: Get Date, add 7 days, convert date to m:dd:yy - how?

Posted: Mon May 23, 2011 12:05 pm
by AbraCadaver
Or just:

Code: Select all

echo date('Y-m-d', strtotime('+7 days'));

Re: Get Date, add 7 days, convert date to m:dd:yy - how?

Posted: Mon May 23, 2011 12:32 pm
by simonmlewis
What is "P10D"?
And how do I store the final date into a variable?
Looks like AbraCadaver's method is a simpler one, but again, how do I store that into the variable?

Re: Get Date, add 7 days, convert date to m:dd:yy - how?

Posted: Mon May 23, 2011 1:09 pm
by AbraCadaver
simonmlewis wrote:What is "P10D"?
And how do I store the final date into a variable?
Looks like AbraCadaver's method is a simpler one, but again, how do I store that into the variable?
What? 751 posts and you haven't asked how to store something in a variable until now?

Code: Select all

$variable = date('Y-m-d', strtotime('+7 days'));

Re: Get Date, add 7 days, convert date to m:dd:yy - how?

Posted: Mon May 23, 2011 1:18 pm
by simonmlewis
I know how to, but couldn't quite see what u were doing.
Your query literally converts the date today into a variable, with 7 days added?
So if I wanted to add a year, I just alter it to +365 days?

Re: Get Date, add 7 days, convert date to m:dd:yy - how?

Posted: Mon May 23, 2011 1:20 pm
by AbraCadaver
I would use "+1 year" because it may be a leap year and the function should work that out.

Re: Get Date, add 7 days, convert date to m:dd:yy - how?

Posted: Mon May 23, 2011 1:49 pm
by simonmlewis
Excellent - got it working.
I was using the longer style before as I had learnt that - but this way is far better.
How would you use this method, with a variable stored date, rather than just today's date?

Re: Get Date, add 7 days, convert date to m:dd:yy - how?

Posted: Mon May 23, 2011 1:53 pm
by mikosiko
other option:
If you are using the calculated date just to include it in a SQL query maybe you could calculate/use it directly in the query and not in PHP; to do that just use the native mysql date function ADDDATE() with the proper INTERVAL as per example:
http://dev.mysql.com/doc/refman/5.5/en/ ... on_adddate

Re: Get Date, add 7 days, convert date to m:dd:yy - how?

Posted: Mon May 23, 2011 2:15 pm
by AbraCadaver
simonmlewis wrote:Excellent - got it working.
I was using the longer style before as I had learnt that - but this way is far better.
How would you use this method, with a variable stored date, rather than just today's date?
You would just plug-in the variable, as long as it is of a correct format for strtotime() of which there are several:

Code: Select all

$date = "2011-01-01";
$variable = date('Y-m-d', strtotime("$date +7 days"));
Or you can put the starting date in timestamp format first:

Code: Select all

$date = strtotime("2011-01-01");
$variable = date('Y-m-d', strtotime("+7 days", $date));

Re: Get Date, add 7 days, convert date to m:dd:yy - how?

Posted: Mon May 23, 2011 2:41 pm
by emelianenko
This means it adds 10 days,

All you had to do was copy and paste and see for yourself the results.


Just run the code and you can see it yourself. Copy, paste and run. That is all you have to do

My method was not complicated, but as you were showing Object oriented style, so I pleased you and did it object oriented too.

Re: Get Date, add 7 days, convert date to m:dd:yy - how?

Posted: Mon May 23, 2011 3:03 pm
by AbraCadaver
They may have been confused because they used 7 days as an example and you used 10, which 'P10D' looks odd as a representation of 7 days. :wink:

Re: Get Date, add 7 days, convert date to m:dd:yy - how?

Posted: Tue May 24, 2011 4:33 am
by emelianenko

Code: Select all

<?php
$date = new DateTime(date('Y-m-d'));
$date->add(new DateInterval('P7D'));
echo $date->format('Y-m-d') . "\n";

?>



Re: Get Date, add 7 days, convert date to m:dd:yy - how?

Posted: Mon Jun 13, 2011 8:42 am
by simonmlewis
Back to this subject of DAYS and adding/subtracting:

I need to find out if a person is under 16 years of age (purpose is to reduce the cost of an entry fee to an event).

Can you literally do $todaysdate - $row->dob (both in 1970-12-30 format), and then convert that into days and years???

If you can, how? I'm sure there is a simple way, as there was with + 7 days to a date.

Re: Get Date, add 7 days, convert date to m:dd:yy - how?

Posted: Wed Jul 27, 2011 8:56 am
by simonmlewis

Code: Select all

      $subs1 = new DateTime($row->datereg);
      $subs = new DateTime($row->datereg);
      $subs->modify('+30days');
      
      $date = strtotime("$row->datereg");   
      $add30 = date('Y-m-d', strtotime("+30 days", $date));

      $remaining = ($add30 - $row->datereg);
      
      if ($subs1 <= $subs)
      { echo "&nbsp;&nbsp;|&nbsp;&nbsp;<span style='background-color: #00cc00; padding: 2px; color: #ffffff'>TRIAL USED $remaining $add30</span>";}
      }
I'm making a real pigs-ear of this.
I need to see if they are within their trial period.
But also (and this is the bit I cannot make work), display how many days they have left.

I have converted it to a strtotime to them see if one is less than the other, but how to do work out the days between the their final day, and today? I've just noticed I don't have "today" in the calculation, but I still don't know how to convert the result into a number of day.

Thanks.