faqts : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

25 of 48 people (52%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

increment in date variable

Feb 29th, 2008 09:46
dman, Edwin Chan, zahid bilal, http://www.ttnr.org


Let's say you want to increment 3 years, 2 months and decrement 1 day 
to the current date variable:
<?php
// set the date variable to NOW
$date=mktime();
// Check the current date
echo "Date is now: ".date("d-M-Y",$date)."<BR>";
// add 3 years, 2 months and subtract 1 day
$years_to_add =3;
$months_to_add=2;
$days_to_add  =-1;
$date=mktime(0,0,0,
             date("m",$date)+$months_to_add,
             date("d",$date)+$days_to_add,
             date("Y",$date)+$years_to_add
            );
// Check the current date again
echo "Date is now: ".date("d-M-Y",$date);
?>