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
https://forums.phpfreaks.com/topic/169866-solved-date-format/
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
https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-896185
Share on other sites

But the date is in DD-MM-YYYY

 

Are you sure that's what you are getting from the form? Echo out the $_POST var.

 

Alternative to date() would be to explode at the hyphen and then just reorder the elements manually.

 

$string = explode('-',$string);

$string = array_reverse($string);

$string = implode('-',$string);

Link to comment
https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-896187
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
https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-896190
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
https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-897106
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
https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-897113
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
https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-897119
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
https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-897120
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
https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-897123
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
https://forums.phpfreaks.com/topic/169866-solved-date-format/#findComment-897141
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.