Results 1 to 5 of 5

Thread: Change date in field

  1. #1
    Join Date
    Jan 2008
    Posts
    21

    Default Change date in field

    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.

  2. #2
    Join Date
    Dec 2006
    Location
    Wisconsin
    Posts
    150

    Default

    Code:
    <?php
    echo date('Y')+1;
    echo date('\-m\-d');
    ?>
    Last edited by AfroJoJo; 02-21-2008 at 10:19 PM.

  3. #3
    Join Date
    Jan 2008
    Posts
    21

    Default

    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,

    echo date('Y',$mydate)+1;
    echo date('\-m\-d',$mydate);

    It returns 1970-12-31 every time where my variable equals 2007-1-2.

    Thanks
    Last edited by Bassman; 02-22-2008 at 06:11 AM.

  4. #4
    Join Date
    Nov 2006
    Location
    Sydney, Australia
    Posts
    4,951

    Default

    $d = date('d',$mydate);
    $m = date('m',$mydate);
    $y = date('Y',$mydate);
    echo date('Y-m-d',mktime(0,0,0,$m,$d,$y+1));

  5. #5
    Join Date
    Feb 2006
    Location
    Florida, USA
    Posts
    1,505

    Default

    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
    PHP Code:
    echo date("Y-m-d",$mydate+31557600); 
    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..
    PHP Code:
    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.
    PHP Code:
    $d explode("-",date("n-j-Y",$mydate));
    echo 
    date("Y-m-d",mktime(0,0,0,$d[0],$d[1],$d[2]+1)); 
    This is the same as doing this..
    PHP Code:
    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.
    Last edited by Basil; 02-22-2008 at 09:49 AM.
    If at first you don't succeed, try reading the instructions.
    semlar.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •