Solved

php date add

Posted on 2016-08-30
9
75 Views
Last Modified: 2016-08-31
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?
0
Comment
Question by:Richard Korts
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 3

Expert Comment

by:Donna
ID: 41777368
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
0
 
LVL 3

Expert Comment

by:Donna
ID: 41777370
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?
0
 
LVL 9

Expert Comment

by:Brian Tao
ID: 41777371
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

0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:Richard Korts
ID: 41777372
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
 
LVL 3

Assisted Solution

by:Donna
Donna earned 125 total points
ID: 41777376
if you omit timezone, it used the timezone on the server, yes.

You can try : echo $exp = date('Y-m-d', strtotime("+30 days"));

and you can use an if statement and a short variable ($i) to switch from 30 or 31 days, if you are looking for same day next month...
0
 
LVL 53

Accepted Solution

by:
Scott Fell,  EE MVE earned 250 total points
ID: 41777390
$thirtyDays = date('Y-m-d', strtotime("+30 days"));
echo $thirtyDays;

Open in new window

0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 total points
ID: 41777394
seems to me the "old fashioned" way is much easier

"Easy" is relative and exists in a context.  I would hate to endure the complications of procedural code when I'm working with OO designs.  Date computations are like email - it seems like it ought to be easy, until you try to actually do it (right, Secretary Clinton?)

Assuming you can do date/time calculations yourself?  Don't do that.  Instead use the built-in PHP functions.  Your own computations, if you try to write them, will get incredibly complicated when you realize that twice a year (but only in some locations and not others), a day does not have 24 hours because of daylight-savings time.  Don't go there.  Instead follow the guidance that we already have written here at E-E.

These two articles show many of the same computations in parallel construction.  One gives the old procedural way; the other gives the modern object-oriented way.  You will find date addition and many other useful references in here.  Maybe bookmark them for future reference.

Old:
https://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL-Procedural-Version.html
$future = date('c', strtotime('TODAY + 30 DAYS'));

Open in new window


New:
https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html
$alpha = new DateTime('TODAY');
$month = $alpha->add(new DateInterval("P30D"));
echo $month->format('c');

Open in new window

0
 

Author Comment

by:Richard Korts
ID: 41778916
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.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41778928
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."
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question