473,624 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strtotime and/or Date functions not working correctly

7 New Member
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
6 3506
Atli
5,058 Recognized Expert Expert
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
larsy87
7 New Member
@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 Recognized Expert Expert
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
larsy87
7 New Member
@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 Recognized Expert Expert
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
larsy87
7 New Member
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
2998
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 check for a complete years. And a complete year is from day 1 until the proceeding day the following year. A couple of examples:
17
2824
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 it is supposed to do. (I used GCC/MingW32 as the compiler with C'99 enabled. Oh, and there is no overflow-checking in this code!) The code follows: #include <ctype.h>
4
7758
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 CANNOT use the inbuilt date functions or the DateTime Type. Your program should work with any date between 1900 and 3000. Here are your acceptance tests:
5
60044
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 of days. 2 months before date from the current date and number of days. 3 months before date from the current date and number of days. 8 months before date from the current date and number of days.
2
6546
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 relation to Eastern Time. So all of the dates should be displayed as either PST or MST depanding on the time of the year. I am trying to use IsDaylightSavingTime to find out if the date in the database was inserted during DST. The code for my...
3
1916
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 www.IntraRELY.com
5
2856
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 connection string has subscripe notifications permission set to grant. But now it seems that the cache is kicking my product out of the cache
1
1971
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
8048
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. The data-only file stays there permanently, but I have the Users copy the interface file to their PC and use it locally. My problem is that I have a number of queries that use the Format() and date() functions. I have no problem executing them, but my...
3
3484
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 button the calendar does popup but when a date is selected the date is not displayed in the text box. If you reload the page then the date is displayed in the textbox, any ideas? thanks. -- Paul G Software engineer.
0
8238
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8624
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8336
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6111
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4082
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1485
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.