473,756 Members | 6,250 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Php -mysql date problem

Hello,

I'm new to php and mysql and I use Dreamweaver MX 2004, so sorry for this
"newbie" question... I've found no answer in the forum ...
I've a date problem with my formular. In my mysql DB my filed "date" in
table "experience " is like this: Y-m-d (2002-07-23).
My fied`date` is date, NOT NULL with no default entry

My form read well the date data depending the id, (pe. 30.02.2003), but when
I submit a new date, I receive as result in the form "30.11.1999 " and my DB
field I've now "0000-00-00"....

I don't know how to correct this... I've lost several hours and I think it's
simple...
I will appreciate any suggestion and help.

Thx for your time, regards,
Dominique

Here's the php code for my form:

<?php require_once('. ./../../Connections/metadeco_connec t.php'); ?>
<?php
function GetSQLValueStri ng($theValue, $theType, $theDefinedValu e = "",
$theNotDefinedV alue = "")
{
$theValue = (!get_magic_quo tes_gpc()) ? addslashes($the Value) : $theValue;

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValu e) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theV alue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . date("Y-d-m",strtotime($t heValue)) .
"'" : "NULL";
break;
case "time":
$theValue = ($theValue != "") ? "'" . date("H:i:s",st rtotime($theVal ue)) .
"'" : "NULL";
break;
case "datetime":
$theValue = ($theValue != "") ? "'" . date("Y-d-m
H:i:s",strtotim e($theValue)) . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValu e : $theNotDefinedV alue;
break;
}
return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_ SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] ==
"form_experienc e_edit")) {
$updateSQL = sprintf("UPDATE experience SET `date`=%s WHERE id=%s",
GetSQLValueStri ng($_POST['datum'], "date"),
GetSQLValueStri ng($_POST['hiddenField'], "int"));

mysql_select_db ($database_meta deco_connect, $metadeco_conne ct);
$Result1 = mysql_query($up dateSQL, $metadeco_conne ct) or die(mysql_error ());

$updateGoTo = "index.php" ;
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($update GoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf( "Location: %s", $updateGoTo));
}
mysql_select_db ($database_meta deco_connect, $metadeco_conne ct);
$query_rs1exper ience = "SELECT * FROM experience";
$rs1experience = mysql_query($qu ery_rs1experien ce, $metadeco_conne ct) or
die(mysql_error ());
$row_rs1experie nce = mysql_fetch_ass oc($rs1experien ce);
$totalRows_rs1e xperience = mysql_num_rows( $rs1experience) ;

// *** Move To Specific Record: declare variables
$MM_rs = &$rs1experience ;
$row_MM_rs = &$row_rs1experi ence;
$MM_rsCount = $totalRows_rs1e xperience;
$MM_uniqueCol = "id";
$MM_paramName = "id";
$MM_paramIsDefi ned = ($MM_paramName != "" &&
isset($HTTP_GET _VARS[$MM_paramName]));

// *** Move To Specific Record: handle detail parameter
if ($MM_paramIsDef ined && $MM_rsCount != 0) {
// get the value of the parameter
$param = $HTTP_GET_VARS[$MM_paramName];
// find the record with the unique column value equal to the parameter value
do {
if ($row_MM_rs[$MM_uniqueCol] == $param) break;
} while($row_MM_r s = mysql_fetch_ass oc($MM_rs));
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Metadeli c :: Portfolio Admin</title>
<link href="../../css/metastyle.css" rel="stylesheet " type="text/css">
</head>
<body>
<div align="center">
<form action="<?php echo $editFormAction ; ?>" method="POST"
name="form_expe rience_edit" id="form_experi ence_edit">
<input name="hiddenFie ld" type="hidden" value="<?php echo
$row_rs1experie nce['id']; ?>">
<table width="95%" border="0" cellspacing="5" cellpadding="3" >
<tr>
<td align="right" valign="top" bgcolor="#eeeee e">Titre <strong>FR</strong>
</td>
<td width="100%" align="left" valign="top"> <span class="skills"> <img
src="../images/arrowlink.gif" width="16" height="16" align="absmiddl e"><?php
echo $row_rs1experie nce['titre_fr']; ?></span></td>
</tr>
<tr>
<td align="right" valign="top" bgcolor="#eeeee e">Date</td>
<td align="left" valign="top">
<input name="datum" type="text" id="datum" value="<?php echo
date('Y.m.d',st rtotime($row_rs 1experience['date'])); ?>">
<br>
<img src="../images/arrowlink.gif" width="16" height="16"
align="absmiddl e"><span class="skills"> <?php echo date('d.m.Y',
strtotime($row_ rs1experience['date']));?></span></td>
</tr>
<tr>
<td align="right" valign="top" bgcolor="#eeeee e">Confirmation </td>
<td align="left" valign="top"><i nput type="submit" name="Submit"
value="Valider" >
&nbsp;&nbsp;
<input name="Reset" type="reset" value="Reset"></td>
</tr>
</table>
<input type="hidden" name="MM_update " value="form_exp erience_edit">
</form> </div>
</body>
</html>
<?php
mysql_free_resu lt($rs1experien ce);
?>

Jul 17 '05 #1
5 3734
Dominique Javet wrote:
I'm new to php and mysql and I use Dreamweaver MX 2004, so sorry for this
"newbie" question... I've found no answer in the forum ...
I've a date problem with my formular. In my mysql DB my filed "date" in
table "experience " is like this: Y-m-d (2002-07-23).
My fied`date` is date, NOT NULL with no default entry

My form read well the date data depending the id, (pe. 30.02.2003), but when
I submit a new date, I receive as result in the form "30.11.1999 " and my DB
field I've now "0000-00-00"....

I don't know how to correct this... I've lost several hours and I think it's
simple...
I will appreciate any suggestion and help.


MySQL is dumb when it comes to dates!
PHP is a little more intelligent but it doesn't compare to human
ingenuity.

So, you have to transform whatever the user typed, first into something
PHP understand, and ultimately to something MySQL understands.

I'd go about that by isolating the day, month, and year from the input;
then dealing with them appropriately.

If your users are always inserting dates as "dd.mm.yyyy " do
$input = "30.02.2003 ";

$input_day = (int)substr($in put, 0, 2); // 30
$input_month = (int)substr($in put, 3, 2); // 2
$input_year = (int)substr($in put, 6, 4); // 2003
if (checkdate($inp ut_month, $input_day, $input_year)) {
// date valid
} else {
// date invalid (the actual $input above will be invalid)
}
strtotime() accepts a whole lot of formats, but not the one you're using
http://www.php.net/strtotime

and follow the link there to "Date Input Formats".
Happy Coding :)

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #2
Pedro Graca wrote:
Dominique Javet wrote:
I'm new to php and mysql and I use Dreamweaver MX 2004, so sorry for
this "newbie" question... I've found no answer in the forum ...
I've a date problem with my formular. In my mysql DB my filed "date"
in table "experience " is like this: Y-m-d (2002-07-23).
My fied`date` is date, NOT NULL with no default entry

My form read well the date data depending the id, (pe. 30.02.2003),
but when I submit a new date, I receive as result in the form
"30.11.1999 " and my DB field I've now "0000-00-00"....

I don't know how to correct this... I've lost several hours and I
think it's simple...
I will appreciate any suggestion and help.


MySQL is dumb when it comes to dates!
PHP is a little more intelligent but it doesn't compare to human
ingenuity.

So, you have to transform whatever the user typed, first into
something
PHP understand, and ultimately to something MySQL understands.

I'd go about that by isolating the day, month, and year from the
input;
then dealing with them appropriately.

If your users are always inserting dates as "dd.mm.yyyy " do
$input = "30.02.2003 ";

$input_day = (int)substr($in put, 0, 2); // 30
$input_month = (int)substr($in put, 3, 2); // 2
$input_year = (int)substr($in put, 6, 4); // 2003
if (checkdate($inp ut_month, $input_day, $input_year)) {
// date valid
} else {
// date invalid (the actual $input above will be invalid)
}
strtotime() accepts a whole lot of formats, but not the one you're
using http://www.php.net/strtotime

and follow the link there to "Date Input Formats".
Happy Coding :)


I would not use substr on that... For example, what if I put in today's
date? 1.5.04 - it will take the day as 1. the month as .0 and the year will
get nothing. use explode. www.php.net/explode
Jul 17 '05 #3
Hello,

Thx a lot for your advise.
But, how can I pass the result form the text field into a hidden field?
What is the javascript synthax?
Because when I first run this script, I receive errors due to the $_POST:

<input name="datum" type="text" id="datum" value="<?php echo
date('d.m.Y',st rtotime($row_rs 1experience['date'])); ?>">
<?
$date_array = split('[/.-]',$_POST["datum"]);/*This splits out if entered in
form as dd/mm/yyyy or dd-mm-yyyy*/
$date_formated = $date_array[2].$date_array[0].$date_array[1];/*This
rearranges it into db format*/
echo $date_formated;
?>
<input name="hiddenFie ld_datum" type="hidden" value="<?php echo
$date_formated; ?>">

Damn.. is so difficult for a newbie like me ;o)

A lot of thx for yoru help and time.
Regards, Dom
Jul 17 '05 #4
I noticed that Message-ID: <c7************ @ID-209842.news.uni-berlin.de>
from Dominique Javet contained the following:
<input name="datum" type="text" id="datum" value="<?php echo
date('d.m.Y',s trtotime($row_r s1experience['date'])); ?>">
<?
$date_array = split('[/.-]',$_POST["datum"]);/*This splits out if entered in
form as dd/mm/yyyy or dd-mm-yyyy*/
$date_format ed = $date_array[2].$date_array[0].$date_array[1];/*This
rearranges it into db format*/
echo $date_formated;
?>


I've done it this way before but I wouldn't bother now. I'd offer the
user a series of drop down boxes. No typing involved so the user cannot
screw up the input. Then I convert that to a unix timestamp (using
strtotime() or mktime() ) and store that as an integer. Easy.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #5
Hello,

Thx to everybody for your help!
I appreciate.

Regards, Dom

Jul 17 '05 #6

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

Similar topics

2
32426
by: Ben | last post by:
I would like to use php to query a database and retrieve a unix timestamp. The problem is that mysql is storing the data in the date format and not a timestamp. I am sure that I can amend my query to format the date returned as a timestamp without having to do the conversion in php. Can someone tell me what to put in my db query?
3
3328
by: Noyb | last post by:
How do I convert a string into a value I can upload to mysql date field. Specifically, I have a user select a month, day and year from drop-downs, then I want them submitted to one date field. Thanks! Steve.
0
2149
by: Peter Ruijter | last post by:
Hello, I've read and used the birthday solutions of Wolfgang Führer from the mySQL DATE examples on the mysql.com website. SELECT naam, gebdatum FROM users WHERE gebdatum != '0000-00-00' AND (( DAYOFYEAR(gebdatum) < DAYOFYEAR(now()) )*366)+DAYOFYEAR(gebdatum) >= DAYOFYEAR(now())ORDER BY (( DAYOFYEAR(gebdatum) < DAYOFYEAR(now()) )*366)+DAYOFYEAR(gebdatum)
3
1584
by: amerar | last post by:
Hi All, I want to convert a date...... I am given the Day of the Week, plus the Hour and Minute. I want to convert that into a YYYY-MM-DD HH:MI format so I can store it and sort it in the database.
1
2254
by: jimmy | last post by:
I'm trying to insert a date into a MySQL date column. The string i am trying to insert takes the following format: 2007-02-23 which corresponds to the date format that MySQL uses which is YYYY-MM- DD. When i insert this into the database through an INSERT query however the date field uses the value 0000-00-00. Why might this be? Thanks
6
3162
by: kyandebishop | last post by:
Hello everybody. I took database course two years ago and I am having a serious problem. I created a table called pet with mySQL 5.0 and it has 6 fields. Among them are two fields for birth Date and death Date. When inserting the data I have one pet born today. I am trying to query the pet who was born today but it is not working. Here is my syntex SELECT * FROM pet WHERE birth Date = CURRENT_DATE( ) ; I get a syntex error even...
2
2882
osward
by: osward | last post by:
Hello there, I am using phpnuke 8.0 to build my website, knowing little on php programing. I am assembling a module for my member which is basically cut and paste existing code section of various module that I found it useful. Here is the 1st problem I encounter: I had a function to edit a event row form the database which is fine with me, than I pass on the code to a function that save(update) the data to the database.
2
5646
by: tariquetuku | last post by:
Can anyone plz advice, how to change mysql date format to (d-m-Y) permanently which will affect all the databases in the mysql server. i.e, i want to change the date format in one place which will affect all the database's date format automaticly? Is there any way out? Or code?
12
3091
by: AmiMitra | last post by:
i am using asp.net......i have a textbox where i am inserting a date in the format 'dd/mm/yyyy' . i have to insert this data into database. but mysql date format is 'yyyy-mm-dd'...how to do that? please help...
0
9303
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
9894
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
9541
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8542
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7078
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
4955
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...
1
3651
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3141
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2508
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.