i've been trying to find out how to calculate for expiry date in php. basically i've 2 date signup_date and expiry_date. user have to key in singup_date but for expiry_date i wanted to come out calculating by 3month, 6month, 1year and 2year etc. i dunno how to calculate for those using php. i found out this one but that was not working.
something like this $years = 1; $months = 6; $days = ($years * 365) + ($months * 30); // or $days = ($years * 365) + (365/$months); $expiry_date= time() + ( $days * 24 /* hours */ * 60 /* minutes */ * 60 /* seconds */ ); PHP:
Rather use date and mktime (see below) with php lesser than 5.3 With php 5.3 and up you can use "Datetime" and "Dateinterval" // Example for php < 5.3 echo "Today: " .date('Y-m-d') ."\n"; $nMonths = 2; echo "2 Month ahead: " .date('Y-m-d', mktime( 0, 0, 0, date('m',strtotime(date('Y-m-d')))+$nMonths ) ) . "\n"; $nYears = 2; echo "2 Years ahead: " .date('Y-m-d', mktime( 0, 0, 0, date('m',strtotime(date('Y-m-d'))), date('d',strtotime(date('Y-m-d'))), date('Y',strtotime(date('Y-m-d')))+$nYears ) ) . "\n"; PHP: A Ready to use function for php < 5.3 from: http://de3.php.net/manual/en/function.date-add.php#95574 function add_date($givendate,$day=0,$mth=0,$yr=0) { $cd = strtotime($givendate); $newdate = date('Y-m-d h:i:s', mktime(date('h',$cd), date('i',$cd), date('s',$cd), date('m',$cd)+$mth, date('d',$cd)+$day, date('Y',$cd)+$yr)); return $newdate; } // 2 month echo dateAdd(date('Y-m-d'), 0, 2, 0); // 2 years echo dateAdd(date('Y-m-d'), 0, 0, 2); PHP: