from MM/DD/YYYY to YYYY-MM-DD 
Author Message
 from MM/DD/YYYY to YYYY-MM-DD

I was wondering if anyone would be able to explain to me (or point me in the
direction of any info on) how to convert a date from MM/DD/YYYY to
YYYY-MM-DD. I have a form asking users to enter a date in the format
MM/DD/YYYY but the data needs to go into a MySQL database in the format:
YYYY-MM-DD.

Thanks,
Kerri



Mon, 24 Oct 2005 05:15:16 GMT  
 from MM/DD/YYYY to YYYY-MM-DD


Quote:
> I was wondering if anyone would be able to explain to me (or point me in the
> direction of any info on) how to convert a date from MM/DD/YYYY to
> YYYY-MM-DD. I have a form asking users to enter a date in the format
> MM/DD/YYYY but the data needs to go into a MySQL database in the format:
> YYYY-MM-DD.

> Thanks,
> Kerri

there is probably a way better way to do this -- but this is what I use --

//
//accept American date format (mm/dd/yy or mm-dd-yy) and return MySQL format
(yyyy-mm-dd)
//will accept m or d instead of mm or dd
//assumes all dates are this century unless given yyyy instead of yy
function american2mysql($americandatestring){
 if (strlen($americandatestring)==0){return "";}
 $pos = strpos($americandatestring, "/");
 if (! $pos === false){$datepartarray = explode("/", $americandatestring);}

 $pos = strpos($americandatestring, "-");
 if (! $pos === false){$datepartarray = explode("-", $americandatestring);}

 if (strlen($datepartarray[0]) == 1){$datepartarray[0]= "0".$datepartarray[0];}
 if (strlen($datepartarray[1]) == 1){$datepartarray[1]= "0".$datepartarray[1];}
 if (strlen($datepartarray[2]) == 2){$datepartarray[2]= "20".$datepartarray[2];}
 $mysqlstring = $datepartarray[2]."-".$datepartarray[0]."-".$datepartarray[1];
 return $mysqlstring;

Quote:
}



Mon, 24 Oct 2005 05:32:34 GMT  
 from MM/DD/YYYY to YYYY-MM-DD
Change your form.


Quote:
> I was wondering if anyone would be able to explain to me (or point me in
the
> direction of any info on) how to convert a date from MM/DD/YYYY to
> YYYY-MM-DD. I have a form asking users to enter a date in the format
> MM/DD/YYYY but the data needs to go into a MySQL database in the format:
> YYYY-MM-DD.

> Thanks,
> Kerri



Mon, 24 Oct 2005 05:26:48 GMT  
 from MM/DD/YYYY to YYYY-MM-DD


Quote:
> I was wondering if anyone would be able to explain to me (or point me in the
> direction of any info on) how to convert a date from MM/DD/YYYY to
> YYYY-MM-DD. I have a form asking users to enter a date in the format
> MM/DD/YYYY but the data needs to go into a MySQL database in the format:
> YYYY-MM-DD.

> Thanks,
> Kerri

you might want this one too --

//
//accept MySQL date format (yyyy-mm-dd) and return Amercian date format
(mm/dd/yy)
// will accept a datetime sting, but truncates the time
function mysql2american($mysqldatestring){
 if (strlen($mysqldatestring)==0){return "";}
 $mysqldatestring = substr($mysqldatestring,0,10); //just the date portion
 $datepartarray = explode("-",$mysqldatestring);
 $americanstring =
$datepartarray[1]."/".$datepartarray[2]."/".substr($datepartarray[0],2,2);
 return $americanstring;

Quote:
}



Mon, 24 Oct 2005 05:36:19 GMT  
 from MM/DD/YYYY to YYYY-MM-DD
There is a very simple way to do this:

$phpdate = "05/08/2003";

list($Year,$Month,$Day) = split("/",$phpdate);
$mysqldate = "$Year-$Month-$Day";

Cheers
Hamilton

----------------------------------------------------------------------------
-------------------------------------------

www.laughland.biz
"the web site for web sites"

We specialise in php/mysql programming.  Low rates, great service.

----------------------------------------------------------------------------
-------------------------------------------


Quote:
> I was wondering if anyone would be able to explain to me (or point me in
the
> direction of any info on) how to convert a date from MM/DD/YYYY to
> YYYY-MM-DD. I have a form asking users to enter a date in the format
> MM/DD/YYYY but the data needs to go into a MySQL database in the format:
> YYYY-MM-DD.

> Thanks,
> Kerri



Mon, 24 Oct 2005 06:02:10 GMT  
 from MM/DD/YYYY to YYYY-MM-DD
Oops should be:

list($Month,$Day,$Year) = split("/",$phpdate);

Forgot to change the Split parameters around  when I copied this line from
my script! :}

should be

Quote:
> There is a very simple way to do this:

> $phpdate = "05/08/2003";

> list($Year,$Month,$Day) = split("/",$phpdate);
> $mysqldate = "$Year-$Month-$Day";

> Cheers
> Hamilton

> --------------------------------------------------------------------------
--
> -------------------------------------------

> www.laughland.biz
> "the web site for web sites"

> We specialise in php/mysql programming.  Low rates, great service.

> --------------------------------------------------------------------------
--
> -------------------------------------------



> > I was wondering if anyone would be able to explain to me (or point me in
> the
> > direction of any info on) how to convert a date from MM/DD/YYYY to
> > YYYY-MM-DD. I have a form asking users to enter a date in the format
> > MM/DD/YYYY but the data needs to go into a MySQL database in the format:
> > YYYY-MM-DD.

> > Thanks,
> > Kerri



Mon, 24 Oct 2005 06:05:58 GMT  
 from MM/DD/YYYY to YYYY-MM-DD
Wow. Thanks, everyone! It is so kind of you to help me; I really appreciate
it.

Respectfully yours,
Kerri



Mon, 24 Oct 2005 07:01:30 GMT  
 from MM/DD/YYYY to YYYY-MM-DD
[change date format from MM/DD/YYYY to YYYY-MM-DD]

Looks like just another perfect case for RegExps:

//original date is in $dateold

$datenew = ereg_replace("([0-9]{2})[/\-]([0-9]{2})[/\-]([0-9]{2,4})",
      "\\3-\\1-\\2", $dateold);
if ($datenew != $dateold) {
  //input was correct, new date is in $datenew

Quote:
} else {

  //input was not in expected format
Quote:
}



Mon, 24 Oct 2005 09:04:24 GMT  
 from MM/DD/YYYY to YYYY-MM-DD
I second this as you're assuming that everyone uses mm/dd/yyyy where as
yyyy-mm-dd is an international standard.
Quote:

> Change your form.



>>I was wondering if anyone would be able to explain to me (or point me in

> the

>>direction of any info on) how to convert a date from MM/DD/YYYY to
>>YYYY-MM-DD. I have a form asking users to enter a date in the format
>>MM/DD/YYYY but the data needs to go into a MySQL database in the format:
>>YYYY-MM-DD.

>>Thanks,
>>Kerri



Mon, 24 Oct 2005 17:31:31 GMT  
 from MM/DD/YYYY to YYYY-MM-DD


Quote:
> I second this as you're assuming that everyone uses mm/dd/yyyy where as
> yyyy-mm-dd is an international standard.


> > Change your form.



> >>I was wondering if anyone would be able to explain to me (or point me in

> > the

> >>direction of any info on) how to convert a date from MM/DD/YYYY to
> >>YYYY-MM-DD. I have a form asking users to enter a date in the format
> >>MM/DD/YYYY but the data needs to go into a MySQL database in the format:
> >>YYYY-MM-DD.

> >>Thanks,
> >>Kerri

If you want a routine that will take a date that has been entered by the
user in one of several possible formats then take a look at the code that I
use (after my signature):

Tony Marston

<?php
//**************************************************************************
***

// Distributed under the GNU General Public Licence
//**************************************************************************
***

class DateClass
{
 // private variables
 var $monthalpha;  // array of 3-character month names
 var $internaldate;   // date as held in the database (yyyymmdd)
 var $externaldate;   // date as shown to the user (dd Mmm yyyy)
 var $errors;         // error messages

//
****************************************************************************
// class constructor
//
****************************************************************************
   function DateClass ()
 {
    $this->monthalpha =
array(1=>'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','
Dec');

 } // DateClass

//
****************************************************************************
// accessor functions
//
****************************************************************************
 function getInternalDate ($input)
 // convert date from external format (as input by user)
 // to internal format (as used in the database)
 {

  // look for d(d)?m(m)?y(yyy) format
  $pattern = '(^[0-9]{1,2})'    // 1 or 2 digits
            .'([^0-9a-zA-Z])'   // not alpha or numeric
          .'([0-9]{1,2})'     // 1 or 2 digits
          .'([^0-9a-zA-Z])'   // not alpha or numeric
            .'([0-9]{1,4}$)';   // 1 to 4 digits

  if (ereg($pattern, $input, $regs)) {
   $result = $this->verifyDate($regs[1],$regs[3],$regs[5]);
   return $result;
  } // if

  // look for d(d)?MMM?y(yyy) format
  $pattern = '(^[0-9]{1,2})'    // 1 or 2 digits
            .'([^0-9a-zA-Z])'   // not alpha or numeric
          .'([a-zA-Z]{1,})' // 1 or more alpha
          .'([^0-9a-zA-Z])'   // not alpha or numeric
            .'([0-9]{1,4}$)';   // 1 to 4 digits

  if (ereg($pattern, $input, $regs)) {
   $result = $this->verifyDate($regs[1],$regs[3],$regs[5]);
   return $result;
  } // if

  // look for d(d)MMMy(yyy) format
  $pattern = '(^[0-9]{1,2})'    // 1 or 2 digits
          .'([a-zA-Z]{1,})' // 1 or more alpha
            .'([0-9]{1,4}$)';   // 1 to 4 digits

  if (ereg($pattern, $input, $regs)) {
   $result = $this->verifyDate($regs[1],$regs[2],$regs[3]);
   return $result;
  } // if

  // look for MMM?d(d)?y(yyy) format
  $pattern = '(^[a-zA-Z]{1,})' // 1 or more alpha
            .'([^0-9a-zA-Z])'   // not alpha or numeric
          .'([0-9]{1,2})'     // 1 or 2 digits
          .'([^0-9a-zA-Z])'   // not alpha or numeric
            .'([0-9]{1,4}$)';   // 1 to 4 digits

  if (ereg($pattern, $input, $regs)) {
   $result = $this->verifyDate($regs[3],$regs[1],$regs[5]);
   return $result;
  } // if

  // look for MMMddyyyy format
  $pattern = '(^[a-zA-Z]{1,})' // 1 or more alpha
          .'([0-9]{2})'     // 2 digits
            .'([0-9]{4}$)';    // 4 digits

  if (ereg($pattern, $input, $regs)) {
   $result = $this->verifyDate($regs[2],$regs[1],$regs[3]);
   return $result;
  } // if

  // look for yyyy?m(m)?d(d) format
  $pattern = '(^[0-9]{4})'     // 4 digits
            .'([^0-9a-zA-Z])'   // not alpha or numeric
          .'([0-9]{1,2})'     // 1 or 2 digits
          .'([^0-9a-zA-Z])'   // not alpha or numeric
            .'([0-9]{1,2}$)';   // 1 to 2 digits

  if (ereg($pattern, $input, $regs)) {
   $result = $this->verifyDate($regs[5],$regs[3],$regs[1]);
   return $result;
  } // if

    // look for ddmmyyyy format
  $pattern = '(^[0-9]{2})'     // 2 digits
          .'([0-9]{2})'     // 2 digits
            .'([0-9]{4}$)';    // 4 digits

  if (ereg($pattern, $input, $regs)) {
   $result = $this->verifyDate($regs[1],$regs[2],$regs[3]);
   return $result;
  } // if

  // look for yyyy?MMM?d(d) format
  $pattern = '(^[0-9]{4})'     // 4 digits
            .'([^0-9a-zA-Z])'   // not alpha or numeric
          .'([a-zA-Z]{1,})' // 1 or more alpha
          .'([^0-9a-zA-Z])'   // not alpha or numeric
            .'([0-9]{1,2}$)';   // 1 to 2 digits

  if (ereg($pattern, $input, $regs)) {
   $result = $this->verifyDate($regs[5],$regs[3],$regs[1]);
   return $result;
  } // if

  $this->errors = 'This is not a valid date';
      return FALSE;

 } // getInternalDate

//
****************************************************************************
 function getInternalTime ($input)
 // convert time from external format (as input by user)
 // to internal format (as used in the database)
 {
      // look for HH?MM?SS format
      $pattern = '(^[0-9]{2})'     // 2 digits
            .'([^0-9a-zA-Z])'   // not alpha or numeric
          .'([0-9]{2})'       // 2 digits
          .'([^0-9a-zA-Z])'   // not alpha or numeric
            .'([0-9]{2}$)';     // 2 digits

  if (ereg($pattern, $input, $regs)) {
   $result = $this->verifyTime($regs[1],$regs[3],$regs[5]);
   return $result;
  } // if

      // look for HHMMSS format
      $pattern = '(^[0-9]{2})'     // 2 digits
          .'([0-9]{2})'       // 2 digits
          .'([0-9]{2}$)';     // 2 digits

  if (ereg($pattern, $input, $regs)) {
   $result = $this->verifyTime($regs[1],$regs[2],$regs[3]);
   return $result;
  } // if

      $this->errors = 'This is not a valid time';
      return FALSE;

   } // getInternalTime

//
****************************************************************************
 function verifyDate($day, $month, $year)
 {

  // convert alpha month to digits
  if (eregi('([a-z]{3})', $month)) {
     $month = ucfirst(strtolower($month));
   if (!$month = array_search($month, $this->monthalpha)) {
      $this->errors = 'Month name is invalid';
      return FALSE;
   } // if
  } // if

  // ensure that year has 4 digits
  if (strlen($year) == 1) {
     $year = '200' .$year;
  } // if
  if (strlen($year) == 2) {
     $year = '20' .$year;
  } // if
  if (strlen($year) == 3) {
     $year = '2' .$year;
  } // if

  if (!checkdate($month, $day, $year)) {
   $this->errors = 'This is not a valid date';
   return FALSE;
  } else {
   if (strlen($day) < 2) {
      $day = '0' .$day;  // add leading zero
   } // if
   if (strlen($month) < 2) {
      $month = '0' .$month; // add leading zero
   } // if
   $this->internaldate = $year .'-' .$month .'-' .$day;
   return $this->internaldate;
  } // if

      return;

 } // verifyDate

//
****************************************************************************
 function verifyTime($hours, $minutes, $seconds)
 {

  if ($hours > 24) {
     $this->errors = 'Invalid HOURS';
   return FALSE;
  } // if

      if ($minutes > 59) {
     $this->errors = 'Invalid MINUTES';
   return FALSE;
  } // if

      if ($minutes > 59) {
     $this->errors = 'Invalid MINUTES';
   return FALSE;
  } // if

      return "$hours:$minutes:$seconds";

 } // verifyTime

//
****************************************************************************

   function getExternalDate ($input)
 // convert date from internal format (as used in the database)
 // to external format (as shown to the user))
 {
    $monthalpha = $this->monthalpha;

    // input may be 'yyyy-mm-dd' or 'yyyymmdd', so
    // check the length and process accordingly
    $len = strlen($input);

      if (strlen($input) == 8) {
     // test for 'yyyymmdd'
     $pattern = '(^[0-9]{4})' // 4 digits (yyyy)
             .'([0-9]{2})'    // 2 digits (mm)
             .'([0-9]{2}$)'; // 2 digits (dd)
     if (ereg($pattern, $input, $regs)) {
        if (!checkdate($regs[2], $regs[3], $regs[1])) {
           $this->errors = 'This is not a valid date';
         return FALSE;
        } else {
         $monthnum = (int)$regs[2];
           $this->externaldate = "$regs[3] $monthalpha[$monthnum] $regs[1]";
         return $this->externaldate;
        } // if
     } // if
     $this->errors = "Invalid date format: expected 'yyyymmdd";
     return FALSE;
      } // if

    if (strlen($input) == 10) {
     // test for 'yyyy-mm-dd'
     $pattern = '(^[0-9]{4})' // 4 digits (yyyy)
             .'([^0-9])'  // not a digit
         .'([0-9]{2})' // 2 digits (mm)
         .'([^0-9])'  // not a digit
         .'([0-9]{2}$)'; // 2 digits (dd)
     if (ereg($pattern, $input, $regs)) {
      if (!checkdate($regs[3], $regs[5], $regs[1])) {
         $this->errors = 'This is not a valid date';
       return FALSE;
      } else {
         $monthnum = (int)$regs[3];
         $this->externaldate = "$regs[5] $monthalpha[$monthnum] $regs[1]";
       return $this->externaldate;
      } // if
     } // if
     $this->errors = "Invalid date format: expected 'dd-mm-yyyy'";
     return FALSE;
    } // if

    $this->errors = 'This is not a valid date';
      return $input;

 } // getExternalDate

//
****************************************************************************
 function addDays($internaldate, $days)
 // add a number of days (may be negative) to $internaldate (YYYY-MM-DD)
 // and return the result in the same format
 {

      // ensure date is in internal format
      $internaldate = $this->getInternalDate($internaldate);

  // convert to
...

read more »



Mon, 24 Oct 2005 18:15:08 GMT  
 from MM/DD/YYYY to YYYY-MM-DD

Quote:



> > I second this as you're assuming that everyone uses mm/dd/yyyy where as
> > yyyy-mm-dd is an international standard.


> > > Change your form.



> > >>I was wondering if anyone would be able to explain to me (or point me
in

> > > the

> > >>direction of any info on) how to convert a date from MM/DD/YYYY to
> > >>YYYY-MM-DD. I have a form asking users to enter a date in the format
> > >>MM/DD/YYYY but the data needs to go into a MySQL database in the
format:
> > >>YYYY-MM-DD.

> > >>Thanks,
> > >>Kerri

> If you want a routine that will take a date that has been entered by the
> user in one of several possible formats then take a look at the code that
I
> use (after my signature):

> Tony Marston

> <?php

<lots of code snipped>

Quote:
> ?>

Or you could just use:

<? $mysql_date = substr($in_date, 6, 4).'-'.substr($in_date, 3,
2).'-'.substr($in_date, 0,2) ?>

Gary

--
Do not meddle in the affairs of dragons, because you are
crunchy and taste good with ketchup.
http://garypigott.net



Mon, 24 Oct 2005 20:26:55 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Test yyyy,mm,dd dates with JULIAN program...

2. routine to convert date format 0CYYDDDF to format DD/MM/YY or DDMMYY

3. Display DD/MM picture for a field

4. how can I read a date (MM-DD-YY) via Topspeed ODBC

5. How to display date from unix format to dd/mm/yy format

6. Any support for European (DD/MM/YY) dates?

7. Obtaining System Date in yyyy/mm/dd in COBOL/400 ??? Please Help !!!

8. Q: getting date & time with g77 : (eg yyyy-mmm-dd, hh:mm:ss )

9. Win32 Nimda.A@mm (dr) and Win32 Nimda.E@mm (dr)

10. YYYY Date Format

11. How to Get File Modify Time in MM/DD/YY Format?

12. WeX DD 1.1.2 released

 

 
Powered by phpBB® Forum Software