473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Strtotime and/or Date functions not working correctly

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. ?>

6 3480
Atli
5,058 Expert 4TB
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
@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
5,058 Expert 4TB
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
@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
5,058 Expert 4TB
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
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

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

Similar topics

1
by: sylvian stone | last post by:
Hi, I've used standard date functions in the past, but need to create something a little different, as I am working on an investment calculator. What I need to do is validate two dates, and...
17
by: Jonathan Burd | last post by:
Greetings everyone, Reading about the int64_t types added by C'99, I decided to implement a 64-bit version of atoi for my own library. However, for the test number provided, it isn't doing what...
4
by: Russell | last post by:
I have an assignment that I have to complete. I have to write a windows app in C#. Here is the spec: 1/ Date Comparison Build a program that will find the number of days between two dates. You...
5
by: Kiran | last post by:
Hi, Please help on date functions in C#. I would like to know the relevant exampl to retrieve 2 weeks before date from the current date, 1 month before date from the current date and number...
2
by: MJR | last post by:
I live in Arizona. We of course do not change for DST. All of the data that is inserted into our database is in Arizona time. My client wants me to display the Time Zone the record was inserted in...
3
by: IntraRELY | last post by:
I have the following function, Notice how I am passing the dateInterval as a string. What is the correct way to pass "DateInterval.Year" as a variable to a function? TIA, Steve Wofford...
5
by: Joel Barsotti | last post by:
I'm trying to get sql cache dependency working correctly. I've got sql server 2005 for my back end. I've don the "ALTER DATABASE dbName SET broker_enable" command. I made sure the user in my...
1
by: madhu | last post by:
I want db2 date functions and what is the diffrence between the oracle date functions and db2 date functions?
5
by: tombarth11 | last post by:
I have an Access 2003 SP2 Application in which I split the data base into 2 files: data-only.mdb and interface.mdb. I then "compiled" the interface file to a .mde file. Both files are on a server....
3
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I am using an ajax masked edit extender along with a text box and masked edit validator and calendarExtender. It was working correctly but now for some reason when the user selects the image...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.