Andrea Salmon
Andrea Salmon

Reputation: 13

php strtotime function (2)

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.

Upvotes: 1

Views: 209

Answers (1)

Madara's Ghost
Madara's Ghost

Reputation: 174947

You're basically doing:

100 * 80 / 80

Just save it as

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

Or better yet, represent it as a DateTime object:

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

Using a DateTime object (And the DateTime family) gives you much better and more flexible control over your date/times.

Upvotes: 2

Related Questions