Jump to content


Photo

Displaying date DD-MM-YYYY but updating it as YYYY-MM-DD


  • Please log in to reply
9 replies to this topic

#1 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 01 September 2006 - 02:39 PM

I've done an update form, I want to display the date as DD-MM-YYYY (which I've done fine) but when I come to update it trys to update it as DD-MM-YYYY but can't.

How do I rearange it to YYYY-MM-DD but still display it as normal dates would be?
~ Mutley.

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 01 September 2006 - 02:53 PM

So, assuming that the POSTed field value is named $updated_date and it's in dd-mm-yyyy format, the rearranged date ready for a MySQL date field will be ....

list($d,$m,$y) = explode("-", $_POST['updated_date']);
$rearranged_date = date("Y-m-d", mktime(0,0,0,$m,$d,$y));

Legend has it that reading the manual never killed anyone.
My site

#3 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 01 September 2006 - 02:56 PM

I'm too slow for andy :(
I'm just a guy who likes to code for fun...

#4 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 01 September 2006 - 03:05 PM

So, assuming that the POSTed field value is named $updated_date and it's in dd-mm-yyyy format, the rearranged date ready for a MySQL date field will be ....

list($d,$m,$y) = explode("-", $_POST['updated_date']);
$rearranged_date = date("Y-m-d", mktime(0,0,0,$m,$d,$y));


Where should that be placed? I formatted the date with:
DATE_FORMAT(date,'%d-%m-%Y') AS dstamp

Should I put the update code where all the others are listed as $_POST.... etc? Thanks.
~ Mutley.

#5 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 01 September 2006 - 07:48 PM

Anyone? Please help.
~ Mutley.

#6 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 01 September 2006 - 08:13 PM

On your form you should have
$update_date = $_POST['update_date'];

which is the field on your form where the date is to be updated

before the SQL INSERT put
list($day, $month, $year) = explode("-", $update_date);
$update = date('Y-m-d', strtotime("$year/$month/$day"));
which basically changes the date format to a date that SQL understands

Then in you SQL UPDATE you update the field i the database where the date is with $update
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#7 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 01 September 2006 - 09:17 PM

That's great but have one small problem from it, it always updates the date as:
31-12-1969

But it puts it in the right order, just not the date I want. Thank's alot for that though. Any help on this problem?
~ Mutley.

#8 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 01 September 2006 - 09:35 PM

Since either Adrock's code or mine both produce correct 'corrected' dates, I suspect that there's something wrong with your code or how you implemented either solution.  Please post all of the relevant parts of the script as it presently exists that processes data from the form, up to and including your MySQL insert query.
Legend has it that reading the manual never killed anyone.
My site

#9 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 01 September 2006 - 09:37 PM

Form area:
$result = mysql_query("SELECT team_id, score_id, home, away, date, DATE_FORMAT(date,'%d-%m-%Y') AS dstamp, scorehome, scoreaway, description FROM scores WHERE team_id=".$team_id." ORDER BY date ");
<input class="form" type="text" size="8" name="date" value="<?=$row['dstamp']; ?>"></td><td>	


Submit to database code:

list($day, $month, $year) = explode("-", $date);
$update = date('Y-m-d', strtotime("$year/$month/$day"));
$date = $_POST['date'];
$description = $_POST['description'];


mysql_select_db("rufc");

$sql = "UPDATE scores SET home='$home', away='$away', scorehome='$scorehome', 
scoreaway='$scoreaway', description='$description', date='$update' WHERE score_id = '".$score_id."' LIMIT 1";
mysql_query($sql);
echo $sql;

~ Mutley.

#10 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 01 September 2006 - 09:41 PM

You need the value of date BEFORE you process it into its components. Change to this:

$date = $_POST['date']; // what's the date?
list($day, $month, $year) = explode("-", $date); // now split it up
$update = date('Y-m-d', strtotime("$year/$month/$day")); // now re-form it
$description = $_POST['description'];

Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users