|
Posted by Thomas 'PointedEars' Lahn on 10/27/07 17:11
Chris Roy-Smith wrote:
> Jes wrote:
>> I have a date field on a HTML form where the user is asked to key in
>> dd/mm/yyyy
Bad idea.
>> 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
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
[Back to original message]
|