Wrong date conversion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    Wrong date conversion

    I use the following to format my dates from the mySQL format to my html display format.

    Code:
    	$time = strtotime($date);
    	$m = date("m", $time);
    	$d = date("d", $time);
    	$y = date("Y", $time);
    $date = "$m/$d/$y";
    And it works fine. But my users insert a bogus date of "1901-01-01" when a project is on hold.

    Running that date through this routine returns a date of
    12/31/1969.

    Why is that?
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    #2
    I found this thread with reference to date. I am running php 5.3. But surely there must be other people out there having a need for date ranges beyond these limits. So how does one handle dates outside this limited range?


    here is the particular quote that gave me the hint.

    The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer). However, before PHP 5.1.0 this range was limited from 01-01-1970 to 19-01-2038 on some systems (e.g. Windows).

    Comment

    • omerbutt
      Contributor
      • Nov 2006
      • 638

      #3
      MYSQL ACCEPTS Y/m/d format for dates where as you are providing

      m/d/Y

      change the last line to
      Code:
       $date = "$y/$m/$d";

      Comment

      • Claus Mygind
        Contributor
        • Mar 2008
        • 571

        #4
        Please note I am reading the date from MySQL. In the following code the "$date" is the MySQL date "1900-01-01"

        Code:
        $time = strtotime($date);
        The I use the "strtotime" to parse the date into it's components with php before displaying it on the web page.

        It appears php 5 cannot handle anything before "1901-12-13" and php 4 cannot handle anything before "1970-01-01".

        Any further suggestions would be appreciated

        Comment

        • omerbutt
          Contributor
          • Nov 2006
          • 638

          #5
          hi Claus,
          haaah. ....i didnt see that ,
          The DateTime class, here, might help (quoting) : Each component of date (e.g. year) is internally stored as 64-bit number so all imaginable dates (including negative years) are supported.
          But note that :
          * It's only exists in PHP >= 5.2
          * And several methods only exist in PHP >= 5.3

          So : beware of which methods you're using, if you're developping on PHP 5.3 and want your software to be compatible with PHP 5.2
          Another solution (especially, if using Zend Framework in your application) would be the Zend_Date component (quoting) :
          Although PHP 5.2 docs state, "The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT," Zend_Date supports a nearly unlimited range, with the help of the BCMath extension

          another is strtotime() limitations , what i use myStrtomtime($s tring) can handle uptill 1909 . here it is
          [code=PHP]
          <?php
          function myStrtotime($st rInput)
          {
          $iVal = -1;
          for ($i=1900; $i<=1969; $i++)
          {
          // Check for this year string in date
          $strYear = (string)$i;
          if (!(strpos($strI nput, $strYear)===fal se))
          {
          $replYear = $strYear;
          $yearSkew = 1970 - $i;
          $strInput = str_replace($st rYear, '1970', $strInput);
          }
          }
          $iVal = strtotime($strI nput);
          if ($yearSkew> 0)
          {
          $numSecs = (60 * 60 * 24 * 365 * $yearSkew);
          $iVal = $iVal - $numSecs;
          $numLeapYears = 0; // determine number of leap years in period
          for ($j=$replYear; $j<=1969; $j++)
          {
          $thisYear = $j;
          $isLeapYear = false;
          // Is div by 4?
          if (($thisYear % 4) == 0)
          {
          $isLeapYear = true;
          }
          // Is div by 100?
          if (($thisYear % 100) == 0)
          {
          $isLeapYear = false;
          }
          // Is div by 1000?
          if (($thisYear % 1000) == 0)
          {
          $isLeapYear = true;
          }
          if ($isLeapYear == true)
          {
          $numLeapYears++ ;
          }
          }
          $iVal = $iVal - (60 * 60 * 24 * $numLeapYears);
          }
          return $iVal;
          }
          echo date("m-d-Y",myStrtotime( "1909-01-01")) ;
          ?>
          [/code]

          hope it could help you some how
          regards,
          Omer Aslam

          Comment

          • Claus Mygind
            Contributor
            • Mar 2008
            • 571

            #6
            Thank you very much for the helpful reply

            Comment

            • omerbutt
              Contributor
              • Nov 2006
              • 638

              #7
              My pleasure if that helped you
              regards,
              Omer Aslam

              Comment

              Working...