Results 1 to 9 of 9

Thread: Changing Dates to Integer Values

  1. #1
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default Changing Dates to Integer Values

    I have some date fields in a table with values in the default format (eg 2008-07-08) that I need to convert to an integer format like 20080708 to import into another table.

    I suspect this is fairly simple but what I have tried has not worked. Can anyone please help clue me in? Thanks.

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    I hope JS does'nt conflict much the data on your DB.
    Code:
    <script type="text/javascript">
    window.onload=function()
    {
    var t='2007-07-08';
    var pro=t.split('-');
    for(var i=0;i<pro.length;i++)
    alert(pro[i]);
    }
    </script>
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. The Following User Says Thank You to rangana For This Useful Post:

    kuau (07-11-2008)

  4. #3
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    You might also find its PHP counterpart useful:
    PHP Code:
    <?php 
    $t
    ='2007-08-08';
    $pro=split('-',$t);
    for(
    $i=0;$i<sizeof($pro);$i++)
    echo 
    '- '.$pro[$i].'<br>';
    ?>
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  5. The Following User Says Thank You to rangana For This Useful Post:

    kuau (07-11-2008)

  6. #4
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Dear Rangana: Thanks for the js but this is not for displaying the date. This is for changing the data in the entire table. I need to actually change the data permanently, not just when a window loads. It will be a SQL command something like:

    UPDATE table SET `StartDate2` = date to integer (`StartDate`);

    The yellow part is what I need. Do I set StartDate2 to INT to start? I thought someone might know off the top of their head without my having to slog through the manual. Thanks!

  7. #5
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Dear Rangana:

    Can you please explain what the php is doing line by line? I really want to understand php. Thanks.

  8. #6
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Has the PHP solved your issue?
    Actually, PHP was'nt my forte, but I thought I know how to do the counterpart in PHP.
    Here's an explanation:
    PHP Code:
    <?php 
    $t
    ='2007-08-08'// Stores 2007-2008-08 to variable 't'
    $pro=split('-',$t);
    // Uses the split function having '-' as our patter to search for.
    //Split function returns an array, so we need to loop through it.
    for($i=0;$i<sizeof($pro);$i++)
    // Starts the loop. Conditional statement;increments the loop
    echo '- '.$pro[$i].'<br>';
    // Since $pro holds the array values of split, we can call each data via its index.
    // pro[0],pro[1],pro[2]....and so on
    // We place $i in replacement of the fixed index.
    ?>
    Hope that makes sense and does'nt confuse you that much.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  9. #7
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Thanks for explaining. I would have to write a bigger script than that to address the table. I'm hoping there is an easier way using SQL directly on the table.

    In the php I don't understand the echo '-' ... wouldn't that put a dash in front of the date? Thanks.

  10. #8
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Yes it would. I did it for presentation. You can change the echo part to:
    PHP Code:
    echo $pro[$i]; 
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  11. The Following User Says Thank You to rangana For This Useful Post:

    kuau (07-11-2008)

  12. #9
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Dear Rangana: I ended up importing the entire table to MS Access and using the OS date format to get the dates in the format I needed then changing the dataype to TEXT. I'm hoping to export it back to MySQL and then changing it to INT. No doubt not the best solution, but if all else fails, go with what you know, as they say.

    Anyway, I learned some php from you in the process, so thank you! e

Bookmarks

Posting Permissions

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