tags:

views:

25

answers:

4

actually I save date in database in varchar format. firstly I retrieve date from databae & store it into $date. now i want to perform addition of 5 days with $date1.

when i search on internet i got this code :

$date = date('Y-m-d', strtotime("+5 days"));

now can you tell me how I insert $date1 value to this code for get expected result.

or even if you have another method then plz tell me. thanks in advance.

+2  A: 

Working with strtotime you could to something like this:

<?php

$date = "2010-04-01";

echo date("Y-m-d", strtotime("+5 days", strtotime($date))); // 2010-04-06

The first strtotime($date) call converts the string into an timestamp integer, the secound call takes that integer and adds 5 days to it. Then date formats it back into a string


On a sidenote:

There is not really a need to save a date as a varchar since mysql (which i guess is what you are using from the tag) will also work with "DATETIME" fields for you. E.g. SELECT DATETIMEFIELD FROM table will return '2010-01-01 00:00:00' or a "DATE" field will return "2010-01-01", both are strtotime parseable :)

edorian
hey thanks dude
Pratikg88
+2  A: 

I've already added my vote to edorian's answer since that answers your question directly.

However, you can actually add the 5 days at the time you query from the database using DATE_ADD

SELECT DATE_ADD( your_date_column, INTERVAL 5 DAY ) AS newDate

On another note, why not use the DATE format for your date column rather than VARCHAR?

Brendan Bullen
+1 For mentioning [DATE_ADD](http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add).
wimvds
edorian
A: 

Or a simple +

SELECT ( your_date_column + INTERVAL 5 DAY ) AS newDate;

There is no need for something strange like DATE_ADD() when you have a + and -

Frank Heikens
A: 
echo date('Y-m-d', strtotime('+5 days', strtotime($date1)));

hope this will do ;)

viMaL