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
https://forums.phpfreaks.com/topic/127233-solved-newbie-problem-with-dates/
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!! :-)

Beware of adding months when the day is 31st of a month eg this month (Oct)

 

<?php
$t = mktime (0,0,0,date('m')+1, 31, 2008);
echo date ('d M Y', $t);                          // 01 Dec 2008   ( ie 31st Nov )
?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.