#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Posts
    35
    Rep Power
    18

    date addition problem


    Hello,

    I have a field in date-format in my mysql db and I'd like to query the values and add a specific number to the date, so that I would have two results when echoed with php.

    This is what I got so far:


    I query the date (format: 0000-00-00) from my db and write it into the var $result (date is also the fieldname):

    $result = mysql_query("SELECT date,addition FROM testtable");

    I do a while loop with the dates showing up and cut the date into stringparts and I want to add the day-part of the result with another field which I call addition here (format: integer), so that I actually make another date using the mkdate-function.

    So I do this:

    PHP Code:
    <?php

    // mysql query
    $result mysql_query("SELECT date,addition FROM testtable"); 

    while(
    $row mysql_fetch_object($result)) {
             
    $date $row->date;
             
    $date strval($date);
             
    $datepart1 substr("$date"8); // this is the day
             
    $datepart2 substr("$date"52); // this is the month
             
    $datepart3 substr("$date"04); // this is the year
             
    $result_of_addition intval($datepart1)+intval($row->addition);
             
    $second_date date ("j.n.Y"mktime(0,0,0,intval($result_of_addition),intval($datepart2),intval($datepart3)));

            echo 
    $datepart1.".".$datepart2.".".$datepart3." - ".$second_date
    }
    ?>
    Let's say the results I get from my db are the following:

    the first result of field "date" has the value of = 2003-01-21 and
    the first result of field "addition" has the value of 8.

    The whole thing is that I want the function to recalculate a second date that is still in the future and which depends on the first date whilst adding the field result of "addition" with the first date.

    So for example the result of the "echo"-line for above process should look like this:

    25.1.2003 - 2.2.2003

    So the function should add the "8" to the date I got from the db, however it does not seem to add it right, I get a different result and I do not know what I'm actually doing wrong =(

    Does anybody know an answer?
  2. #2
  3. No Profile Picture
    =) wannabe?
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 2002
    Location
    florida
    Posts
    2,153
    Rep Power
    19
    assuming the addition is days.
    PHP Code:


    $sec_date 
    strtotime($row->date) + 3600*24*$row->addition;

    print 
    date('Y-m-d',$sec_date); 
  4. #3
  5. coding with style
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2003
    Location
    Washington
    Posts
    1,254
    Rep Power
    63
    Admittedly, I just scanned your post, so this may not be the solution you're looking for, but oh well

    MySQL has built in date functions, so you can probably do this kind of thing in the query.

    Check this out:
    http://www.mysql.com/doc/en/Date_calculations.html

    And try asking in the MySQL section if you can't figure it out on your own.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Posts
    35
    Rep Power
    18
    bloody hell!

    and the answer was so simple

    thanks man! It works

IMN logo majestic logo threadwatch logo seochat tools logo