Jump to content

Archived

This topic is now archived and is closed to further replies.

metrostars

add days to date

Recommended Posts

Hi,

Is it possible to (using sql now feature preferably), to insert todays date plus a number of days thaht i set.

Everything i have tried has not added a month once 31 days is reached.

I have no idea how to do this. I have tried the manuals on php.net but i cannot get it to work.

tHanks

Share this post


Link to post
Share on other sites
What have you tried?

Please post the code that's not working.

Ken

Share this post


Link to post
Share on other sites
[code]
$nowdate = strtotime("now");  // creates a date string of todays date
$newdate = strtotime("+2 day", $nowdate); // adds 2 days onto that date string
$mydate = date("d-m-Y", $newdate) //creates a new date object from that string (03-08-2006)
[/code]

is one way to do it

Share this post


Link to post
Share on other sites
try this:
[code]
<?php
$days = 3;
mysql_query("INSERT INTO tableName (dateColumn) VALUES (DATE_ADD(CURDATE(), INTERVAL $days DAY))");
?>
[/code]
notice i'm using CURDATE(), but it also returns (obviously) the current date like NOW() would.

Share this post


Link to post
Share on other sites
[quote author=paul2463 link=topic=102562.msg407193#msg407193 date=1154434755]
[code]<?php
$nowdate = strtotime("now");  // creates a date string of todays date
$newdate = strtotime("+2 day", $nowdate); // adds 2 days onto that date string
$mydate = date("d-m-Y", $newdate) //creates a new date object from that string (03-08-2006)
?>[/code]

is one way to do it
[/quote]
This can be done much more simply:
[code]<?php
$mydate = date('Y-m-d',strtotime('today +2 days'));
?>[/code]

Ken

Share this post


Link to post
Share on other sites
is it just me, or didn't he ask to use SQL for this?

Share this post


Link to post
Share on other sites
[quote author=metrostars link=topic=102562.msg407176#msg407176 date=1154432061]
Hi,

Is it possible to (using sql now feature [u][b]preferably[/b][/u]), to insert todays date plus a number of days thaht i set.

Everything i have tried has not added a month once 31 days is reached.

I have no idea how to do this. I have tried the manuals on php.net but i cannot get it to work.

tHanks
[/quote]

Definition of preferably: More desirable or worthy than another; preferred:

Meaning: He would prefer it be sql, but that does not rule out the chance of it being done another way.

Share this post


Link to post
Share on other sites
The OP did say "using sql now feature preferably", but I was replying to one of the other replies, as can be seen in the quoted text.

Ken

Share this post


Link to post
Share on other sites
[quote author=hostfreak link=topic=102562.msg407296#msg407296 date=1154446035]
Definition of preferably: More desirable or worthy than another; preferred:

Meaning: He would prefer it be sql, but that does not rule out the chance of it being done another way.

[/quote]
wow... and the sarcasm runs rampant. i'm aware of what "preferably" means. it would make sense, however, for people to spend the time helping the poster come up with a solution in his "prefered" method.

ken, i apologize, your post does indeed (quite obviously, actually) answer the other response. thanks for the clarification.

Share this post


Link to post
Share on other sites
[quote]Code:
<?php
$nowdate = strtotime("now");  // creates a date string of todays date
$newdate = strtotime("+2 day", $nowdate); // adds 2 days onto that date string
$mydate = date("d-m-Y", $newdate) //creates a new date object from that string (03-08-2006)
?>
is one way to do it

This can be done much more simply:

Code:
<?php
$mydate = date('Y-m-d',strtotime('today +2 days'));
?>
Ken [/quote]

thanks for that Ken but I spilt it into three tasks to identify what each step actually did, once each step is understood then the shortened version could be used.
Paul


Share this post


Link to post
Share on other sites
Thanks to everyone who answered. All worked perfectly.

The SQL one wouldn't work when i inserted the syntax directly into the database (it just showed 0000-00-00) all the time, o i had to select it from the database on the previous page and pass the value via. URL parameter.

Thanks alot.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.