Jump to content

[SOLVED] Newbie problem with dates


laqutus

Recommended Posts

Im new to PHP and programming as a whole, I need some help with this problem with dates, I need to increment the date by 2 months so thats there is an exp[iry date 2 months from current date. However for some reason I can't get this to work and my code has turned into a right mess. It was working but now we are in the 10th month and it now sets the expiry date to 02 and not 12.........There must be a better way to do this?????? Please help.......

 

Existing code:

 

//Set date

$add= date('Y/m/d');

 

// Break apart date

$nadd = explode("/", $add);

 

// Trim the integer if it is less than 10

  if ($nadd[1] < 10){ $nads = ltrim($nadd[1], '0'); }

  $nadd[1] = $nads + $run;

 

// Check if value after calculation is still below 10 if it is then add back the leading zero

  if ($nadd[1] < 10){ $nadd[1] = 0 . $nadd[1]; }

 

// Set all the segments of the array

$newad = array($nadd[0] , $nadd[1] , $nadd[2]);

 

// Join the date string back together

$dies = implode("/", $newad);

 

 

I know this code is poor, but I dont know any better at present, any help would be useful.

 

Thanks in advance

Dan

Link to comment
Share on other sites

you can use mktime() which will create a unix timestamp and add 2 months to the months.

<?php
$newdate=mktime(0, 0, 0, date('m')+2, date('d'), date('Y'));
echo date('Y/m/d', $newdate);

results: 2008/10/06

 

Thank you so much, that sure is efficient code, christ mine was terrible....

Thanks again!! :-)

Link to comment
Share on other sites

Use strtotime(). It is designed for this.

 

<?php
$olddate = date('Y-m-d'); // input date
$newdate = date('Y-m-d',strtotime("$olddate +2 months")); // output date
?>

 

Thanks also a very good version, works great.........Thank you!!

Link to comment
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.