By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,472 Members | 1,222 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,472 IT Pros & Developers. It's quick & easy.

Strtotime and/or Date functions not working correctly

P: 7
Hi all,

I'm having a problem with either the date() func or the strtotime() func.

With the code written like below, the year inserted will always be 2010.

Expand|Select|Wrap|Line Numbers
  1.  $subchilddob = date("y-m-d", strtotime($_POST['child_dob_mth'][$child]."-".$_POST['child_dob_day'][$child]."-".$_POST['child_dob_year'][$child]));
With the code written like below, the day will always be either 1 or 2 and will sometimes roll the month back 1.

Expand|Select|Wrap|Line Numbers
  1. $subchilddob = date("y-m-d", strtotime($_POST['child_dob_year'][$child]."-".$_POST['child_dob_mth'][$child]."-".$_POST['child_dob_day'][$child]));

This is leading me to believe something is incorrect with the 3rd parameter, as it is the problem in both cases.

I have 3 drop downs on the form, submitting to a mysql table with data type 'date'. I know I'm missing something glaringly obvious, and any light shed would be appreciated!
May 21 '10 #1

✓ answered by Atli

Just the relevant parts of the form should do. That is, the <select> boxes for the DOB.

The format you posted, "November-8-2000", is not a valid format for the strtotime function. It is meant to convert commonly used formats into a timestamp, and this format apparently doesn't fit that. - Try making it something like: "November 8, 2000". That's common in the US (I believe), so the function should be able to use it.

On a separate note, you should try to build the form to return numbers, rather than names. That is, you might want to build the month <select> box like the following:
Expand|Select|Wrap|Line Numbers
  1. <select name="DOB_Month">
  2.     <option value="01">January</option>
  3.     <option value="02">February</option>
  4.     <!-- And so forth... --->
  5. </select>
Then you could build the MySQL compatible date without having to use the strtotime or date functions:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $year = $_POST['DOB_Year']; // E.g. 2004
  3. $month = $_POST['DOB_Month']; // E.g. 1 or 01
  4. $day = $_POST['DOB_Day']; // E.g. 8 or 08
  5.  
  6. $mysql_date = sprintf("%d-%02d-%02d", $year, $month, $day);
  7. echo $mysql_date; // 2004-01-08
  8. ?>

Share this Question
Share on Google+
6 Replies


Atli
Expert 5K+
P: 5,058
Hey.

What does the string you are passing the strtotime function actually look like? Try creating it into a variable first and print it, just to see if it is as you expect it to be.

P.S.
It is best, when constructing date strings to be used in MySQL queries, to use the YYYY-MM-DD format ("Y-m-d"), rather than a YY-MM-DD format ("y-m-d"). - It may not change anything, as MySQL tries to parse several types, but the three digit year format is preferred.
May 22 '10 #2

P: 7
@Atli
Thanks for your reply.

I changed it to ("Y-m-d") and same results in the db.

The way the website works is theres a form, then when you click submit it gets posted to a process php file where all the database work happens, so I don't think I can print to the page(don't want to change the website/mess up the website) so it's hard to track what it looks like.

What I do know is the values are chosen from a drop down list, so probably integers, then they need to be concatenated with hyphens in the middle and changed to the DATE format for the mysql database.

Which is where the problem lies. Seems straightforward, but nothing I've tried works.
May 22 '10 #3

Atli
Expert 5K+
P: 5,058
so I don't think I can print to the page(don't want to change the website/mess up the website) so it's hard to track what it looks like.
You don't need to make permanent changes. Simply add a couple of lines to the page so it prints what you need to see, visit the page once, then revert the changes back. No harm done.

Also, could you show us the HTML form that is being posted?
May 22 '10 #4

P: 7
@Atli
I had a brain fart there - my bad. Dates being passed to the strtotime func look like this:

November-8-2000

What part of the HTML do you want to see? The php that makes the drop downs or the whole thing or???

There's a lot more on the form other then birthdays, don't want to needlessly clutter the forum up.

Thanks for you continuing help!
May 22 '10 #5

Atli
Expert 5K+
P: 5,058
Just the relevant parts of the form should do. That is, the <select> boxes for the DOB.

The format you posted, "November-8-2000", is not a valid format for the strtotime function. It is meant to convert commonly used formats into a timestamp, and this format apparently doesn't fit that. - Try making it something like: "November 8, 2000". That's common in the US (I believe), so the function should be able to use it.

On a separate note, you should try to build the form to return numbers, rather than names. That is, you might want to build the month <select> box like the following:
Expand|Select|Wrap|Line Numbers
  1. <select name="DOB_Month">
  2.     <option value="01">January</option>
  3.     <option value="02">February</option>
  4.     <!-- And so forth... --->
  5. </select>
Then you could build the MySQL compatible date without having to use the strtotime or date functions:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $year = $_POST['DOB_Year']; // E.g. 2004
  3. $month = $_POST['DOB_Month']; // E.g. 1 or 01
  4. $day = $_POST['DOB_Day']; // E.g. 8 or 08
  5.  
  6. $mysql_date = sprintf("%d-%02d-%02d", $year, $month, $day);
  7. echo $mysql_date; // 2004-01-08
  8. ?>
May 22 '10 #6

P: 7
The format you posted, "November-8-2000", is not a valid format for the strtotime function. It is meant to convert commonly used formats into a timestamp, and this format apparently doesn't fit that. - Try making it something like: "November 8, 2000". That's common in the US (I believe), so the function should be able to use it.

That fixed it! Thanks!

Now I have a database problem. But I know db's a tad better then php..so hopefully I can figure that one out!
May 22 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.