Jump to content

dates inserted into mysql


anthony-needs-you

Recommended Posts

Hi i insert my dates from a calander into my mysql database with this:

 

$expireDate = date("Y-m-d", strtotime($_POST['expireDate']));

 

The client wants it displayed in the input box before being inserted in uk format d-m-Y.

 

If i do this using the above code the database doesnt understand it. I can display it after inserting it but how do i display it in the uk format before insert?

Link to comment
https://forums.phpfreaks.com/topic/139204-dates-inserted-into-mysql/
Share on other sites

There's a few things that need to all be done for this work optimally.

 

1) I'd suggest inserting your dates like so:

<?php
$expireDate = strtotime($_POST['expireDate']);
$query = "INSERT INTO some_table (expire) VALUES (FROM_UNIXTIME('$expireDate')";
?>

That way, you will always have it as the correct format inside the databsae.

 

2) When pulling the data, use the MySQL DATE_FORMAT() function.

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format

 

Once you have all that set up, I'd suggest making a timestamp of the date before displaying it.  Then, you can call the date() function when you display it, and use the timestamp in the value attribute of the expireDate field on your form.  If you do it this way, strtotime() is not needed on the receiving page.

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.