Date field in DD/MM/YYY

J

Jes

Dear all

I have a date field on a HTML form where the user is asked to key in
dd/mm/yyyy
However, when that is written to MySql it is either not accepted or
another value is tored in the database.

Is there any way to change value of this field back to yyyymmdd format
as accepted correctly in sql. ? The change should preferably be when
user click on submit.
Does anyone know of any code/function that does this ?

I'm using PHP and HTML

Thanks
Jesmond
 
B

Bart Van der Donck

Jes said:
I have a date field on a HTML form where the user is asked to key in
dd/mm/yyyy
However, when that is written to MySql it is either not accepted or
another value is tored in the database.

Is there any way to change value of this field back to yyyymmdd format
as accepted correctly in sql. ? The change should preferably be when
user click on submit.
Does anyone know of any code/function that does this ?

alert('27/10/2007'.split('/').reverse().join(''))
 
T

Thomas 'PointedEars' Lahn

Bad idea.

Should be done server-side as already suggested.

Hi Jesmond,
I use the following functions for this task in php. This saves the user from
seeing "strange dates". Feel free to modify to suit your needs. I wrote
this code while starting out in php so it's not a slick as it could be,

Indeed it isn't. However, if the form was designed better, the conversion
was not necessary.
but it does the job, and I havn't got around to tidying it up. (If it aint
broke.......)

<?php
//this function fort making sql dates readable to non-logical date users...

function toAusDate($SQLDate){
if ($SQLDate==null){
return null;
}
$DateArray=explode("-", $SQLDate);
if ($year=="0000"){
return "Unknown";
}else{
$val = $date[2].'-'.$date[1].'-'.$date[0];
return $val;
}
}

// same as above, but handles time as well

function ToAusDateTime($SQLDateTime){
$year=substr($SQLDateTime,0,4);
$month=substr($SQLDateTime,5,2);
$day=substr($SQLDateTime,8,2);
$hour=substr($SQLDateTime,11,2);
$min=substr($SQLDateTime,14,2);
$sec=substr($SQLDateTime,17,2);
if ($year=="0000"){
return "Unknown";
}else{
$DateTime=$day.'-'.$month.'-'.$year.' '.$hour.':'.$min.':'.$sec;
}
return $DateTime;
}

/* makes date SQL friendly, note that it takes dates with fewer digits for
any portion of the date field, you may have to modify if you want different
asumptions as to what century a date belongs to
*/

function toSQLdate($AusDate){
// split date up, first find delimiter used.

if(strpos($AusDate,'-')){
$delim='-';
}else if(strpos($AusDate,'/')){
$delim='/';
}else{
$date=false;
return $date;
break 2;
}
// find first break in date
$breakPos1=strpos($AusDate,$delim);
$breakPos2=strpos($AusDate,$delim,$breakPos1+1);
$day=substr($AusDate,0,$breakPos1);
if (strlen($day)==1){
$day='0'.$day;
}
$month=substr($AusDate,$breakPos1+1,$breakPos2-$breakPos1-1);
if (strlen($month)==1){
$month='0'.$month;
}
$year=substr($AusDate,$breakPos2+1,strlen($AusDate)-$breakPos2);
if (strlen($year)==1){
$year='200'.$year;
}
if (strlen($year)==2){
if ($year>40){
$year='19'.$year;
}else{
$year='20'.$year;
}
}else if(strlen($year)<>4){
$date=false;
return $date;
break 2;
}
$date=$year.'-'.$month.'-'.$day;
return $date;
}
?>

Consider this instead:

<?php
//this function fort making sql dates readable to non-logical date users...

function toAusDate($sqlDate)
{
return toAusDateTime($sqlDate, true);
}

// same as above, but handles time as well

function toAusDateTime($sqlDateTime, $showTime = false)
{
if (intval(substr($sqlDateTime, 0, 4)) === 0)
{
return 'Unknown';
}
else
{
return date(
'd-m-Y' . ($showTime ? ' H:i:s' : ''),
strtotime($sqlDateTime));
}
}

/* makes date SQL friendly, note that it takes dates with fewer digits for
any portion of the date field, you may have to modify if you want different
asumptions as to what century a date belongs to
*/

function toSQLdate($ausDate)
{
// split date up, first find delimiter used.
$date = preg_split('/[-\/]/', $ausDate, -1, PREG_SPLIT_NO_EMPTY);

$day = $date[0];
$month = $date[1];
$year = intval($date[2]);

if ($year < 10)
{
$year += 2000;
}
else if ($year < 100)
{
if ($year > 40)
{
$year += 1900;
}
else
{
$year += 2000;
}
}
else if (strlen($year) > 4)
{
return false;
}

return date('Y-m-d', mktime(0, 0, 0, $month, $day, $year));
}
?>


X-Post & F'up2 comp.lang.php

PointedEars
 
B

Bart Van der Donck

Michael said:
Close: alert('27/10/2007'.split('/').reverse().join('-'))

I had put it in YYYYMMDD because that was the request of the original
poster (which was without the Mini). Obviously, MySQL will accept much
more than YYYYMMDD or YYYY-MM-DD. Please refer to my articles some
time ago:

http://groups.google.com/group/comp.lang.javascript/msg/ba6188acd0582e5c
http://groups.google.com/group/comp.lang.javascript/msg/bca28e20b33aec22

Of course, the initial format of '27/10/2007' should be checked before
executing split.reverse.join on it.

I join the statement of Thomas Lahn that all this should better be
done server-side.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
oglegroups.com>, Mon, 29 Oct 2007 05:24:07, Bart Van der Donck
Of course, the initial format of '27/10/2007' should be checked before
executing split.reverse.join on it.

I join the statement of Thomas Lahn that all this should better be
done server-side.

IMHO, the date should be validated (as Gregorian) client-side, so that
simple errors don't waste a transaction. Little code is needed.

Depending on the application, it may make sense to do more client-side
validation - for example, few hotels want to take bookings for past
dates or ones far ahead.

If client-side validation can ensure, or nearly ensure, that normal
users' mistakes are caught client-side, then server-side checking, or
most of it, only needs to defend against nasty attack. If the client-
side Javascript validates a date string as ISO-8601 compliant, and the
server receives instead an FFF date string, then there is no call for
the server-side code to be user-sympathetic.
 
B

Bart Van der Donck

Dr said:
IMHO, the date should be validated (as Gregorian) client-side, so that
simple errors don't waste a transaction. Little code is needed.

That may be a wise strategy.
Depending on the application, it may make sense to do more client-side
validation - for example, few hotels want to take bookings for past
dates or ones far ahead.

For this kind of date stuff I've had excellent experiences with
http://www.mattkruse.com/javascript/calendarpopup/
If client-side validation can ensure, or nearly ensure, that normal
users' mistakes are caught client-side, then server-side checking, or
most of it, only needs to defend against nasty attack. If the client-
side Javascript validates a date string as ISO-8601 compliant, and the
server receives instead an FFF date string, then there is no call for
the server-side code to be user-sympathetic.

Generally spoken, I've always been a bit reluctant in regard to big
client scripting projects. Coding at the server is often simpler and
more robust IMHO.
 
J

Jes

Dear all

I have adatefield on a HTML form where the user is asked to key in
dd/mm/yyyy
However, when that is written to MySql it is either not accepted or
another value is tored in the database.

Is there any way to change value of this field back to yyyymmdd format
as accepted correctly in sql. ? The change should preferably be when
user click on submit.
Does anyone know of any code/function that does this ?

I'm using PHP and HTML

Thanks
Jesmond

Thanks Roy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top