OK, Now that I know how to add minutes to a datetime field...

I need to be able to take a specific date time as in NOW...

Then add 30 days to it, and have record that date at 19:00:00 as an expiration date time

I start by capturing the current time (for display on the screen)

$current=(date('Y-m-d G:i:s'));

Then I want to display in the same format what the expiration date and time will be in 30 days at 1900 hours...

I've tried multiple variations of strtotime and date formatting, etc. but haven't had any luck

thanks in advance

Douglas

Dani AI

Generated

Nice work getting this solved, — your strtotime approach is fine for many cases. Contributors here covered the common options: showed the seconds-based trick (fast but brittle), and rightly flagged daylight‑saving caveats and suggested mktime. For a more robust, timezone-aware solution use PHP's DateTime/DateInterval so transitions and user timezones are handled predictably.

Try this pattern (replace the timezone with your application or user zone):

$tz = new DateTimeZone('America/New_York');   // use your app/user timezone
$now = new DateTimeImmutable('now', $tz);

// add 30 calendar days, then set the clock to 19:00
$expiration = $now->add(new DateInterval('P30D'))->setTime(19, 0, 0);

echo $expiration->format('Y-m-d H:i:s');

Why this is better:

  • DateInterval('P30D') adds 30 calendar days (not a fixed number of seconds), so it won't silently break across DST changes like 86400*30 can.
  • DateTime objects are timezone-aware; pick a timezone explicitly instead of relying on the server default.
  • Use DateTimeImmutable if you want to keep the original timestamp intact (useful when you already store $now elsewhere).

Notes and troubleshooting:

  • If you want "one calendar month" instead of "30 days" use 'P1M' — those are different for months with varying lengths.
  • If you already have a string timestamp, parse it with DateTime::createFromFormat (specify the correct format and timezone).
  • For storage, canonicalize to UTC before inserting into the DB and convert to the user's timezone for display. That avoids surprises with MySQL timezone conversions and daylight saving shifts.

This complements the thread: keep the simple strtotime ideas for quick scripts, but prefer DateTime/DateInterval for production code where timezones, DST, and clarity matter.

Recommended Answers

All 9 Replies

OK, Now that I know how to add minutes to a datetime field...

I need to be able to take a specific date time as in NOW...

Then add 30 days to it, and have record that date at 19:00:00 as an expiration date time

I start by capturing the current time (for display on the screen)

$current=(date('Y-m-d G:i:s'));

Then I want to display in the same format what the expiration date and time will be in 30 days at 1900 hours...

I've tried multiple variations of strtotime and date formatting, etc. but haven't had any luck

thanks in advance

Douglas

As soon as I submitted the thread, the answer popped into my head, so I had to test it out...

If there is a better way to do it, please let me know, but until then, this will do the trick.

$date = (date('Y-m-d'));
$newdate = strtotime ( '+30 day' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-d 19:00:00' , $newdate );
print "<br> newdate is : ".$newdate;

Thanks again, and if you have a better suggestion, please post it.

Douglas

Member Avatar for Member #120589
$newdate = date('Y-m-d 19:00:00, strtotime( '+30 day', date('Y-m-d')));

Don't know if that's quicker though.

I agree with Ardav. Just incase you're interested. If you ever need to add 30 days on to a time from now I just do;

$time_now = time();
$days_to_add = 30;
$seconds_to_add = 86400 * 30; // There are 86400 seconds per day

$future_timestamp = $time_now + $seconds_to_add;

To test it's working you can then do:

echo date('jS F Y H:i.s', $future_timestamp);
Member Avatar for Member #120589

If you ever need to add 30 days on to a time from now I just do;

Agree, however, the OP stated that he needs to make the time element 19:00:00

So perhaps:

$future_timestamp = date('Y-m-d 19:00:00', time() + 86400*30);

BUT
I remember a problem with this a while back - 'daylight saving' I think? Adding days in seconds messes up when clocks go back/forward. I think I ended up having to mess with mktime overflow (adding large number of days to the day item) - that seemed to work, but I'm assuming strtotime should work too - and may be easier.

$date = date('Y-m-d H:i:s',mktime(19,0,0,date('n'),date('j')+30,date('Y')));

or

$date = date('Y-m-d 19:00:00',mktime(0,0,0,date('n'),date('j')+30,date('Y')));

The benefit of mktime is that it takes daylight saving into account by using the system (or applied) timezone.

$date = strtotime(date("Y-m-d G:i:s")."+30 days");

Thank you All for your responses... I played with it a little more after my last post and came up with basically the same thing as presented above...

Here it the final result...

$current=(date('Y-m-d G:i:s'));
    
$expire_date = date('Y-m-d 19:00:00', (strtotime('+30 day', strtotime($current ))));

I already use the $current variable for other purposes in relation to the order, so used it to determine the expire date as well...

Thanks again.

Douglas

Member Avatar for Member #120589

Ok mark as solved

Ok mark as solved

Sorry about that...

I thought I had already marked it as solved.

Does the last person that posts before it gets marked as solved receive credit for solving it?

Always been curious about that.

Thanks
Douglas

Member Avatar for Member #120589

No - all contributors to a thread get the solved.
If you want to single out somebody for being especially helpful, give them an rep - or equally if you think somebody's get posted to share the spoils but has not added anything of note, give them a neg rep or a downvote. :)

However, most 'good' threads result from a decent discussion of various approaches. A final solution is often a 'team effort'. Even something as simple as asking a question could set you thinking in a different direction, thereby coming to a solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.