473,232 Members | 1,473 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,232 software developers and data experts.

Date conversion

I have Month, Day and Year fields on my form. When I submit this form I
want to create a valid date from all three fields before inserting into
MYSQL table.
I tried following:
$dob=$Month."-".$Day."-".$Year;
$dob = strtotime($dob);
But it does not update my date field in the table.
I am novice in PHP, do you know where i am making a mistake.

Jul 17 '05 #1
8 2161
nimish wrote:
I have Month, Day and Year fields on my form. When I submit this form
I want to create a valid date from all three fields before inserting
into MYSQL table.
I tried following:
$dob=$Month."-".$Day."-".$Year;
$dob = strtotime($dob);
But it does not update my date field in the table.
I am novice in PHP, do you know where i am making a mistake.


A MySQL date field is in the format YYYY-MM-DD so you need to format it
like so:

$dob = "$Year-$Month-$Day";

and then not do any further conversion.

Note that you should really be using $_POST for values passed from a
form as register_globals is now off by default and you cannot rely on
eg $Month being posted from a form value named "Month".

You also cannot trust any data sent from a web form and need to validate
it first. This can be as simple as casting the value as an integer like
so:

$dob = (int)$_POST['Year'].'-'.(int)$_POST['Month'].(int)$_POST['Day'];

This doesn't actually ensure it is a valid date though; you can test it
with the checkdate() function: http://www.php.net/checkdate

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #2
Thanks very much Chris.

I changed output in YYYY-MM-DD format as per your instructions, and
even check date using checkdate(). And it returns true. But still it
is not updating my table. I am writing SQL statement like this:
$query = "insert into tbl_master(dob) values($dob)";

Now what can be the wrong?
Nimish

Chris Hope wrote:
nimish wrote:
I have Month, Day and Year fields on my form. When I submit this form I want to create a valid date from all three fields before inserting into MYSQL table.
I tried following:
$dob=$Month."-".$Day."-".$Year;
$dob = strtotime($dob);
But it does not update my date field in the table.
I am novice in PHP, do you know where i am making a mistake.
A MySQL date field is in the format YYYY-MM-DD so you need to format

it like so:

$dob = "$Year-$Month-$Day";

and then not do any further conversion.

Note that you should really be using $_POST for values passed from a
form as register_globals is now off by default and you cannot rely on
eg $Month being posted from a form value named "Month".

You also cannot trust any data sent from a web form and need to validate it first. This can be as simple as casting the value as an integer like so:

$dob = (int)$_POST['Year'].'-'.(int)$_POST['Month'].(int)$_POST['Day'];
This doesn't actually ensure it is a valid date though; you can test it with the checkdate() function: http://www.php.net/checkdate

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/


Jul 17 '05 #3
Thanks very much Chris.
I changed output in YYYY-MM-DD format as per your instructions, and
even check date using checkdate(). And it returns true. But still it
is not updating my table. I am writing SQL statement like this:
$query = "insert into tbl_master(dob) values($dob)";
Now what can be the wrong?
Nimish

Jul 17 '05 #4

nimish wrote:
Thanks very much Chris.

I changed output in YYYY-MM-DD format as per your instructions, and
even check date using checkdate(). And it returns true. But still it
is not updating my table. I am writing SQL statement like this:
$query = "insert into tbl_master(dob) values($dob)";


Does the call to mysql_query() return any errors?

Try to do the query like this:

$query = "insert into tbl_master set dob='" . $dob . "'";
$result = @mysql_query($query);
if (!$result) echo "There's a problem with the
insert<br>".mysql_error().'<br>Query:'.$query."<br >\n";

Ken

Jul 17 '05 #5

Ken,
I am not getting any error in SQL execution. I get 0000-00-00 00:00:00
in this field every time, which is a default value. Actually query is
not getting fail, as I am seeing other fields getting populated.
Nimish

Jul 17 '05 #6

Print the query to see what is being sent to MYSQL:

....
$query = "insert into tbl_master(dob) values($dob)";
print "DEBUG: $query\n";
....

If the result looks like this:

DEBUG: insert into tbl_master(dob) values(2005-01-11)

the problem is the way you have formatted the date. Try this*:

....
$dob = "$Year-$Month-$Day";
$query = "insert into tbl_master(dob) values(\"$dob\")";
....

....or this*:

....
$dob = "$Year$Month$Day";
$query = "insert into tbl_master(dob) values($dob)";
....

(*example code only: naturally you will also take into account the
comments about not trusting user input)

---
Steve

Jul 17 '05 #7
Great!!
....
$dob = "$Year-$Month-$Day";
$query = "insert into tbl_master(dob) values(\"$dob\")";
This works.
Thanks a lot.

Jul 17 '05 #8
.oO(nimish)
$dob = "$Year-$Month-$Day";
$query = "insert into tbl_master(dob) values(\"$dob\")";


Should be

$query = "insert into tbl_master(dob) values('$dob')";

Strings are enclosed with single quotes in SQL. Using double quotes is a
proprietary MySQL feature.

Another question: Where do $Year, $Month and $Day come from? I hope (for
you and your db) you don't use user-submitted values directly without
validation in a database query. If these values are submitted by a form
access them with one of the arrays $_GET or $_POST (dependent on the
used submission method) and make sure they contain allowed values.

Micha
Jul 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: praba kar | last post by:
Dear All, I have doubt regarding date string to time conversion function. In Python I cannot find flexible date string conversion function like php strtotime. I try to use following type...
1
by: Riley DeWiley | last post by:
I have an UPDATE query that is always setting 0 records. When I cut and paste the SQL into Access and use it, it fails in the same way unless I coerce the date fields to be '=now()', in which case...
1
by: Franck | last post by:
Hi, 'm gettin mad about date conversion. Here is the point. Got and add-in for Excel which call functions from a web service (on a remote server) The remote server has regional settings set...
10
by: melissa.nava | last post by:
Here is my code: ***** Public Property DOB() As Date Get Try DOB = (msBirthMonth + "/" + msBirthDay + "/" + msBirthYear)
8
by: no_spam_for_gman | last post by:
Hi, Has anybody ever created a DB2 date function to convert a JDEdwards date to a db2 date? Example: September 28 is 106271 in JDEdwards.
44
by: user | last post by:
Hi, Let's say I have 2 dates in the b/m format: Date 1 and date 2 How do I check whether Date2 is later than Date 1? Date1. 21-Nov-2006 09:00:00 PM
10
by: =?Utf-8?B?TWlrZQ==?= | last post by:
I have a string variable containing a date formatted as YYYYMMDD For example - Dim x as string = "20070314" If I try to perform a type conversion as follows I get an error: Dim y as Date =...
1
by: vadala | last post by:
The requirement is to send start data and end date from java to UI (.net) for a functionality. We are setting the time in java (1.5) before handing it over to WebService (sending to UI ) in...
7
by: bruce.dodds | last post by:
Access seems to be handling a date string conversion inconsistently in an append query. The query converts a YYYYMM string into a date, using the following function: CDate(Right(,2) & "/1/" &...
2
by: RN1 | last post by:
A TextBox displays the current date (in dd/mm/yyyy format) & time when a user comes to a page (e.g. 15/10/2008 1:36:39 PM). To convert the date into international format so that the remote server...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.