473,406 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Wrong date conversion

Claus Mygind
571 512MB
I use the following to format my dates from the mySQL format to my html display format.

Expand|Select|Wrap|Line Numbers
  1.     $time = strtotime($date);
  2.     $m = date("m", $time);
  3.     $d = date("d", $time);
  4.     $y = date("Y", $time);
  5. $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?
Jun 10 '11 #1

✓ answered by omerbutt

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($string) can handle uptill 1909 . here it is
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function myStrtotime($strInput)
  3. {
  4.     $iVal = -1;
  5.     for ($i=1900; $i<=1969; $i++)
  6.     {
  7.         // Check for this year string in date
  8.         $strYear = (string)$i;
  9.         if (!(strpos($strInput, $strYear)===false))
  10.         {
  11.             $replYear = $strYear;
  12.             $yearSkew = 1970 - $i;
  13.             $strInput = str_replace($strYear, '1970', $strInput);
  14.         }
  15.     }
  16.     $iVal = strtotime($strInput);
  17.     if ($yearSkew> 0)
  18.     {
  19.         $numSecs = (60 * 60 * 24 * 365 * $yearSkew);
  20.         $iVal = $iVal - $numSecs;
  21.         $numLeapYears = 0;  // determine number of leap years in period
  22.         for ($j=$replYear; $j<=1969; $j++)
  23.         {
  24.             $thisYear = $j;
  25.             $isLeapYear = false;
  26.             // Is div by 4?
  27.             if (($thisYear % 4) == 0)
  28.             {
  29.                 $isLeapYear = true;
  30.             }
  31.             // Is div by 100?
  32.             if (($thisYear % 100) == 0)
  33.             {
  34.                 $isLeapYear = false;
  35.             }
  36.             // Is div by 1000?
  37.             if (($thisYear % 1000) == 0)
  38.             {
  39.                 $isLeapYear = true;
  40.             }
  41.             if ($isLeapYear == true)
  42.             {
  43.                 $numLeapYears++;
  44.             }
  45.         }
  46.         $iVal = $iVal - (60 * 60 * 24 * $numLeapYears);
  47.     }
  48.     return $iVal;
  49. }
  50. echo date("m-d-Y",myStrtotime("1909-01-01")) ;
  51. ?>
  52.  
hope it could help you some how
regards,
Omer Aslam

6 2565
Claus Mygind
571 512MB
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?

http://www.dslreports.com/forum/r193...ing-wrong-date
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).
Jun 10 '11 #2
omerbutt
638 512MB
MYSQL ACCEPTS Y/m/d format for dates where as you are providing

m/d/Y

change the last line to
Expand|Select|Wrap|Line Numbers
  1.  $date = "$y/$m/$d";
  2.  
Jun 13 '11 #3
Claus Mygind
571 512MB
Please note I am reading the date from MySQL. In the following code the "$date" is the MySQL date "1900-01-01"

Expand|Select|Wrap|Line Numbers
  1. $time = strtotime($date);
  2.  
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
Jun 13 '11 #4
omerbutt
638 512MB
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($string) can handle uptill 1909 . here it is
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function myStrtotime($strInput)
  3. {
  4.     $iVal = -1;
  5.     for ($i=1900; $i<=1969; $i++)
  6.     {
  7.         // Check for this year string in date
  8.         $strYear = (string)$i;
  9.         if (!(strpos($strInput, $strYear)===false))
  10.         {
  11.             $replYear = $strYear;
  12.             $yearSkew = 1970 - $i;
  13.             $strInput = str_replace($strYear, '1970', $strInput);
  14.         }
  15.     }
  16.     $iVal = strtotime($strInput);
  17.     if ($yearSkew> 0)
  18.     {
  19.         $numSecs = (60 * 60 * 24 * 365 * $yearSkew);
  20.         $iVal = $iVal - $numSecs;
  21.         $numLeapYears = 0;  // determine number of leap years in period
  22.         for ($j=$replYear; $j<=1969; $j++)
  23.         {
  24.             $thisYear = $j;
  25.             $isLeapYear = false;
  26.             // Is div by 4?
  27.             if (($thisYear % 4) == 0)
  28.             {
  29.                 $isLeapYear = true;
  30.             }
  31.             // Is div by 100?
  32.             if (($thisYear % 100) == 0)
  33.             {
  34.                 $isLeapYear = false;
  35.             }
  36.             // Is div by 1000?
  37.             if (($thisYear % 1000) == 0)
  38.             {
  39.                 $isLeapYear = true;
  40.             }
  41.             if ($isLeapYear == true)
  42.             {
  43.                 $numLeapYears++;
  44.             }
  45.         }
  46.         $iVal = $iVal - (60 * 60 * 24 * $numLeapYears);
  47.     }
  48.     return $iVal;
  49. }
  50. echo date("m-d-Y",myStrtotime("1909-01-01")) ;
  51. ?>
  52.  
hope it could help you some how
regards,
Omer Aslam
Jun 13 '11 #5
Claus Mygind
571 512MB
Thank you very much for the helpful reply
Jun 16 '11 #6
omerbutt
638 512MB
My pleasure if that helped you
regards,
Omer Aslam
Jun 17 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: nimish | last post by:
I have Month, Day and Year fields on my form. When I submit this form I want to create a valid date from all three fields before inserting into MYSQL table. I tried following:...
4
by: Annie D via AccessMonster.com | last post by:
Problem! I have a series of CSV files that I am bringing in to Access, contained within each of them are 6 date fields, however, they are not being recognized as dates…this is a big problem!...
8
by: John Wildes | last post by:
Hello all I'm going to try and be brief with my question, please tell me if I have the wrong group. We are querying transaction data from a DB3 database application. The dates are stored as...
1
by: Franck | last post by:
Hi, 'm gettin mad about date conversion. Here is the point. Got and add-in for Excel which call functions from a web service (on a remote server) The remote server has regional settings set...
7
by: flupke | last post by:
I'm using a solid DB and i'm accessing it via the odbc module (activepython). I get a DbiDate object returned but i don't find a way to decently print it or get a format like %d/%m%/%y. I found...
7
by: bruce.dodds | last post by:
Access seems to be handling a date string conversion inconsistently in an append query. The query converts a YYYYMM string into a date, using the following function: CDate(Right(,2) & "/1/" &...
2
by: RN1 | last post by:
A TextBox displays the current date (in dd/mm/yyyy format) & time when a user comes to a page (e.g. 15/10/2008 1:36:39 PM). To convert the date into international format so that the remote server...
2
by: anderst | last post by:
Hi! I need help with date conversion, I need a perl-script that transform Wed Mar 28 00:00:00 GMT+02:00 2007 to 2007-03-28 I have tried to use perl modules Date::Manip and Date::Calc, but no...
0
by: sumansms | last post by:
Hi All, I am getting wrong date when I am using DateTime.Parse function. The following is the code.. DateValue = DateTime.Parse(datarow.ToString()).ToString("MM/dd/yyyy"); I am taking the data...
4
maheshwag
by: maheshwag | last post by:
my code is: string sql="insert into dummy(date)values(@date) sqlcommand cmd=new sqlcommand(sql,con) cmd.parameters.Add("@date", SqlDbtype.datetime).values=maskedtextbox1.text;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.