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

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 diafol
$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 diafol

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 diafol

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 diafol

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.