Forum Moderators: coopster

Message Too Old, No Replies

And still another issue on dates

Adding + number of days

         

Francis

9:23 am on Jul 26, 2005 (gmt 0)

10+ Year Member



Please help.

I can't seem to find how in the world can I add a certain number of days from a date field in the database.

For example, I have 07-27-2005 on a database field, and I want to add 45 days more and save it into another field.

Thanks. Sorry again for the trouble.

omoutop

9:40 am on Jul 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



why dont u save the (whole) new date on the other field?
...just a thought...

hughie

9:50 am on Jul 26, 2005 (gmt 0)

10+ Year Member



The simple way to do it is to call the database and get it to spit out a unix timestamp

$result=mysql_query("SELECT *, UNIX_TIMESTAMP(Date_Field) As UnixDate FROM mytable ");
$row=mysql_fetch_array($result);
$olddate=$row[UnixDate];
$newdate=$olddate+((60*60)*24)*45) // i.e. add total number of seconds in 45 days

then reinsert it into the dbase

$newdate=date('Y-m-d',$newdate); // converts unixdate back to mysql format
$sql=mysql_query("UPDATE mytable SET Date_Field='$newdate' ");

hope that helps,
hughie

dcrombie

10:24 am on Jul 26, 2005 (gmt 0)



Using postgres you can:

UPDATE table SET newfield = oldfield + INTERVAL '45 days';

I think there's a similar syntax in MySQL...

;)

hughie

10:58 am on Jul 26, 2005 (gmt 0)

10+ Year Member



hmm, maybe mine isn't so simple ;-)

nice solution that!

hughie

dhardisty

12:16 pm on Jul 26, 2005 (gmt 0)

10+ Year Member



yes, MySQL is
UPDATE table SET newfield = ADDDATE(oldfield, INTERVAL 45 day);

best,
Dave

Francis

8:49 am on Aug 1, 2005 (gmt 0)

10+ Year Member



Thanks all, for the reply.

I was able to do this to generate the necessary display on the page:

$today = strtotime("now");
$month = date('m',$today);
$day = date('d',$today);
$year = date('y',$today);
$date_today = date('m/d/y',$today);
$expire_date = mktime(0, 0, 0, $month, $day + 45, $year);

Seems like a nice thought and more importantly, it's working for displaying the date on the page. But my problem now is in the updating of the database record.

If I do:

$query = "INSERT INTO $table_to_use (field1, field2, date_applied, expire_date, status) VALUES ('$var1', '$var2', '$date_today', '$expire_date', '$status')";

Assuming that the date today is Aug. 1, 2005, what gets written on the field is:

-- for date_applied, it's 2008-01-05
-- for expire_date, it's 0000-00-00.

Why is this so?

Thanks again for the help.

Francis

6:56 am on Aug 2, 2005 (gmt 0)

10+ Year Member



any suggestions please?

thanks.

dreamcatcher

8:00 am on Aug 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use strtotime instead:

$expire_date = date("Y-m-d", strtotime("+45 days));

dc