Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

array and date format

Posted on 2006-04-21
4
Medium Priority
?
223 Views
Last Modified: 2011-09-20
i want to have

if ship_date is 0000-00-00, then it has to be empty.
if ship_date is 2006-04-21, then it has to be 04/21/2006.

How can i get those results based on the bottom code.


--------------------------------------


$csvquery = 'SELECT * FROM testcsv ';
$result = mysql_query($csvquery) or die("Error encountered ".mysql_error());
$csvData= Array();
$fieldCount=mysql_num_fields($result);
 
for($j=0;$j<$fieldCount;$j++) {
  $csvData[0][$j]=mysql_field_name($result,$j);         
}
             
$i=1;
while($csvrow = mysql_fetch_array($result, MYSQL_NUM)) {
       $csvData[$i++]=$csvrow;
       print_r($csvData);
}


-------------------------------------------------------



Array
(
    [0] => Array
        (
            [0] => order_number
            [1] => LNnumber
            [2] => order_approval_status
            [3] => ship_date
            [4] => tracking_number
        )

    [1] => Array
        (
            [0] => M0101
            [1] => KVTR-6NYLQF
            [2] => 1
            [3] => 2006-04-21
            [4] =>
        )

)
Array
(
    [0] => Array
        (
            [0] => order_number
            [1] => LNnumber
            [2] => order_approval_status
            [3] => ship_date
            [4] => tracking_number
        )

    [1] => Array
        (
            [0] => M0101
            [1] => KVTR-6NYLQF
            [2] => 1
            [3] => 2006-04-21
            [4] =>
        )

    [2] => Array
        (
            [0] => M0102
            [1] => KVTR-6NYLQG
            [2] => 1
            [3] => 0000-00-00
            [4] =>
        )

)
Array
(
    [0] => Array
        (
            [0] => order_number
            [1] => LNnumber
            [2] => order_approval_status
            [3] => ship_date
            [4] => tracking_number
        )

    [1] => Array
        (
            [0] => M0101
            [1] => KVTR-6NYLQF
            [2] => 1
            [3] => 2006-04-21
            [4] =>
        )

    [2] => Array
        (
            [0] => M0102
            [1] => KVTR-6NYLQG
            [2] => 1
            [3] => 0000-00-00
            [4] =>
        )

    [3] => Array
        (
            [0] => M0103
            [1] => KVTR-6NYLQH
            [2] => 4
            [3] => 0000-00-00
            [4] =>
        )

)
0
Comment
Question by:bbvic
  • 2
4 Comments
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 16507420
<?php
if ('0000-00-00' != $date_from_csv)
 {
 $date_from_csv = date('d/m/Y', strtotime($date_from_csv));
 }
else
 {
 $date_from_csv = '';
 }


or

$date_from_csv = ('0000-00-00' != $date_from_csv) ? date('d/m/Y', strtotime($date_from_csv)) : '';

0
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 2000 total points
ID: 16507430
Oops sorry. You want funny american formatted date!

How on earth do you sort by dates? Wierd.

Anyway!

<?php
if ('0000-00-00' != $date_from_csv)
 {
 $date_from_csv = date('m/d/Y', strtotime($date_from_csv));
 }
else
 {
 $date_from_csv = '';
 }
?>


or
<?php
$date_from_csv = ('0000-00-00' != $date_from_csv) ? date('m/d/Y', strtotime($date_from_csv)) : '';
?>
0
 

Author Comment

by:bbvic
ID: 16507546
thanks
0
 
LVL 9

Expert Comment

by:tolgaong
ID: 16507753
a solution with mysql query

$csvquery="SELECT
order_number,
LNnumber,
order_approval_status,
IF(left(ship_date,4)='0000','',date_format(ship_date,'%m/%d/%Y')) as ship_date
FROM `testcsv` ";
$result = mysql_query($csvquery) or die("Error encountered ".mysql_error());
$csvData= Array();
$fieldCount=mysql_num_fields($result);
 
for($j=0;$j<$fieldCount;$j++) {
  $csvData[0][$j]=mysql_field_name($result,$j);        
      }
           
$i=1;
while($csvrow = mysql_fetch_array($result, MYSQL_NUM)) {
      $csvData[$i++]=$csvrow;
      }
echo "<pre>";
print_r($csvData);
echo "</pre>";
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month21 days, 6 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question