aslum
aslum

Reputation: 12254

Is there an easyway to increment a date by a regular increment?

I have datestamps in the format YYYY-mm-dd HH:mm:ss Is there an easy way to increase or decrease this by a set amount (say 1 hour or 1 day)?

Upvotes: 0

Views: 95

Answers (3)

genesis
genesis

Reputation: 50976

$plus_day = date('Y-m-d H:i:s', strotime($your_time . " + 1 day"));
$plus_hour = date('Y-m-d H:i:s', strotime($your_time . " + 1 hour"));

Upvotes: 6

salathe
salathe

Reputation: 51950

Another option is to work with the DateTime class, which has a variety of ways of manipulating the date/time.

$incremented = date_create($date)->modify('+1 day')->format('Y-m-d H:i:s');

// Same as above
// $incremented = date_create($date . ' +1 day')->format('Y-m-d H:i:s');
// $incremented = date_create($date)->add(new DateInterval('P1D'))->format('Y-m-d H:i:s');
// $date = new DateTime($date); $date->modify('+1 day'); $incremented = $date->format('Y-m-d H:i:s');

Upvotes: 4

Adam Hopkinson
Adam Hopkinson

Reputation: 28795

See MySQL DATE_ADD function:

SELECT DATE_ADD(date, INTERVAL 1 HOUR) FROM tablename

Update See @genesis' answer for the PHP way. Which you choose is up to whether you want to do the interval in mysql or php (I suspect PHP is the best option)

Upvotes: 2

Related Questions