Jump to content

month/date with leading zero


phppup

Recommended Posts

I've got a date input from a dropdown menu and want to add a leading zero to the first 09 numbers.

    if(document.getElementById("month").value < 10)  {
        ("0" + document.getElementById("month").value) = month;     }

Is apparently an incorrect application.

I have seen  

("0" + (yourDate.getMonth() + 1)).slice(-2)

but cannot seem to get that working either.

Please help.

 

 

Link to comment
Share on other sites

Is there a reason you're not using a datepicker? I know the jQuery DatePicker has formatting options and I have to assume other native/node/ESx datepickers do as well. It'd certainly be easier than prepending zeros to combobox values everywhere.

Link to comment
Share on other sites

I was going to open a new thread, but since MAXXD kept this one alive.....

My JS code seems to be working fine, but for some reason the input to transfer the birthday to the db table is not making the connection from JS to the hidden input.

function xyz()  {     // called onclick

    - - blah blah blah to add lead zeros - - 

  birthday = year.value + "-" + month.value + "-" + day.value;
   
alert(birthday); //works fine and confirms format and correct date

}
 
</script>

<input type="hidden" id="birthday" name="birthday" value="birthday"> 

In my PHP a line was added:   echo $birthday;

Along with the essentials to post into the db

The above code will create a BLANK entry into the db after JS confirms in the alert that the correct data is available.

<input type="hidden" id="birthday" name="birthday" value="2019-01-24"> 

will confirm 2019-01-24 through echo $birthday and also add 2019-01-24 to the db field, even if 2001-07-04 was confirmed by JS from my form.

What do I need to do to resolve the broken connection, please?

Link to comment
Share on other sites

19 minutes ago, phppup said:

maxxd:  because DatePickers seem burdensome for the user to input a birthdate from 1955.

Legit. However, you can use jQuery's datepicker with drop-down month and year selectors to make this easier.

To the question at hand, you say you're getting the date from a drop-down menu. How is that built? Seems to me it'd be easiest to add the leading zero on the value attribute (not necessarily the display text) during form compile rather than during validation and/or submission.

As to your question about the validation, it depends on how you're submitting the data to the controller script. If you're submitting via AJAX from JavaScript, make sure you're passing the 'birthday' variable you've built in that snippet. If you're doing a full page refresh, assign the value of the 'birthday' variable to the hidden input before submitting the form - otherwise whatever script is processing the form data is getting the data from the hidden form field and not the JS variable.

Link to comment
Share on other sites

Yes, the leading zeros are handled acceptably.  Maybe not exactly perfect, but good enough for now.

I had thought of this, but considered the problem to be something more (although nothing alarming).  I'll give it another look and see if I can resolve it with this insight.

I don't generally interact well with AJAX or JQuery, but I do like this particular Date Picker as an option (if I modify things later.... too committed to current code, right now.)  Here's a question: How can I tap into that Date Picker to add a feature for the person born in 1955 so I can ask if he knew he was born on a Tuesday?

PS: the DatePicker sample seems to run from 1992 to 2012, so I would need to figure out how to adjust that for my purposes.

And while we're tossing ideas around, I had considered bypassing the entire problem by POSTing year, month, and day to PHP and letting PHP reassemble the $birthday rather than using the JS.  Any thoughts or feedback on that methodology?

 

 

Link to comment
Share on other sites

Created a separate button for onclick="function xyz()" and it ran the script and alert validation with no problem.

But when I used SUBMIT, the same problem remained in that

<input type="hidden" id="birthday" name="birthday" value="birthday"> 

 inserted the word 'birthday ' into the db  [i had changed it to a VARCHAR column so that ALL entries would be visible]

Link to comment
Share on other sites

4 hours ago, phppup said:

Here's a question: How can I tap into that Date Picker to add a feature for the person born in 1955 so I can ask if he knew he was born on a Tuesday? 

I'd give http://api.jqueryui.com/datepicker/#method-getDate a shot - it should at least put you on the right track for this.

4 hours ago, phppup said:

the DatePicker sample seems to run from 1992 to 2012, so I would need to figure out how to adjust that for my purposes. 

http://api.jqueryui.com/datepicker/#option-yearRange

4 hours ago, phppup said:

And while we're tossing ideas around, I had considered bypassing the entire problem by POSTing year, month, and day to PHP and letting PHP reassemble the $birthday rather than using the JS

You're already running a complete page refresh on form submit, so I don't really see where this would become an issue for you. And again, you could format the date string in PHP before it's output to the page, thereby negating what appears to be a pain point for you.

Link to comment
Share on other sites

3 hours ago, phppup said:

Created a separate button for onclick="function xyz()" and it ran the script and alert validation with no problem.

But when I used SUBMIT, the same problem remained in that


<input type="hidden" id="birthday" name="birthday" value="birthday"> 

 inserted the word 'birthday ' into the db  [i had changed it to a VARCHAR column so that ALL entries would be visible]

Again, are you setting the value of #birthday to the computed date before you submit the page? If not, the processing script will have 'birthday' in $_POST['birthday'] because your validation script doesn't change the value of the field.

Link to comment
Share on other sites

I think I may just re-work the entire script. It may be easier than tinkering with all the little problems, although I am making some interesting discoveries along the way.

As for Jquery datepicker, it might be an interesting piece to toy with at a later date (no pun intended). LoL

Link to comment
Share on other sites

Re-structured my code for a more streamline structure, but same problem.

My THREE dropdowns that have names and ID's respectively as year, month, and day.

My only use of JS

function date_to_database()  {
      birthdate = 
      
      document.getElementById("year").value  + "-" + 
             document.getElementById("month").value + "-" + 
             document.getElementById("date").value;  
           alert("The dob to be passed is: " + birthdate);
     }

I've also added ALERTS to each dropdown ONCHANGE to alert(dob)

So I am effectively getting a visual confirmation of every instance that the date is effected.

in HTML;        <input type="hidden" id="birthdate" name="birthdate" value="birthdate">

My table now has two fields for birthdate1 and birthdate2

And my PHP is designed as:

$birthdate1=$_POST['birthdate1'];

$birthdate2= $_POST['year'].'-'.$_POST['month'].'-'.$_POST['date'];

My result is: The birthdate1 column will only populate the VARCHAR field with the word "birthdate" (or whatever other static value I put as the hidden's value.

The birthdate2 column will POST the correct information to the birthdate2 column.

While I realize that using The birthdate2 provides a solution to the problem, I am still confused as to what is preventing the birthdate1 data from being passed from HTML as a hidden input.

Answers?  Missed steps?  A break in the process?

 

Link to comment
Share on other sites

Your existing birthdate = code is not how you change a hidden input's value, all it does is assign a value to the variable birthdate.

If you want to change a hidden input's value then you need to change the value attribute on that input element by assigning to it.  Similar to when you read an input's value, but for writing instead.

function date_to_database()  {
	document.getElementById('birthdate').value = 
		document.getElementById("year").value  + "-" + 
		document.getElementById("month").value + "-" + 
		document.getElementById("date").value;  

  	alert("The dob to be passed is: " + document.getElementById('birthdate').value);
}

However, there's no real point in assembling things client side into a hidden field when you can just use PHP to assemble the date parts server-side when the form is submitted.

 

Link to comment
Share on other sites

23 hours ago, maxxd said:

Again, are you setting the value of #birthday to the computed date before you submit the page? If not, the processing script will have 'birthday' in $_POST['birthday'] because your validation script doesn't change the value of the field.

I'll just leave this here again (I added the emphasis). You need to change the value of the field before you submit the form.

Or follow kicken's suggestion to do the data modification on the server side in PHP.

Or use the jQuery Datepicker widget and avoid all these issues you've been banging your head against for several days now.

Link to comment
Share on other sites

Both kicken and maxxd: your telling me that I need to "change the value attribute on that input element, but your not explaining how I should do that.

Obviously, if I knew how to remedy this correctly, I would have done so days ago. Please elaborate with an example.

It's now an obstacle that I need to learn from.

PS: for practicality, is it preferable to assemble these sort of elements on the server side? Why?

Link to comment
Share on other sites

I'll admit, given that you could handle assigning a value to a variable and reading a value from a form element, I honestly just kinda assumed you'd be able to extrapolate assigning a variable value to a form element. Kicken's example should work for you - follow Barand's advice and tear it apart.

As to the question of doing the data manipulation on the server-side, it depends on what you're doing and how you're comfortable doing it. The world is moving to a place where online applications act and feel like desktop applications, so getting comfortable with asynchronous functionality (AJAX and fetch) will serve you well in future development. However, if you're not familiar or comfortable with JavaScript (or any of the umpteen bazillion JS frameworks that are out there now) but feel good with PHP, do it on the server side. It'll get the job done and give you more time to explore other processes and solutions.

Link to comment
Share on other sites

My apologies, and thanks.

When I read Kicken's reply it appeared to me as if he had simply cut and pasted my original code as a reference. ( I actually couldn't figure out why he would do that, but I've seen people repost entire responses just to make a short comment.)

However, now that I've reviewed it more closely, I have spotted the adjustments And will test them out.  

 

Link to comment
Share on other sites

OK, after plugging in Kicken's corrections, things are working even better than expected.  

Now my new problem is whether to use the birthdate 1 (transferred via JS and the hidden field) or birthrate2 (assembled by PHP on the server side).

Too many choices.

Link to comment
Share on other sites

In a related problem (which is more PHP) , I now have a SELECT statement to generate a list of people with the same birthdate.

I also have a formula that will calculate age with a birthdate from a form.

How and where (in relation to the SELECT) do I place the adapted formula so that it uses the birthdate (stored in my table) to calculate the ages of the listed people with the given birthday?

Link to comment
Share on other sites

"age" would go in the SELECT clause, along with other columns you are selecting. (whatever you want in the query output goes there)

For example ...

SELECT fname
     , lname
     , dob 
     , timestampdiff(YEAR, dob, curdate()) as age
FROM pupil
ORDER BY age;
+----------+------------+------------+------+
| fname    | lname      | dob        | age  |
+----------+------------+------------+------+
| Peter    | Appleby    | 2003-04-23 |   15 |
| Mary     | Whitehouse | 2002-09-06 |   16 |
| Adam     | Simms      | 2001-06-22 |   17 |
| Allan    | Blair      | 2001-03-04 |   17 |
| Anna     | Hamilton   | 2002-01-16 |   17 |
| Anne     | Bailey     | 2001-08-02 |   17 |
| Anthony  | Bell       | 2001-10-04 |   17 |
| David    | Powell     | 2001-05-03 |   17 |
| Emma     | Watson     | 2001-11-20 |   17 |
| George   | Wilson     | 2001-06-30 |   17 |
| Henry    | Irving     | 2001-08-12 |   17 |
| Jane     | Morrison   | 2001-08-24 |   17 |
| John     | Patterson  | 2001-09-06 |   17 |
| John     | Tully      | 2001-09-03 |   17 |
| John     | Watson     | 2001-09-30 |   17 |
| Margaret | Norton     | 2001-04-23 |   17 |
| Mary     | Sheldon    | 2001-06-14 |   17 |
| Michael  | Grove      | 2001-08-11 |   17 |
| Peter    | Adamson    | 2001-09-18 |   17 |
| Wayne    | Jones      | 2001-05-06 |   17 |
| William  | Smith      | 2001-12-08 |   17 |
| Caroline | Freeman    | 2000-12-13 |   18 |
| Jack     | Williams   | 2000-09-06 |   18 |
| Mary     | Blake      | 1999-10-04 |   19 |
+----------+------------+------------+------+

 

To see who share the same birthday ...

SELECT RIGHT(dob,5)  as mthday
     , GROUP_CONCAT(fname,' ',lname, ' (', timestampdiff(YEAR, dob, curdate()) , ')' SEPARATOR ' / ') as pupils
FROM pupil
GROUP BY RIGHT(dob,5)
HAVING COUNT(*) > 1;
+--------+-----------------------------------------------------------------+
| mthday | pupils                                                          |
+--------+-----------------------------------------------------------------+
| 04-23  | Margaret Norton (17) / Peter Appleby (15)                       |
| 09-06  | John Patterson (17) / Jack Williams (18) / Mary Whitehouse (16) |
| 10-04  | Anthony Bell (17) / Mary Blake (19)                             |
+--------+-----------------------------------------------------------------+

 

Link to comment
Share on other sites

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.