Reply to flexible mysql/human date conversion

Your name:

Reply:


Posted by Jim Michaels on 12/17/05 07:01

the bottom function was derived from the manual of course.
date2mysql() handles year-only, month-and-day only, and full date specs.
it also handles 2-digit years, filling them out as year 2000+.

function date2mysql($d) {
//convert USA style mm/dd/yy date to MySQL DB-friendly yyyy-mm-dd
if (strchr($d,"/")) {
$dt=explode("/",$d);
switch(count($dt)) {
case 3: //specified all 3.
if (strlen($dt[2])==2) {$dt[2]="20$dt[2]";} //make it a 4-digit year
2000+ thing
$d="$dt[2]-$dt[0]-$dt[1]"; break;
case 2: //they left the year out
$today = getdate();
$year=$today['year'];
$d="$year-$dt[0]-$dt[1]";
break;
case 1: //year only
if (strlen($dt[0])==2) {$dt[0]="20$dt[0]";} //make it a 4-digit year
2000+ thing
$d="$dt[0]-01-01"; break;
default: $d="2001-01-01"; break; //junk input. could be filled in with
today $d=date("Y-n-j");
}
}
return $d;
}

return $d;
}

function mysql2date($d) {
//convert MySQL date format yyyy-mm-dd to USA style mm/dd/yyyy
return date("n/j/Y",strtotime($d));
}

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация