Reputation: 49817
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
Upvotes: 3
Views: 735
Reputation: 4560
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"));
Upvotes: 0
Reputation: 4461
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.
Upvotes: 2
Reputation: 12581
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.
Upvotes: 1