Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP format date for MySQL

Status
Not open for further replies.

ljCharlie

IS-IT--Management
Apr 21, 2003
397
0
0
US
I need help on formating a date variable of DD-MM-YYYY into YYYY-MM-DD and must be in numbers. Currently in my .shtml file I have a script that will format the date textbox form to DD-MM-YYYY but I want, say, 05-Jun-2003 into 2003-06-05 so I can insert into MySQL in my .php file.

Many thanks in advance!

ljCharlie
 
try inserting it with this value itself 05-Jun-2003, i think mysql will automatically convert it to its date format.

not quite sure, but just try.

Known is handfull, Unknown is worldfull
 
I've found this procedure and use it into my programs:

<?
/*
Function Name: date_validate

Author: Eric Sammons, Vansam Software, Inc. ( Email: eric@vansam.com

Date: 2001-09-01
Version 1.0.0

Purpose:
Receives various date field formats, validates them, and then and
converts them to MySQL standard date format

Valid date fields:
mm-dd-yyyy, mm/dd/yyyy, yyyy-mm-dd, yyyy/mm/dd

Returns:
if valid input, the date in MySQL standard format
if invalid input, error message with &quot;Error:&quot; at the beginning of message

Sample Use:
$MySQLDate=date_validate($datefield);
if (substr($MySQLDate, 0, 5)==&quot;Error&quot;) {
// Insert Error Code
} else {
// Insert Valid Date Code
}
*/

function date_validate ($datefield) {

// First check to see if the input ($datefield) is in one of the accepted formats

// Check for delimiters (&quot;-&quot; or &quot;/&quot;) and put three fields into an array
if (strpos($datefield, &quot;-&quot;)) {
$datesplit = explode(&quot;-&quot;, $datefield);
} elseif (strpos($datefield, &quot;/&quot;)) {
$datesplit = explode(&quot;/&quot;, $datefield);
} else {
$date_err=&quot;Error: Invalid date field. No proper delimiters (- or /) found&quot;;
return $date_err;
}

// Check for three input fields (month, day, year)
if (count($datesplit)>3) {
$date_err=&quot;Error: Invalid date field. Too many fields (&quot;.count($datesplit).&quot;) found&quot;;
return $date_err;
}

// Put date array into single format
if (strlen($datesplit[2])==4) { // The year is listed last - switch fields around
$newdatesplit[0]=$datesplit[2]; // Move Year to first field
$newdatesplit[1]=$datesplit[0]; // Move Month to second field
$newdatesplit[2]=$datesplit[1]; // Move Day to third field
$datesplit=$newdatesplit;
} elseif (strlen($datesplit[0])==4) { // The year is first listed - do nothing
// nothing to be done
} else { // Date entered is not valid; could not find year field
$date_err=&quot;Error: Date not valid. No Year field found (Year must be 4 digits)&quot;;
return $date_err;
}

// Main validation code

if ($datesplit[1]>12) { // No valid month field
$date_err=&quot;Error: Invalid Month field (&quot;.$datesplit[1].&quot;) &quot;;
return $date_err;
} else {
switch ($datesplit[1]) { // Check number of days in a month
case 4:
case 6:
case 9:
case 11:
if ($datesplit[2]>30) {
$date_err=&quot;Error: Invalid # of days (&quot;.$datesplit[2].&quot;) for month &quot;.$datesplit[1].&quot; and year &quot;.$datesplit[0];
return $date_err;
}
break;
case 2: // February Check
if (($datesplit[0]/4)==(floor($datesplit[0]/ 4))) {
if (($datesplit[0]/ 100)==(floor($datesplit[0]/100))) {
if (($datesplit[0]==1600) or ($datesplit[0]==2000) or ($datesplit[0]==2400)) {
if ($datesplit[2]>29) {
$date_err=&quot;Error: Invalid # of days (&quot;.$datesplit[2].&quot;) for month &quot;.$datesplit[1].&quot; and year &quot;.$datesplit[0];
return $date_err;
}
} else {
if ($datesplit[2]>28) {
$date_err=&quot;Error: Invalid # of days (&quot;.$datesplit[2].&quot;) for month &quot;.$datesplit[1].&quot; and year &quot;.$datesplit[0];
return $date_err;
}
}
} else {
if ($datesplit[2]>29) {
$date_err=&quot;Error: Invalid # of days (&quot;.$datesplit[2].&quot;) for month &quot;.$datesplit[1].&quot; and year &quot;.$datesplit[0];
return $date_err;
}
}
} else {
if ($datesplit[2]>28) {
$date_err=&quot;Error: Invalid # of days (&quot;.$datesplit[2].&quot;) for month &quot;.$datesplit[1].&quot; and year &quot;.$datesplit[0];
return $date_err;
}
}
break;
default:
if ($datesplit[2]>31) {
$date_err=&quot;Error: Invalid # of days (&quot;.$datesplit[2].&quot;) for month &quot;.$datesplit[1].&quot; and year &quot;.$datesplit[0];
return $date_err;
}
}
}
// Add leading zero if month or day field is only one character
if (strlen($datesplit[1])==1) {
$datesplit[1]=&quot;0&quot;.$datesplit[1];
}
if (strlen($datesplit[2])==1) {
$datesplit[2]=&quot;0&quot;.$datesplit[2];
}

// Create date field in MySQL format
$newdate=$datesplit[0].&quot;-&quot;.$datesplit[1].&quot;-&quot;.$datesplit[2];
return $newdate;

} // End date_validate function
?>

Try an open source Tsm report Tool:

 
I'm running PHP 4.3.1, and I've found strtotime() works well converting that format of date:

Code:
<?php

$date_string = &quot;05-Jun-2003&quot;;

$date = strtotime ($date_string);

print date (&quot;Y-m-d&quot;, $date);

?>

Want the best answers? Ask the best questions: TANSTAAFL!
 
Thank you all for helping me. I'll give that a try.

ljCharlie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top