Jump to content

How to format a date into MySQL [RESOLVED]


AdRock

Recommended Posts

I have a text field on a form with a date that is entered using this format [code]8/16/2006[/code]

I would like to insert it as a date in a MySQL database and when i want to display it from the database I would like it in this format [b]18 August 2006[/b]

How would I go about inserting it into the database into a recgonisable format.  I can display it in the required format once it's in the database but i need to get it in there first
Link to comment
Share on other sites

I have a really strange problem with changing the date into a sql date

I selected a date of [b]30-10-2006 [/b] and in the database it inserted the date as [b]2036-01-27[/b]

Here is the code that inserts/updates the record
[code]$event = stripslashes($_POST['txtEvent']);
...
....
....
    $EventDate = date('Y-m-d', strtotime($event));

    $query="UPDATE events SET title='$name', eventdate='$EventDate', content='$message', photo='$pic' WHERE id='$ud_id'";
    mysql_query($query);[/code]
Link to comment
Share on other sites

[quote author=AdRock link=topic=105335.msg421399#msg421399 date=1156430413]
I have a really strange problem with changing the date into a sql date

I selected a date of [b]30-10-2006 [/b] and in the database it inserted the date as [b]2036-01-27[/b]

Here is the code that inserts/updates the record
[code]$event = stripslashes($_POST['txtEvent']);
...
....
....
    $EventDate = date('Y-m-d', strtotime($event));

    $query="UPDATE events SET title='$name', eventdate='$EventDate', content='$message', photo='$pic' WHERE id='$ud_id'";
    mysql_query($query);[/code]

[/quote]

that's because 30-10-2006 is not a typical readable date format for strtotime(). when you're working with dates and times, it's usually a MM/DD/YYYY or YYYY-MM-DD format. so, in your case, when it's a DD-MM-YYYY, you'll need to do something like this:
[code]
<?php
$event = stripslashes($_POST['txtEvent']);
list($day, $month, $year) = explode("-", $event);
$EventDate = date('Y-m-d', strtotime("$month/$day/$year"));
?>
[/code]

the only reason i pass the variables through the date() and strtotime() functions again is to be absolutely certain that the value entered into the database is the right format.

hope this helps
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.