Jump to content

Update DATE field by adding 1 year


sh0wtym3

Recommended Posts

I'm working on a script that will add 1 year to the date, but it doesn't seem to work and I can't figure out why. Here is my code:

 

<?php
$conn = mysql_connect("localhost","username","password");
mysql_select_db("database");

$id = $_GET['id'];

$sql = "UPDATE table SET year = date_add(mydate, INTERVAL 1 YEAR) WHERE id = '$id'";
$res = mysql_query($sql, $conn);
?>

 

"mydate" is the name of the column with the dates, and the field type for that column is set to date.

 

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/166697-update-date-field-by-adding-1-year/
Share on other sites

I just ran your almost exact code on my database via phpmyadmin and it worked properly

 

UPDATE counter SET datestamp = date_add(datestamp, INTERVAL 1 YEAR) WHERE id = '5';

 

counter is the table

datestamp is the name of the column which is type date

id is the unique identifier

 

When I ran this it updated my field from

 

2008-04-19

 

to

 

2009-04-19

Instead of "year = ..." Like this:

$sql = "UPDATE table SET year = date_add(mydate, INTERVAL 1 YEAR) WHERE id = '$id'";

 

Don't you want to update mydate like this:

$sql = "UPDATE table SET mydate = date_add(mydate, INTERVAL 1 YEAR) WHERE id = '$id'";

 

?

Archived

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

×
×
  • Create New...

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.