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.

1
  • Have you tried adding debugging code to that, so you can see all the values that your code is working with, and that the right part of the if statement is being triggered? Commented Jun 25, 2012 at 16:53

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.