Jump to content

[SOLVED] Date format


jeeves245

Recommended Posts

I have a query - "UPDATE 'table' SET 'column' = '$_POST['date']' WHERE ........"

 

But the date is in DD-MM-YYYY format. What's the best way to convert my dates to YYYY-MM-DD format before it goes into the DB?

 

I know how to convert them when they're coming OUT of the DB, just not when they're going IN.

 

Thanks :)

 

 

Link to comment
Share on other sites

Ohhh, well I got the date in the correct format. It writes to the DB, but as 2036-04-21 which isn't right.. I used this:

 

UPDATE `smail` SET `estimatedDelivery` = '" . date("Y-m-d",strtotime($_POST['estimatedDelivery'][$i])) . "' WHERE `psNumber` = '" . mysql_real_escape_string($_POST['id'][$i]) . "'";

 

Trial and error seems to be getting me somewhere.. slowly

Link to comment
Share on other sites

Yes, the values in $_POST['date'] are definitely 00-00-0000 format.

 

Would this work:

 

$string = explode('-',$_POST['estimatedDelivery']);
$string = array_reverse($string);
$string = implode('-',$string);

 

Then in the update query substitute

"UPDATE `smail` SET `estimatedDelivery` = '" . $_POST['estimatedDelivery'][$i] . "' WHERE ...

for

"UPDATE `smail` SET `estimatedDelivery` = '" . $string[$i] . "' WHERE ...

 

Link to comment
Share on other sites

There is so many ways of doing this... heres one

<?php
$oldDate = '30-07-2009';
list($day, $month, $year) = explode('-', $oldDate);
$newDate = $year . '-' . $month . '-' . $day;
echo $newDate; // 2009-07-30 

 

EDIT: OOpps.. didnt read all messages posted here, this is quite the same as already mentioned...

Link to comment
Share on other sites

You just have to remember that $_POST['estimatedDelivery'] is an array. Just reformat the date in the for loop from your previous thread:

 

<?php
$count = count($_POST['estimatedDelivery']);
for ($i = 0; $i < $count; $i++) {
   $date = implode('-', array_reverse(explode('-', $_POST['estimatedDelivery'][$i])));
   $sql2 = "UPDATE `smail` SET `estimatedDelivery` = '" . mysql_real_escape_string($date) . "' WHERE `estimatedDelivery` = '" . mysql_real_escape_string($_POST['id'][$i]) . "'";
   mysql_query($sql2);
}
?>

Link to comment
Share on other sites

Hmm..

 

When I use print_r ($date); to see what is in that array, it only gives me one date even though $_POST['estimatedDelivery'] contains about 20 dates.

 

When I run the code you gave me, no changes are made in the DB at all.

 

Bah, this is like the last line I need to complete the program and i've spent about 2 days on it :(

Link to comment
Share on other sites

Why dont you do print_r($_POST['estimateDelivery']); and paste the results in here, then we can surely show the right way when we know the contents we are dealing with. Or maybe not the whole content if it is large, but some data from the beginning atleast to describe the structure of the array and the data in it.

Link to comment
Share on other sites

Why dont you do print_r($_POST['estimateDelivery']); and paste the results in here, then we can surely show the right way when we know the contents we are dealing with. Or maybe not the whole content if it is large, but some data from the beginning atleast to describe the structure of the array and the data in it.

 

print_r($_POST['estimateDelivery']); gives me absolutely no output

Link to comment
Share on other sites

Hmm..

 

When I use print_r ($date); to see what is in that array, it only gives me one date

 

That's what you'd expect, since $date is derived from a single element of the $_POST['estimatedDelivery'] array, hence the [$i] part. Just make sure you've got no misspelling, and it should work.

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.