Link to home
Start Free TrialLog in
Avatar of Richard Korts
Richard KortsFlag for United States of America

asked on

php date add

One of those days when EVERYTHING goes wrong.

What is wrong with this:
<?php
$today = date('Y-m-d');
// 30 days out
$exp = date_add($today, date_interval_create_from_date_string('30 days'));
echo $exp;
?>

Open in new window


I get Warning: date_add() expects parameter 1 to be DateTime, string given in /home/backflow/public_html/date_add_test.php on line 4

What does it want, that IS date / time?
Avatar of Donna
Donna
Flag of United States of America image

Yes, you need to read the manual. date_add() Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object, and returns another DateTime object. So param1, which is $today, should be a DateTime object, but it's a String.  (and it's returned as a String on line 2)

http://php.net/manual/en/function.date-add.php
http://php.net/manual/en/datetime.add.php
If you want to create a DateTIme object from the date (is that even what you want?) you can try this:

http://php.net/manual/en/datetime.construct.php 

DateTime date_create ([ string $time = "now" [, DateTimeZone $timezone = NULL ]] )

             >  (Returns new DateTime object.)

What is it you are trying to do?
Avatar of Brian Tao
The date() function returns a date/time formatted string.  The date_add() function requires the first parameter to be a DateTime object rather than a date/time formatted string.
Use the following instead:
// procedural style
$today = date_create();
// or OO style
$today = new DateTime();

Open in new window

Avatar of Richard Korts

ASKER

seems to me the "old fashioned" way is much easier.

1. Use mktime for time now.
2. multiply # of days by 86400.
3. add those seconds to time now
4. convert to date.
SOLUTION
Avatar of Donna
Donna
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I used this:

$tnow = mktime (date("H"), date("i"), date("s"), date("n"), date("j"), date("Y"));
// 30 days out
$addsecs = 30 * 86400;
$t30 = $tnow + $addsecs;

Works perfect.

Seems like translating 30 days to seconds should work regardless of DST or Feb, 28 or 29 days, etc.

I used this in 2006 on a site, it still works.
I used this in 2006 on a site, it still works.
You've been fortunate!  It's only going to be the wrong calculation when the script runs at a time that sends its reach across the DST change.  To my mind, that is one of the things that I know  can go wrong, and I don't want a call from an irate client at 2:00am when the time resets.  For me, it's just easier to "do it right."