Jump to content


Photo

add days to date


  • Please log in to reply
10 replies to this topic

#1 metrostars

metrostars
  • Members
  • PipPipPip
  • Advanced Member
  • 169 posts
  • LocationNewcastle, UK

Posted 01 August 2006 - 11:34 AM

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

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 01 August 2006 - 12:01 PM

What have you tried?

Please post the code that's not working.

Ken

#3 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 01 August 2006 - 12:19 PM

$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
you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 01 August 2006 - 12:21 PM

try this:
<?php
$days = 3;
mysql_query("INSERT INTO tableName (dateColumn) VALUES (DATE_ADD(CURDATE(), INTERVAL $days DAY))");
?>
notice i'm using CURDATE(), but it also returns (obviously) the current date like NOW() would.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 01 August 2006 - 01:43 PM

<?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:
<?php
$mydate = date('Y-m-d',strtotime('today +2 days'));
?>

Ken

#6 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 01 August 2006 - 02:50 PM

is it just me, or didn't he ask to use SQL for this?
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#7 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 01 August 2006 - 03:27 PM

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


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.


#8 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 01 August 2006 - 03:28 PM

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

#9 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 01 August 2006 - 04:48 PM

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.

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.

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#10 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 02 August 2006 - 07:18 AM

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


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



you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#11 metrostars

metrostars
  • Members
  • PipPipPip
  • Advanced Member
  • 169 posts
  • LocationNewcastle, UK

Posted 02 August 2006 - 01:45 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users