Jump to content

Add Current Year To User-Inputted Date


jeff5656

Recommended Posts

I have a date field in a form.

 

If a user enters 12/23 instead of 12/23/12 I want to add the current year before putting it into the datatbase. The format in the database is Y-m-d because it's a date field.

 

How do I modify this code to do that? I'm sorry I'm a bit weak with dates, especially if I have to stray from the standard date code!

 

$this_date = date("Y-m-d", strtotime($_POST['this_date ']));
$query = "insert into table (this_date) values ('$this_date')";

 

If a user enters 12/23/12 or 12/23/2012, I do not need to add the current year, and the above code will work, so I need the code to take into account a user entering the year OR not entering it.

Link to comment
https://forums.phpfreaks.com/topic/272309-add-current-year-to-user-inputted-date/
Share on other sites

You shouldn't allow users to directly type dates. Even if you display the correct format next to the input field, you will get all kinds of incorrect input. You need to use a date picker/three separate drop-down select menus.

 

You should also not store unix timestamps since you must perform an ambiguous, timezone dependent, conversion on them to use for anything other than simple comparisons.

But does anyone know how to do this, or is it something that can't be done. I feel that PHP can do anything really, but this is a bit out of my range - that's why I came here because I know someone out there knows! I appreciate the feedback about user input etc, but if there is anyone that knows how to do this I would love to see what the code looks like that would make this work.

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.