Click here to get back home

php/mysql question? Why are the seconds missing?

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
php/mysql question? Why are the seconds missing? Geoff Cox 07-18-2008
Get Chitika Premium
Posted by Geoff Cox on July 18, 2008, 2:48 pm
Please log in for more thread options
Hello

The code below is from a php file which can obtain data from a mysql
database in Excel format.

Does any part of it explain why the time part if the Date/Time field
does not have the seconds value?

Cheers

Geoff


$link = mysql_connect ($Host, $User, $Password) or die('Could not
connect: ' . mysql_error());
mysql_select_db($DBName) or die('Could not select database');

$select = "SELECT * FROM `".$TableName."` ORDER by DateTime";
$export = mysql_query($select);
$fields = mysql_num_fields($export);

for ($i = 0; $i < $fields; $i++) {
        $csv_output .= mysql_field_name($export, $i) . "\t";
}

while($row = mysql_fetch_row($export)) {
        $line = '';
        foreach($row as $value) {
                if ((!isset($value)) OR ($value == "")) {
                        $value = "\t";
                } else {
                        $value = str_replace('"', '""', $value);
                        $value = '"' . $value . '"' . "\t";
                }
                $line .= $value;
        }
        $data .= trim($line)."\n";
}
$data = str_replace("\r","",$data);

header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=database_dump.xls");
header("Pragma: no-cache");
header("Expires: 0");
print $csv_output."\n".$data;
exit;

Posted by Paul Lautman on July 18, 2008, 3:13 pm
Please log in for more thread options
Geoff Cox wrote:
> Hello
>
> The code below is from a php file which can obtain data from a mysql
> database in Excel format.
>
> Does any part of it explain why the time part if the Date/Time field
> does not have the seconds value?
>
> Cheers
>
> Geoff
>
>
> $link = mysql_connect ($Host, $User, $Password) or die('Could not
> connect: ' . mysql_error());
> mysql_select_db($DBName) or die('Could not select database');
>
> $select = "SELECT * FROM `".$TableName."` ORDER by DateTime";
> $export = mysql_query($select);
> $fields = mysql_num_fields($export);
>
> for ($i = 0; $i < $fields; $i++) {
> $csv_output .= mysql_field_name($export, $i) . "\t";
> }
>
> while($row = mysql_fetch_row($export)) {
> $line = '';
> foreach($row as $value) {
> if ((!isset($value)) OR ($value == "")) {
> $value = "\t";
> } else {
> $value = str_replace('"', '""', $value);
> $value = '"' . $value . '"' . "\t";
> }
> $line .= $value;
> }
> $data .= trim($line)."\n";
> }
> $data = str_replace("\r","",$data);
>
> header("Content-Type: application/vnd.ms-excel");
> header("Content-Disposition: attachment; filename=database_dump.xls");
> header("Pragma: no-cache");
> header("Expires: 0");
> print $csv_output."\n".$data;
> exit;

This code does NOT obtain data from MySQL in Excel format.

This code obtains data from MySQL in the standard format that MySQL delivers
its data.

The code does NOT even write the data out in Excel format. It writes it out
(very badly code wise) in tab delimited format.

Excel then attempts to guess what type of data is between each delimiter.

If you view the outputted data in a text editor, you will see the seconds
value. Try reformatting the cell in Excel.

My advice is to output this data in Excel's XML format, then you can specify
precisely what the format should be for each cell.

See: http://msdn.microsoft.com/en-us/library/aa140066(office.10).aspx



Posted by Geoff Cox on July 18, 2008, 4:05 pm
Please log in for more thread options
On Fri, 18 Jul 2008 20:13:32 +0100, "Paul Lautman"

>This code does NOT obtain data from MySQL in Excel format.
>
>This code obtains data from MySQL in the standard format that MySQL delivers
>its data.
>
>The code does NOT even write the data out in Excel format. It writes it out
>(very badly code wise) in tab delimited format.
>
>Excel then attempts to guess what type of data is between each delimiter.
>
>If you view the outputted data in a text editor, you will see the seconds
>value. Try reformatting the cell in Excel.

Paul,

Thanks for that insight - indeed I can see the seconds using a test
editor!

And yes, if I use a custom format, dd/mm/yyy hh:mm:ss I do get all the
data!

I will follow up the link below too.

Many thanks

Geoff

>
>My advice is to output this data in Excel's XML format, then you can specify
>precisely what the format should be for each cell.
>
>See: http://msdn.microsoft.com/en-us/library/aa140066(office.10).aspx
>

Posted by Geoff Cox on July 18, 2008, 4:58 pm
Please log in for more thread options
wrote:

using a text editor ...

Geoff

Posted by Luuk on July 18, 2008, 3:17 pm
Please log in for more thread options
Geoff Cox schreef:
> Hello
>
> The code below is from a php file which can obtain data from a mysql
> database in Excel format.
>
> Does any part of it explain why the time part if the Date/Time field
> does not have the seconds value?
>
> Cheers
>
> Geoff
>
>
> $link = mysql_connect ($Host, $User, $Password) or die('Could not
> connect: ' . mysql_error());
> mysql_select_db($DBName) or die('Could not select database');
>
> $select = "SELECT * FROM `".$TableName."` ORDER by DateTime";
> $export = mysql_query($select);
> $fields = mysql_num_fields($export);
>
> for ($i = 0; $i < $fields; $i++) {
>         $csv_output .= mysql_field_name($export, $i) . "\t";
> }
>
> while($row = mysql_fetch_row($export)) {
>         $line = '';
>         foreach($row as $value) {
>                 if ((!isset($value)) OR ($value == "")) {
>                         $value = "\t";
>                 } else {
>                         $value = str_replace('"', '""', $value);
>                         $value = '"' . $value . '"' . "\t";
>                 }
>                 $line .= $value;
>         }
>         $data .= trim($line)."\n";
> }
> $data = str_replace("\r","",$data);
>
> header("Content-Type: application/vnd.ms-excel");
> header("Content-Disposition: attachment; filename=database_dump.xls");
> header("Pragma: no-cache");
> header("Expires: 0");
> print $csv_output."\n".$data;
> exit;


sounds like a php-only question, if you're not telling us the definition
of your table.

in other words:
when you start up your mysql client, and type:
SELECT * from TableName ORDER by DateTime;
do you see seconds ? (or not?)

--
Luuk

Similar ThreadsPosted
php.ini sets max time to 90 seconds, yet I get a "Max execution time of 30 seconds exceeded" August 18, 2007, 3:35 am
Loop after "x" seconds September 22, 2005, 4:59 am
mysql_data_seek takes...0.15 seconds !!!! February 18, 2005, 8:03 pm
Show countdown from 10 to 0 seconds July 27, 2005, 1:00 pm
howto: please wait, this may take a few seconds... September 21, 2005, 11:35 pm
What am I missing here? March 12, 2008, 10:55 pm
fopen() taking ten seconds or more to come back... October 18, 2005, 7:57 am
Unix Time and Leap Seconds June 23, 2007, 1:04 am
Run a PHP script in background of a Site in every 10 Seconds February 17, 2008, 8:29 am
line missing September 4, 2004, 1:20 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap