CodeCharge Studio
search Register Login  

Visual PHP Web Development

Visually Create Internationalized Web Applications, Web Reports, Calendars, and more.
CodeCharge.com

YesSoftware Forums -> Archive -> GotoCode Archive

 Format Date w/PHP CC Both to view & put back in DB

Print topic Send  topic

Author Message
DeWebDude
Posted: 06/12/2003, 6:43 AM

I am having no problem calling a small function I wrote and using the before show event to display the MySQL date in Human format on tables, but when the user inputs the date I need to convert it back and put it into mysql format.
That is not working.
To get from DB in before show I do this:
$fldrequested_date = f_mysql_date_2_human($fldrequested_date,"F");

To put it into db I do this:
I tried this in before show insert and before insert
$fldrequested_date = f_mysql_date_2_human($fldrequested_date,"T");

( F = From DB .. T = To DB )

Function looks like this:
function f_mysql_date_2_human($date_value,$direction) {
//intercept mysql's default ZERO date value.
// if ($date_value=="0000-00-00") return "Blank";
if ($direction == "F" ) {
$year=strval(substr($date_value,0,4));
$month=strval(substr($date_value,6,2));
$day=strval(substr($date_value,9,2));
return date("m/d/Y", mktime (0,0,0,$month,$day,$year));
} else {
$year=strval(substr($date_value,7,4));
$month=strval(substr($date_value,0,2));
$day=strval(substr($date_value,4,2));
return date("Y/m/d", mktime (0,0,0,$month,$day,$year));
}

Any help appreciated....
RipCurl
Posted: 06/12/2003, 1:08 PM

Assuming that your "f" results will return a "normal" date of
MM-DD-YYYY
And you want to convert it back to YYYY-MM-DD for the db?

try something like this:

// $date = "04-06-2003"; // old-date format
list( $mm, $dd, $yy ) = split( "-", $date );
$newdate = "$yy-$mm-$dd";
// echo $newdate; /// echo out new date format.
RipCurl
Posted: 06/12/2003, 3:15 PM

Looking at what you have, you can simplify it better. The following code was taken out of a tips article here at gotocode:

// set up your format of dates from MySQL to a format you like

function date_format($old_date, $format)
{
$res = "";
$day = dLookUp("blog_blog", "DATE_FORMAT('$old_date', '%e')", "1=1");
$month = dLookUp("blog_blog", "MONTH('$old_date')", "1=1");
$year = dLookUp("blog_blog", "YEAR('$old_date')", "1=1");
switch ($format)
{
case 1:
$res = date("d/m/Y", mktime($hour,$minutes,$second, $month, $day, $year));
break;
case 2:
$res = date("H:i a", mktime($hour,$minutes,$second, $month, $day, $year));
break;
case 3:
$res = date("m/d/y", mktime($hour,$minutes,$second));
break;
}
return $res;
}

// end formating function


You can place this in your Modules > Global Functions of your project.
OR, if you just want to apply to one page, in the OPEN Event of the page.
This will set=up the format for which you want your dates displayed. You can add more cases as you like, or just narrow it down to one format you like to use on all your pages.


Before Show event of your Grid/Record:

$flddate = date_format($flddate, 3);


$flddate will of course be what you are using to show your date ( $fldrequested_date i guess )


Now, if you wanna change someone's input back to MySQL format, you have to be sure that they will be inputing the date the way you want it (ie HAS to be MM-DD-YYYY ) otherwise, there are more steps for you to do if they dont.

1. if they are inputing it like mm-dd-yyyy

in the Before Insert you can do (this also works in Before Update as well):

list( $mm, $dd, $yy ) = split( "-", $fldrequested_date );
$fldrequested_date = "$yy-$mm-$dd";

2. if they put it in any format not like above, then you'll have to do some extra steps. Look into strtotime() on how to change an "english" term of time/date into UNIX time (ie March 3, 2003 , 6 June 2003, etc), then you can then take the UNIX result and put it into MySQL time.

   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Internet Database

Visually create Web enabled database applications in minutes.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.