charlie
charlie

Reputation: 1384

Add days to a timestamp

Im trying to add a certain amount of days to a timestmp using this in PHP:

$capturedDate = '2008-06-20'; 
$endDate = strtotime($capturedDate); 
$endDate2 = strtotime('+1 day',$endDate); 
echo $endDate2;

but its displaying: 1216526400

any ideas?

Upvotes: 2

Views: 1967

Answers (6)

lsouza
lsouza

Reputation: 2488

DateTime is a very nice way to deal with dates. You can try like this:

$capturedDate = '2008-06-20'; 
$date = DateTime::createFromFormat('Y-m-d', $capturedDate)->modify('+1 day');
echo $date->getTimestamp();

Upvotes: 0

John Conde
John Conde

Reputation: 219814

You should be using DateTime for working with dates. It's timezone friendly.

$datetime = new DateTime('2008-06-20');
$datetime->modify('+1 day');
echo $datetime->getTimestamp();

Upvotes: 1

Hydra IO
Hydra IO

Reputation: 1557

Sooooo close, just take your timestamp and convert it back into date format using date("desired format",$endDate2);

Upvotes: 0

Bad Wolf
Bad Wolf

Reputation: 8349

strtotime() converts the date into a unix timestamp which is the number of seconds since January 1st 1970. If you want a date output you have to run the finished timestamp through date() first.

$capturedDate = '2008-06-20'; 
$endDate = strtotime($capturedDate.' +1 day'); 
echo date("Y-m-d", $endDate);

Upvotes: 0

John Parker
John Parker

Reputation: 54445

strtotime creates a Unix timestamp so if you want to be presented with a formatted date, you need to pass the timestamp as an argument to the date function as follows:

$capturedDate = '2008-06-20'; 
$endDate = strtotime($capturedDate); 
$endDate2 = strtotime('+1 day',$endDate);
echo date('Y-m-d', $endDate2);

Additionally, there are a wide variety of parameters you can use in the date function if you want to display additional information.

e.g.: echo date('Y-m-d H:i:s', $endDate2); or echo date('Y-m-d h:i:s a', $endDate2);, etc.

Upvotes: 0

justderb
justderb

Reputation: 2854

Try:

echo date("Y-m-d H:i:s",$endDate2);

Or (for just the date):

echo date("Y-m-d",$endDate2);

You can find documentation about how to format your string here: http://php.net/manual/en/function.date.php

Upvotes: 2

Related Questions