tags:

views:

121

answers:

5

I've rarely touched PHP date functions,

and now I need to do the follows:

  1. get current date,
  2. get date of three days later
  3. get date of three weeks later
  4. get date of three month later
  5. get date of three years later

and finally to implement such a function:

function dage_generate($number,$unit)
{

}

$unit can be day/week/month/years

A: 

http://us.php.net/manual/en/function.date.php

Note towards the bottom of the page:

Example #3 date() and mktime() example

<?php
$tomorrow  = mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"));
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"),   date("Y"));
$nextyear  = mktime(0, 0, 0, date("m"),   date("d"),   date("Y")+1);
?>
RC
+4  A: 

http://uk.php.net/strtotime can do most of that:

  1. strtotime("today")
  2. strtotime("+ 3 days")
  3. strtotime("+ 3 weeks")
  4. strtotime("+ 3 months")
  5. strtotime("+ 3 years")

The function would be something like:

function dage_generate($number,$unit)
{
  return strtotime("+ ".$number." ".$unit);
}
Matt Lacey
It output pure numbers,but I need it in the format xxxx-xx-xx
Shore
Matt Lacey
Shore: Then use the Date() function to convert those 'pure numbers' into a readable format.
Duroth
date("Y-m-d",strtotime("+ 0 days")) is what I need finally.
Shore
Looks to me like strtotime("+ 0 days") might create some needless overhead. If you want the current date, you can simply leave out the second variable in the date() function.
Duroth
A: 

Using strtotime() you can do this easily.

$now = time();
$threeDays = strtotime("+3 days");
$threeWeeks = strtotime("+3 weeks");
$threeMonths = strtotime("+3 months");
$threeYears = strtotime("+3 years");

Each of those variables will be an integer which represents the unix timestamp at that point in time. You can then format it into a human readable string using date().

echo date('r', $threeWeeks);
// etc...
nickf
A: 

Use date_create() to create a DateTime object, then use the add method.

See the examples on the page for the add() method, they contain all you need.

Joey