Dear All,
I have a date stored in a MySQL database I want to manipulate.
How do I add a year onto a date retrieved from a MySQL database date variable? So if I had 24/04/2004 in the database, how do I add a year to get 24/04/2005?
Or, for example, reduce the date by 28 days, which for the 24/04/2004 example would give 28/03/2004.
Any ideas?
Many thanks
Mark
Date manipulation
Moderator: General Moderators
You can convert the mysql date field to a unix timestamp using mktime(). From there it's pretty easy to manipulate days/months/years. Once you've manipulated the timestamp you can convert the timestamp back into a date and insert the new value into the database.
For example:
let me know if you need clearification on what I just wrote.
For example:
Code: Select all
// $mysql_date is the date field that you've pulled out of the mysql database
// this splits up $mysql_date so that the year, month and day are each an element in the array $date
$date = explode("/",$mysql_date);
// this converts $mysql_date to a unix timestamp
$timestamp = mktime(0,0,0,$date[0],$date[1],$date[2]);
// this adds one year to the timestamp
$timestamp += 60*60*24*365;
// this converts the timestamp back into a normal date in the form day/month/year
$new_date = date("d/m/Y",$timestamp);