[thelist] PHP/MySQL Date Format Help

jsWalter jsWalter at torres.ws
Wed Aug 4 09:00:07 CDT 2004


> -----Original Message-----
> From: thelist-bounces at lists.evolt.org 
> [mailto:thelist-bounces at lists.evolt.org] On Behalf Of Rob Smith
> Sent: Wednesday, August 04, 2004 8:28 AM
> To: Thelist (E-mail)
> Subject: [thelist] PHP/MySQL Date Format Help
> 
> 
> Hi,
> 
> I had a problem last night that I thought you could help me 
> with. If you've ever worked with php/mysql and the date 
> format, you've probably seen the format like YYYY-MM-DD...


This is what I made and use.

This can be easily "fixed" to handle PHP date formats.

Hope it helps.

Walter

=============================================

  /** Method public string AmerDate2SqlDateTime ( string Sql DATETIME
format )
      * @abstract Takes American Standard Date (mm/dd/yyyy) and converts
      *           it to MySQL DATETIME format "2002-07-23"
      *
      * @name AmerDate2SqlDateTime
      * @author Walter Torres <walter at torres.ws> [with a *lot* of help!]
      *
      * @category SQL Tools
      * @uses self contained
      * @requires self contained
      * @static
      * @final
      * @access public
      *
      * @param string $strDate American Standard
      * @return string MySQL DATETIME
      *
      **/
function AmerDate2SqlDateTime ($strDate)
{
  /** Variable local bool $err
      * @var bool $err Default error state
      * @name $err
      *
      * @abstract Default error state
      *
      * @access private
      * @static
      * @since v1.0
      *
      **/
      $err = false;

      // We will be doing many levels of error checking
      // and will need to bale at any time,
      // so we do this...
      if ((strlen($strDate) >= 8) && (strlen($strDate) <= 10))
      {
        /** Variable local array $tempDate
            * @var array $tempDate Holds tore apart string as array
            * @name $tempDate
            *
            * @abstract Holds tore apart string as array
            *
            * @access private
            * @static
            * @since v1.0
            *
            **/
            $tempDate = explode('/', $strDate);

            // See if we got what we thought we should
            if (count ($tempDate) == 3)
            {
                $month = $tempDate[0] + 0;
                $daynum = $tempDate[1] + 0;
                $year = $tempDate[2] + 0;
            }
            else
                  $err = true;
      }
      else
            $err = true;

      $month   = (($month   <  10) ? '0'  . $month   : $month);
// prepend ZERO, maybe
      $daynum  = (($daynum  <  10) ? '0'  . $daynum   : $daynum);
// prepend ZERO, maybe

      if (! $err )
            // mm/dd/yyyy
            return $year. '-' . $month  . '-' . $daynum ;
      else
            return '';

}      // AmerDate2SqlDateTime ()


// convert from mysql DATETIME format "2002-07-23";
function SqlDate2AmerDate ( $strSqlDate )
{
  /** Variable local bool $err
      * @var bool $err Default error state
      * @name $err
      *
      * @abstract Default error state
      *
      * @access private
      * @static
      * @since v1.0
      *
      **/
      $err = false;

      // We will be doning many levels of error checking
      // and will need to bale at any time,
      // so we do this...
      if (strlen($strSqlDate) == 10)
      {
        /** Variable local array $tempDate
            * @var array $tempDate Holds tore apart string as array
            * @name $tempDate
            *
            * @abstract Holds tore apart string as array
            *
            * @access private
            * @static
            * @since v1.0
            *
            **/
            $tempDate = explode('-', $strSqlDate);

            // See if we got what we thought we should
            if (count ($tempDate) == 3)
            {
                $year = $tempDate[0];
                $month = $tempDate[1];
                $daynum = $tempDate[2];
            }
            else
                  $err = true;
      }
      else
            $err = true;


      if (! $err )
            // mm/dd/yyyy
            return $month . '/' . $daynum . '/' . $year;
      else
            return '';

}      // SqlDate2AmerDate ()







// convert from mysql DATETIME format "2002-07-23";
function fromSqlDate($strSqlDate)
{
      // Error checking
      $err = false;

      // We will be doning many levels of error checking
      // and will need to bale at any time,
      // so we do this...
      if (strlen($strSqlDate) == 10)
      {
            // Pull apart the Date
            $tempDate = explode('-', $strSqlDate);

            // See if we got what we thought we should
            if (count ($tempDate) == 3)
            {
                $year = $tempDate[0];
                $month = $tempDate[1];
                $daynum = $tempDate[2];
            }
            else
                  $err = true;
      }
      else
            $err = true;


      if (! $err )
            // PHP Date Object
            return mktime(0,0,0,$month,$daynum,$year);
      else
            return false;
} 

// convert from mysql DATETIME format "01:12:56";
function fromSqlTime($strSqlTime)
{
      // Error checking
      $err = false;

      // We will be doning many levels of error checking
      // and will need to bale at any time,
      // so we do this...
      if ((strlen($strSqlTime) == 5) || (strlen($strSqlTime) == 8))
      {
            // Pull apart the Date
            $tempTime = explode(':', $strSqlTime);

            // See if we got what we thought we should
            if ((count ($tempTime) == 2) || (count ($tempTime) ==
3))
            {
                $hour = $tempTime[0];
                $minute = $tempTime[1] ? $tempTime[1] : '00';
                $second = $tempTime[2] ? $tempTime[2] : '00';
            }
            else
                  $err = true;
      }
      else
            $err = true;


      if (! $err )
            // PHP Date Object
                  return mktime($hour,$minute,$second);
      else
            return false;
} 

// eof




More information about the thelist mailing list