Richard Korts
asked on
php date add
One of those days when EVERYTHING goes wrong.
What is wrong with this:
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?
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;
?>
I get Warning: date_add() expects parameter 1 to be DateTime, string given in /home/backflow/public_html
What does it want, that IS date / time?
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?
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?
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:
Use the following instead:
// procedural style
$today = date_create();
// or OO style
$today = new DateTime();
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.
1. Use mktime for time now.
2. multiply # of days by 86400.
3. add those seconds to time now
4. convert to date.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
$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."
http://php.net/manual/en/function.date-add.php
http://php.net/manual/en/datetime.add.php