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
https://forums.phpfreaks.com/topic/81055-solved-simple-date-store-not-working/
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>

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());

?>

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 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.

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 !

 

 

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]";

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());

?>

 

This may not fit your bill but if you want your own date format, like (only an example)

 

$release_date =  date("H:i jS F Y");

- which returns '20:01 10th December 2007'

 

You can have the date field in your database set to 'Text' thus you can have any format.

 

Sam

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));


?>

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.