Jump to content

Formatting date


jeeves245

Recommended Posts

I didn't read all the back and forth, but:

<?php
$dates = array( '2009-01-01 23:43:01', '12/31/09 12 AM', '3:45 PM' );
// Assume `column` is either TIMESTAMP or DATETIME
foreach( $dates as $d ) {
  $temp = strtotime( $d );
  if( $temp === false ) {
    echo "can't insert {$d}";
    continue;
  }
  $temp = "'" . date( 'Y-m-d H:i:s', $temp ) . "'"
  if( mysql_query( "insert into `yourtable` ( `column` ) values ( {$temp} )" ) ) {
    echo "inserted {$d}";
  }else{
    echo "failed on {$d}";
  }
?>

 

The trick is to use strtotime on the input if it is in a common enough format to get a timestamp out of it.  Then pass it through date( 'Y-m-d H:i:s' ) to put it in YYYY-MM-DD HH:MM:SS format, which is what the database accepts.

Link to comment
Share on other sites

If everything is printing out correctly, the final product should look  like

 

<?php
$con = mysql_connect("localhost","root","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("database", $con);

$file_handle = fopen("deliveries.csv", "rb");

while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode(',', $line_of_text);
$datestore = str_replace("/","-",$parts[0]);
$datestore = explode("-",$datestore);
$datestore = $datestore[2]."-".$datestore[1]."-".$datestore[0];

//Seperate parts of CSV file where there is a comma

$sql="INSERT INTO customer (customerName, address1, address2, address3)
VALUES
('$parts[1]','$parts[4]','$parts[5]','$parts[6]')";
//The above works correctly

$sql="INSERT INTO deliveryInformation(deliveryDate,psNumber,numItems,volume)
VALUES('$datestore','$parts[2]','$parts[3]','$parts[7]')";
//$parts 2,3,7 are inserted correctly, but date shows as 0000-00-00

mysql_query($sql);
}

fclose($file_handle);

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
else
   {
   echo "Records submitted.";
   }
   
   mysql_close($con);
   
?>

Link to comment
Share on other sites

^^^ Try mine first then..

 

I didn't read all the back and forth, but:

<?php
$dates = array( '2009-01-01 23:43:01', '12/31/09 12 AM', '3:45 PM' );
// Assume `column` is either TIMESTAMP or DATETIME
foreach( $dates as $d ) {
  $temp = strtotime( $d );
  if( $temp === false ) {
    echo "can't insert {$d}";
    continue;
  }
  $temp = "'" . date( 'Y-m-d H:i:s', $temp ) . "'"
  if( mysql_query( "insert into `yourtable` ( `column` ) values ( {$temp} )" ) ) {
    echo "inserted {$d}";
  }else{
    echo "failed on {$d}";
  }
?>

 

The trick is to use strtotime on the input if it is in a common enough format to get a timestamp out of it.  Then pass it through date( 'Y-m-d H:i:s' ) to put it in YYYY-MM-DD HH:MM:SS format, which is what the database accepts.

 

I tried that but it didnt work :(...

  :shrug:

Hopefully your script will work, I test mine above, just re arranged the format to YYYY-MM-DD (DATE)... and inserts it into the db.

 

Link to comment
Share on other sites

One more quick question.. is there any way that script can be modified to accommodate for another date? Say that $parts[1] was also a date. Would this work?

 

<?php
//for $parts[0]
$datestore = str_replace("/","-",$parts[0]);
$datestore = explode("-",$datestore);
$datestore = $datestore[2]."-".$datestore[1]."-".$datestore[0];
//for $parts[1]
$datestore2 = str_replace("/","-",$parts[1]);
$datestore2 = explode("-",$datestore2);
$datestore2 = $datestore2[2]."-".$datestore2[1]."-".$datestore2[0];
?>

 

In theory I thought that would work, but it doesn't.

Link to comment
Share on other sites

Yes that would work... You can make that a function actually, to make your code more clear

 

<?php
function formatCSVDate($part){

$part= str_replace("/","-",$part);
$part= explode("-",$part);
$part= $part[2]."-".$part[1]."-".$part[0];

return $part;
}
?>

 

 

do

 

<?php
//for $parts[0]
$datestore =formatCSVDate($parts[0]);
//for $parts[1]
$datestore2 =formatCSVDate($parts[1]);
?>

Link to comment
Share on other sites

Cool, perfect. Thanks again. I'm learning a lot here :D

 

Just been playing around with some code (trying to do it myself)... Would this be the correct way to format the date when extracting it from the database? Assume this code is executed AFTER the SQL select statement.

 

$datestore = str_replace("/","-",$row['deliveryDate']);
$datestore = explode("-",$datestore);
$datestore = $datestore[2]."-".$datestore[1]."-".$datestore[0];

 

$row['deliveryDate'] is where the date from $parts[0] is stored.

Link to comment
Share on other sites

When its in the DATABASE and you execute it to the page, its already formatted so just print it out.

 

If you want to change the format then covert the string to a timestamp then format it to anything you liek using the date function http://ca.php.net/manual/en/function.date.php

 

<?php

/* Mysql Query Here...*/
$date = $row['deliveryDate'];
$timestamp = strtotime($date);

// use the date() function to change the format
$new_date = date("Y m d h:i:s",$timestamp);


?>

Link to comment
Share on other sites

When its in the DATABASE and you execute it to the page, its already formatted so just print it out.

 

If you want to change the format then covert the string to a timestamp then format it to anything you liek using the date function http://ca.php.net/manual/en/function.date.php

 

<?php

/* Mysql Query Here...*/
$date = $row['deliveryDate'];
$timestamp = strtotime($date);

// use the date() function to change the format
$new_date = date("Y m d h:i:s",$timestamp);


?>

 

Yeah sorry I meant I want to change the format back to what it is in the CSV file. Will try that code. Thanks.

Link to comment
Share on other sites

When its in the DATABASE and you execute it to the page, its already formatted so just print it out.

 

If you want to change the format then covert the string to a timestamp then format it to anything you liek using the date function http://ca.php.net/manual/en/function.date.php

 

<?php

/* Mysql Query Here...*/
$date = $row['deliveryDate'];
$timestamp = strtotime($date);

// use the date() function to change the format
$new_date = date("Y m d h:i:s",$timestamp);


?>

 

Yeah sorry I meant I want to change the format back to what it is in the CSV file. Will try that code. Thanks.

 

 

Ya use this function

 

<?php
function CSVDate($date){

$timestamp = strtotime($date);

// use the date() function to change the format
$new_date = date("d/m/Y",$timestamp);

return $new_date;

}

$old_csv_date = CSVDate($row['deliveryDate']);
print $old_csv_date;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.