metrostars Posted August 1, 2006 Share Posted August 1, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/16197-add-days-to-date/ Share on other sites More sharing options...
kenrbnsn Posted August 1, 2006 Share Posted August 1, 2006 What have you tried?Please post the code that's not working.Ken Quote Link to comment https://forums.phpfreaks.com/topic/16197-add-days-to-date/#findComment-66988 Share on other sites More sharing options...
paul2463 Posted August 1, 2006 Share Posted August 1, 2006 [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 Quote Link to comment https://forums.phpfreaks.com/topic/16197-add-days-to-date/#findComment-66994 Share on other sites More sharing options...
obsidian Posted August 1, 2006 Share Posted August 1, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/16197-add-days-to-date/#findComment-66995 Share on other sites More sharing options...
kenrbnsn Posted August 1, 2006 Share Posted August 1, 2006 [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 Quote Link to comment https://forums.phpfreaks.com/topic/16197-add-days-to-date/#findComment-67023 Share on other sites More sharing options...
obsidian Posted August 1, 2006 Share Posted August 1, 2006 is it just me, or didn't he ask to use SQL for this? Quote Link to comment https://forums.phpfreaks.com/topic/16197-add-days-to-date/#findComment-67063 Share on other sites More sharing options...
hostfreak Posted August 1, 2006 Share Posted August 1, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/16197-add-days-to-date/#findComment-67087 Share on other sites More sharing options...
kenrbnsn Posted August 1, 2006 Share Posted August 1, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/16197-add-days-to-date/#findComment-67088 Share on other sites More sharing options...
obsidian Posted August 1, 2006 Share Posted August 1, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/16197-add-days-to-date/#findComment-67154 Share on other sites More sharing options...
paul2463 Posted August 2, 2006 Share Posted August 2, 2006 [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 itThis 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 Quote Link to comment https://forums.phpfreaks.com/topic/16197-add-days-to-date/#findComment-67641 Share on other sites More sharing options...
metrostars Posted August 2, 2006 Author Share Posted August 2, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/16197-add-days-to-date/#findComment-67789 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.