Log in

View Full Version : Changing Dates to Integer Values



kuau
07-11-2008, 05:17 AM
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.

rangana
07-11-2008, 05:35 AM
I hope JS does'nt conflict much the data on your DB.


<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>

rangana
07-11-2008, 05:43 AM
You might also find its PHP counterpart useful:


<?php
$t='2007-08-08';
$pro=split('-',$t);
for($i=0;$i<sizeof($pro);$i++)
echo '- '.$pro[$i].'<br>';
?>

kuau
07-11-2008, 05:44 AM
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!

kuau
07-11-2008, 05:58 AM
Dear Rangana:

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

rangana
07-11-2008, 06:08 AM
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
$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.

kuau
07-11-2008, 06:20 AM
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.

rangana
07-11-2008, 06:30 AM
Yes it would. I did it for presentation. You can change the echo part to:


echo $pro[$i];

kuau
07-11-2008, 10:45 PM
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