1

I started to learn PHP and have to find a mistake (maybe in this code):

if($newvalues["year"] != null)
   $newvalues["year"] = date("Y-m-d", strtotime($newvalues["year"]."-01-01"));

The new date has to be saved in the array "$newvalues", but when I press the save button, it doesn't save anything. Only if the textfield "year" is empty, the other items can be saved.

Can anyone help me, please? Thanks.

4

1 回答 1

2

你基本上在做:

100 * 80 / 80

只需将其保存为

if($newvalues["year"] != null)
   $newvalues["year"] .= "-01-01";

或者更好的是,将其表示为一个DateTime对象:

$newvalues = array("year" => 2012);
if ($newvalues["year"] != null) {
    $newvalues["year"] = new DateTime("{$newvalues["year"]}-01-01");
}
var_dump($newvalues["year"]);

使用 DateTime 对象(以及 DateTime 系列)可以让您更好、更灵活地控制日期/时间。

于 2012-06-25T16:54:01.327 回答