473,397 Members | 2,077 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,397 software developers and data experts.

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_connect.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "",
$theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . date("Y-d-m",strtotime($theValue)) .
"'" : "NULL";
break;
case "time":
$theValue = ($theValue != "") ? "'" . date("H:i:s",strtotime($theValue)) .
"'" : "NULL";
break;
case "datetime":
$theValue = ($theValue != "") ? "'" . date("Y-d-m
H:i:s",strtotime($theValue)) . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
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_experience_edit")) {
$updateSQL = sprintf("UPDATE experience SET `date`=%s WHERE id=%s",
GetSQLValueString($_POST['datum'], "date"),
GetSQLValueString($_POST['hiddenField'], "int"));

mysql_select_db($database_metadeco_connect, $metadeco_connect);
$Result1 = mysql_query($updateSQL, $metadeco_connect) or die(mysql_error());

$updateGoTo = "index.php";
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
}
mysql_select_db($database_metadeco_connect, $metadeco_connect);
$query_rs1experience = "SELECT * FROM experience";
$rs1experience = mysql_query($query_rs1experience, $metadeco_connect) or
die(mysql_error());
$row_rs1experience = mysql_fetch_assoc($rs1experience);
$totalRows_rs1experience = mysql_num_rows($rs1experience);

// *** Move To Specific Record: declare variables
$MM_rs = &$rs1experience;
$row_MM_rs = &$row_rs1experience;
$MM_rsCount = $totalRows_rs1experience;
$MM_uniqueCol = "id";
$MM_paramName = "id";
$MM_paramIsDefined = ($MM_paramName != "" &&
isset($HTTP_GET_VARS[$MM_paramName]));

// *** Move To Specific Record: handle detail parameter
if ($MM_paramIsDefined && $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_rs = mysql_fetch_assoc($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>Metadelic :: 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_experience_edit" id="form_experience_edit">
<input name="hiddenField" type="hidden" value="<?php echo
$row_rs1experience['id']; ?>">
<table width="95%" border="0" cellspacing="5" cellpadding="3">
<tr>
<td align="right" valign="top" bgcolor="#eeeeee">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="absmiddle"><?php
echo $row_rs1experience['titre_fr']; ?></span></td>
</tr>
<tr>
<td align="right" valign="top" bgcolor="#eeeeee">Date</td>
<td align="left" valign="top">
<input name="datum" type="text" id="datum" value="<?php echo
date('Y.m.d',strtotime($row_rs1experience['date'])); ?>">
<br>
<img src="../images/arrowlink.gif" width="16" height="16"
align="absmiddle"><span class="skills"><?php echo date('d.m.Y',
strtotime($row_rs1experience['date']));?></span></td>
</tr>
<tr>
<td align="right" valign="top" bgcolor="#eeeeee">Confirmation</td>
<td align="left" valign="top"><input 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_experience_edit">
</form> </div>
</body>
</html>
<?php
mysql_free_result($rs1experience);
?>

Jul 17 '05 #1
5 3706
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($input, 0, 2); // 30
$input_month = (int)substr($input, 3, 2); // 2
$input_year = (int)substr($input, 6, 4); // 2003
if (checkdate($input_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($input, 0, 2); // 30
$input_month = (int)substr($input, 3, 2); // 2
$input_year = (int)substr($input, 6, 4); // 2003
if (checkdate($input_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',strtotime($row_rs1experience['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="hiddenField_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',strtotime($row_rs1experience['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;
?>


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
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...
3
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....
0
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...
3
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...
1
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...
6
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 ...
2
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...
2
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...
12
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?...
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
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
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...
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,...
0
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...

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.