3

Hi i'm trying to sum time with the current date time, this code seems to work:

$now = time();
$_day = strtotime('+1day',$now);
$_week = strtotime('+7day',$now);
$_month = strtotime('+1month',$now);
$_year = strtotime('+1year',$now);

what i'm wondering is if is there any other way , maybe better way of doing this .. can anyone show me new ways and the reasons why i should turn to another syntax? I would like to do this more accurate as possible, so if i can reflect this script it is great!

thanks

EDIT:

just to be clear i need to use unix timestamps i don't need formatted dates/datetimes

itsme
  • 48,972
  • 96
  • 224
  • 345

3 Answers3

2

If PHP version you use allows (PHP 5 >= 5.3.0) you to use DateTime and DateInterval classes, you coud use them.

example:

$dateTime=new DateTime();
$plusDay=new DateInterval('P1D');
var_dump($dateTime->add($plusDay));

$dateTime=new DateTime();
$plusWeek=new DateInterval('P1W');
var_dump($dateTime->add($plusWeek));

$dateTime=new DateTime();
$plusMonth=new DateInterval('P1M');
var_dump($dateTime->add($plusMonth));

I hope this is helpful.

vascowhite
  • 18,120
  • 9
  • 61
  • 77
Ruslan Polutsygan
  • 4,401
  • 2
  • 26
  • 37
1

I believe that strtotime() is as simple as you're going to get in PHP. All 3 of the answers to this SO question chose to use that syntax, and all of the various tutorials I see around the web use it. strtotime() makes thinking about date arithmetic a lot easier than other approaches.

Community
  • 1
  • 1
Jonah Bishop
  • 12,279
  • 6
  • 49
  • 74
0

Try this solution:

date("Y-m-d", strtotime("+1 day"));
date("Y-m-d", strtotime("+7 days"));
date("Y-m-d", strtotime("+1 month"));
date("Y-m-d", strtotime("+1 year"));
RDK
  • 4,540
  • 2
  • 20
  • 29
  • that seems returning formatted dates , not tested but i'm looking for unix timestamps – itsme Dec 25 '12 at 20:00
  • @imRcH sorry for the down vote. it happened accidentally. Could you make some changes on your answer then I can avoid this unwanted down vote. – Mo. Jun 09 '14 at 09:01