0

Possible Duplicate:
adding one day to a date

I'm trying to add a day to a value pulled from a mysql row. so the value getting returned is let's say

2012-10-22 22:12:13

and I want to make it

2012-11-22 22:12:13

and store it in the variable without having to interval it back into mysql and then pull it right back out.

i tried doing

$end_date_add = $enddate + 0000 . "-" . 00 . "-" . 01 . " " . 00 . ":" . 00 . ":" . 00;

with $end_date being the time logged, but it replaces the time with zeros. am I going about this wrong? Any help much appreciated, thank you.

Community
  • 1
  • 1
VVV
  • 417
  • 2
  • 6
  • 10

4 Answers4

1

This is what you want, i guess...

$date_old = strtotime("+1 MONTH", strtotime("2012-10-22 22:12:13"));

echo date("Y-m-d H:i:s", $date_old);
sephoy08
  • 1,104
  • 7
  • 16
1

You can make use of strtotime to add the one month period:

$date   = '2012-10-22 22:12:13';
$format = 'Y-m-d H:i:s';
echo date($format, strtotime("$date +1 MONTH"));

Output (Demo):

2012-11-22 22:12:13

You can also make use of PHP's DateTime type and add a DateInterval of one day:

$date   = '2012-10-22 22:12:13';
$format = 'Y-m-d H:i:s';
echo (new DateTime($date))->add(new DateInterval('P1M'))->format($format);

Output (Demo):

2012-11-22 22:12:13

The code above is PHP 5.4, in PHP 5.3 you can do:

echo date_add(new DateTime($date), new DateInterval('P1M'))->format($format);
hakre
  • 193,403
  • 52
  • 435
  • 836
0

Date adding

$date = date("Y-m-d"); // current date
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");
Dino Babu
  • 5,814
  • 3
  • 24
  • 33
  • That is the problem, I don't want to get the date now, I want to use the date I have on file, in it's string format already and add 1 day, or 24 hours specifically. – VVV Oct 25 '12 at 05:36
  • So set a different input, it's just an example – Scuzzy Oct 25 '12 at 05:40
0

This could also be done as part of the MYSQL query

SELECT DATE_ADD(datecol, INTERVAL 1 DAY) AS 'datecol_plus_one'
Scuzzy
  • 12,186
  • 1
  • 46
  • 46