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.

Edited by jeff5656
Link to comment
Share on other sites

You can use date('y') to get the current year and add it to the input if the length is less than 6? Although I recommend you convert your date to a Unix timestamp before saving it in your database, that way it is really easy to manipulate in future

Edited by Manixat
Link to comment
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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.