473,388 Members | 1,383 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,388 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 2169
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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...
0
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...

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.