Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Manipulate date and time format 1

Status
Not open for further replies.

likelylad

IS-IT--Management
Jul 4, 2002
388
GB
I have a form that the user is inputing the date and time in the following format
07/11/2002 12:34 (this is copied from an Oracle Database that I have no control over)

The data is then being entered into a MySQL database. The data will not tranfer over in this format.
I need to manipulate it so that it transfers it over as
2002/11/07 12:34 (I have manually typed this format in and I know it gets transfered across successfully)

Thanking you in advance for any help received.




 
something like

preg_replace ("/(\d{2})\/(\d{2})\/(\d{4}) (\d\d:\d\d)/" , "$3/$2/$1 $4", '07/11/2002 12:34');

should do it ______________________________________________________________________
TANSTAAFL!
 
Hi sleipnir214

Thanks for getting back to me.
I am just reading my initial post and I didn't give a dec ent explanation of the problem.

The date/time value is entered into a text box with a name called order_date
This value will be a variable, but in the format dd/mm/yyyy hh:mm

When I hit the submit button I need the variable changed to yyyy-mm-dd hh:mm to be inserted into the MySQL Database.

Can your suggested code be manipulated to solve my problem, also where do I insert this snippet of code.
 
Assuming your form uses the POST method...

Sometime before you perform you insert, do

[tt]
Code:
$_POST['order_date'] = preg_replace ("/(\d{2})\/(\d{2})\/(\d{4}) (\d\d:\d\d)/", "$3-$2-$1 $4", $_POST['order_date']);
[/tt]

This will swap the year and day and replace the slashes with dashes. But you really don't have to worry about converting the slashes to dashes -- MySQL will take the date either way. ______________________________________________________________________
TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top