Jump to content

Recommended Posts

currently, i 'm working with a date and intend to add a specific month to the date, so that the date that is added with the month can be generated...

 

e.g. 2008-12-27 add 1 month then

the generated date which i needed is 2009-1-27

 

after searching online, i found strtotime(), but i'm quite confuse with it and unable to get the answer i want...

 

so really hope that sifu here can give me a hand, thx!

Link to comment
https://forums.phpfreaks.com/topic/138538-add-month-to-a-date/
Share on other sites

Here's a quick example:

<?php
echo date('Y-m-d',strtotime('2008-12-27 +1 month'));
?>

 

Ken

 

yes, thx! it works. but, how about if my date and month to add is in variable, how to apply it?

 

$mydate=2008-12-27;

$addmth=1;

 

i tried, but the answer is wrong, year 1970 is generated, month and day are wrong too...

Link to comment
https://forums.phpfreaks.com/topic/138538-add-month-to-a-date/#findComment-724350
Share on other sites

<?php

echo date('Y-m-d',strtotime('2008-12-27 +1 month'));

?>

 

this should be what you'd probably want

 

<?php

echo date('Y-m-d',strtotime("{$mydate} +{$addmth} month"));

?>

 

i try this

<?php

$jdate=$_SESSION['StartDate'];

$m=$_SESSION['Duration'];

 

echo date('Y-m-d',strtotime($jdate.'+'.$m .'month'));

?>

 

and currently it works, what do you think? will it create problem?

Link to comment
https://forums.phpfreaks.com/topic/138538-add-month-to-a-date/#findComment-724417
Share on other sites

eh, idk lol, just use this to be on the safe side:

 

<?php
$jdate=$_SESSION['StartDate'];
$m=$_SESSION['Duration'];

echo date('Y-m-d',strtotime("{$jdate} +{$m} month"));
?>

 

but they're both the same basically, except yours is missing plenty whitespace lol and idk if eventually that will bose a problem so..

Link to comment
https://forums.phpfreaks.com/topic/138538-add-month-to-a-date/#findComment-724418
Share on other sites

eh, idk lol, just use this to be on the safe side:

 

<?php
$jdate=$_SESSION['StartDate'];
$m=$_SESSION['Duration'];

echo date('Y-m-d',strtotime("{$jdate} +{$m} month"));
?>

 

but they're both the same basically, except yours is missing plenty whitespace lol and idk if eventually that will bose a problem so..

 

ok, thx! i'll take your advice! :)

Link to comment
https://forums.phpfreaks.com/topic/138538-add-month-to-a-date/#findComment-724421
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.