Php date format

Hello,

Please any help about that.

Here is the code :



for($m = 0;$m <= 0; $m++)
{
$now = date ("Y-m-d", mktime (0,0,0,date("m"),date("d")+$m,date("Y")));
echo "<input type=\\"hidden\\" name=\\"open_date\\" value=\\"" . $now . "\\">";
}


It inserts the actual date in YYYY-MM-DD.
The field “open_date” in the MYSQL table is type “date”.
Could you please give the exact modification to give to this code to have DD-MM-YYYY format cause for the moment always 0000-00-00 result…

Thanks a lot,

seb

Try this:


for($m = 0;$m <= 0; $m++)
{
    $now = date ("d-m-Y", mktime (0,0,0,date("m"),date("d")+$m,date("Y")));
    echo "<input type=\\"hidden\\" name=\\"open_date\\" value=\\"" . $now . "\\">";
}

Do you mean only this to be in that format?


$now = date ("d-m-Y", mktime (0,0,0,date("m"),date("d")+$m,date("Y")));

Off Topic:

oops!! beaten!!

thank you for your answer.
The fact to intervert Y-m-d to d-m-Y doesn’t bring anything.
It returns me 0000-00-00 value…

Any idea?

seb

I don’t know what you are trying to do. Does your for loop (for($m = 0;$m <= 0; $m++)) works as expected? But the following works quit good at my end:


$tot = date('t');
for($m = 0; $m <= 31; $m++){
    echo date('d-m-Y', strtotime("+$m day", time())) . '<br />';
}

Do you mean that it’s adding 0000-00-00 to the database?

If so, you’ll need to convert the DD-MM-YYYY you’re submitting to YYYY-MM-DD before adding it to the database.

E.G. (if you’re on PHP 5.3)


$date = DateTime::createFromFormat('d-m-Y', $_POST['open_date']);

Then use this as part of your insert query


$date->format("Y-m-d");

If you post the code you’re using on the database submission portion of your script, I can advise further if needed.

Hello,

Here is the code in the page:


<form name=\\"form1\\" method=\\"post\\" onSubmit=\\"return (formCheck(this))\\" action=\\"complete.php?getid=1\\">
for($m = 0;$m <= 0; $m++)
{
$now = date ("Y-m-d", mktime (0,0,0,date("m"),date("d")+$m,date("Y")));
echo "<input type=\\"hidden\\" name=\\"open_date\\" value=\\"" . $now . "\\">";
}
</form>

Then the submission in the database :


switch ($getid) {
case 1:
$query = "INSERT INTO $taskstable (open_date) VALUES ('$_POST[open_date]','$tmpname')";
$result = mysql_query($query)or die("Unable to select database - complete.php");
$rowin2 = mysql_insert_id();
}
break;

Thank you to give me the solution,

seb

Something along these lines…


<form name=\\"form1\\" method=\\"post\\" onSubmit=\\"return (formCheck(this))\\" action=\\"complete.php?getid=1\\">
for($m = 0;$m <= 0; $m++)
{
$now = date ("d-m-Y", mktime (0,0,0,date("m"),date("d")+$m,date("Y")));
echo "<input type=\\"hidden\\" name=\\"open_date\\" value=\\"" . $now . "\\">";
}
</form>

Note the changed date format as required.


switch ($getid) {
case 1:
$date = DateTime::createFromFormat('d-m-Y', $_POST['open_date']); 
$insert_date = $date->format("Y-m-d");  
$query = "INSERT INTO $taskstable (open_date) VALUES ('$insert_date','$tmpname')";
$result = mysql_query($query)or die("Unable to select database - complete.php");
$rowin2 = mysql_insert_id();
}
break;

Adding the 2 lines to take the date submitted, and convert it to the MySQL DATE format, then inserting it with $insert_date.