Jump to content

[SOLVED] simple date store not working?


delphi123

Recommended Posts

Hi there,

 

I have a web form which I'm trying to use to insert a date from a textbox.

 

I basically want the user to be able to write the date in a format (eg dd/mm/yyyy) into the textbox and it'll add the date to the database.

 

Here's a snippet of my code:

$release_date=date("Ymd",$_POST['release_date']);

mysql_query("
INSERT INTO ratings_software (release_date)
					VALUES('$release_date')")or die("You have an error because:<br />" . mysql_error());

 

My database is filling with funny values like: 1970-01-13 when I enter things?!

 

Help!  ::)

Link to comment
Share on other sites

Although MySQL tries to interpret values in several formats, dates always must be given in year-month-day order (for example, '98-09-04'), rather than in the month-day-year or day-month-year orders commonly used elsewhere (for example, '09-04-98', '04-09-98').

courtesy of the <a href="http://dev.mysql.com/doc/refman/5.0/en/date-and-time-types.html">MYSQL MANUAL</a>

Link to comment
Share on other sites

As said above, you should always store date formats in the DB like yyyy-mm-dd.

 

So your code should look like this

 

<?php

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

mysql_query("
INSERT INTO ratings_software (release_date)
					VALUES('$release_date')")or die("You have an error because:<br />" . mysql_error());

?>

Link to comment
Share on other sites

the php date function requires

 

string date  ( string $format  [, int $timestamp  ] )

 

you are passing it a posted date (such as 29 september 2007) when it needs a timestamp instead

 

try


$tstamp = strtotime($_POST['release_date']);
mysql_query("
INSERT INTO ratings_software (release_date)
					VALUES('$tstamp)")or die("You have an error because:<br />" . mysql_error());


Link to comment
Share on other sites

the php date function requires

 

string date  ( string $format  [, int $timestamp  ] )

 

you are passing it a posted date (such as 29 september 2007) when it needs a timestamp instead

 

try


$tstamp = strtotime($_POST['release_date']);
mysql_query("
INSERT INTO ratings_software (release_date)
					VALUES('$tstamp)")or die("You have an error because:<br />" . mysql_error());


 

The date needs to also be converted to the correct format, the code I supplied will do that.

Link to comment
Share on other sites

sorry poco I missed that bit out - damn - forgive me

should have read

$tstamp = strtotime($_POST['release_date']);
$release_date=date("Ymd",$tstamp);

mysql_query("
INSERT INTO ratings_software (release_date)
					VALUES('$release_date')")or die("You have an error because:<br />" . mysql_error());

 

i usually break mo code down like this, if an error is thrown I can easily isolate the problem, which I would have found in my case !

 

 

Link to comment
Share on other sites

This won't work

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

 

strtotime will interpret a value in the format of "xx/xx/xxxx" as "mm/dd/yyyy". However he is having his users enter the value in the format of "dd/mm/yyyy".

 

You could simply use an explode to create the date parts and then reassemble.

 

$dateParts = explode('/', $_POST['release_date']);
$date = "$dateParts[2]-$dateParts[1]-$dateParts[0]";

Link to comment
Share on other sites

Here is a solution

 

<?php 

$date = explode('/', $_POST['release_date']);
$release_date = $date[2].'-'.$date[1].'-'.$date[0];
$release_date = date("Y-m-d", strtotime($release_date));

mysql_query("INSERT INTO ratings_software (release_date)
             VALUES('$release_date')")or die("You have an error because:<br />" . mysql_error());

?>

 

Link to comment
Share on other sites

Hmm...

 

Wasn't Poco's solution the same as I posted in reply #7 but with an unneeded step?

 

<?php

$date = explode('/', $_POST['release_date']);

//This puts the date in the format YYYY-MM-DD
$release_date = $date[2].'-'.$date[1].'-'.$date[0];

//This takes the above value, converts it to a timestamps
//and then puts it back into the format YYYY-MM-DD ? ? ?
$release_date = date("Y-m-d", strtotime($release_date));


?>

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.