sh0wtym3 Posted July 20, 2009 Share Posted July 20, 2009 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 More sharing options...
gijew Posted July 20, 2009 Share Posted July 20, 2009 Is the date formatted correctly? Link to comment https://forums.phpfreaks.com/topic/166697-update-date-field-by-adding-1-year/#findComment-879019 Share on other sites More sharing options...
sh0wtym3 Posted July 20, 2009 Author Share Posted July 20, 2009 Sorry, it IS updating, but it is updating the day? The date column in mysql is formatted YYYY-MM-DD And as far as I know you can't change that format... How can I get it to change the year instead of the day? Link to comment https://forums.phpfreaks.com/topic/166697-update-date-field-by-adding-1-year/#findComment-879081 Share on other sites More sharing options...
ldougherty Posted July 20, 2009 Share Posted July 20, 2009 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 Link to comment https://forums.phpfreaks.com/topic/166697-update-date-field-by-adding-1-year/#findComment-879096 Share on other sites More sharing options...
The Little Guy Posted July 20, 2009 Share Posted July 20, 2009 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'"; ? Link to comment https://forums.phpfreaks.com/topic/166697-update-date-field-by-adding-1-year/#findComment-879104 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.