View Full Version : Change date in field
02-21-2008, 07:28 PM
I have a date field in a table that I am trying to write a script that will extact the date and change it by one year, The field would contain a date like 2008-1-2 and I want to add one year to it so that it would be 2009-1-2 .
I have tried, without any luck, using the date("Y-m-d") function to change it. Is there a simple way to change it?
Thanks in advance.
02-21-2008, 10:16 PM
02-22-2008, 06:00 AM
I got that far, but that takes todays date and adds a year to it and I want it to take a date that I have in a variable and increase it by a year? If I use,
It returns 1970-12-31 every time where my variable equals 2007-1-2.
02-22-2008, 09:35 AM
$d = date('d',$mydate);
$m = date('m',$mydate);
$y = date('Y',$mydate);
02-22-2008, 09:46 AM
I've done a little test to see what would be the fastest way to do this. The quickest, and least accurate way to do it would be to simply add a year to the date.
365.25*24*60*60 = 31557600 (~seconds in a year)
So you could just do
This isn't 100% accurate, but it uses the least amount of processing, I don't know how accurate you need it to be.
The second way you can do this (quickly and accurately) is using the strtotime function. Something like this..
echo date("Y-m-d",strtotime("+1 year",$mydate));
This is around 2.5 times slower than the first method, but should be 100% accurate.
The third method is using mktime. This is about 3 times slower than the first method.
First you get the month, day and year from your time stamp, then you plug them into mktime, adding 1 to the year.
$d = explode("-",date("n-j-Y",$mydate));
This is the same as doing this..
echo date("Y-m-d",mktime(0,0,0,date("n",$mydate),date("j",$mydate),date("Y",$mydate)+1)); Except by using an array to hold the values php only has to process one date function instead of 3 inside of mktime, this makes it roughly twice as fast.
Powered by vBulletin® Version 4.2.0 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.