Year returns 1970 in PHP [duplicate] - php

This question already has answers here:
Accessing dates in PHP beyond 2038
(5 answers)
Closed 4 years ago.
The below code returns 1970 if the year is 2038 and correct year (2037) if it is altered as 2037. why?
$date = '28/05/2038';
$date = strtr($date, '/', '-');
$year = date('Y', strtotime($date));
echo $year;
I did some research and understood that the strtotime conversion returns 0, when the passed value is unsupported and hence this happens. But I dont understand how will it become unsupported if I change the date from '28/05/2038' to '28/05/2037'.

It seems the problem is easily repeated for PHP versions below 5.2.16 even with even with PHP_INT_SIZE = 8. For that version and above the code executes correctly. Upgrading PHP to 5.2.16 should solve it. Only tested for PHP_INT_SIZE = 8 (64 bit)
Here you can check for yourself.
If you happen to be on PHP 5.2.0 or higher (but below 5.2.16) you can go for DateTime class as others suggested in the question's comments.
Note: Upgrading a service on a populated server may not be possible, not on work and not even on university courses. But if it happens to be possible, then you should test the proper version of PHP before upgrading. If it works, you should investigate why it has been fixed and at which point the functionality was added, googling PHP's bugtracker. Then suggest the change.

Related

Int casting gives wrong number on WAMP [duplicate]

This question already has answers here:
Working with large numbers in PHP
(8 answers)
What's the maximum value for an int in PHP?
(8 answers)
Closed 1 year ago.
I have some legacy code on a Windows machine running PHP 5.6 that we will be dismissing but I need to do some migrations from.
I have trivial bit of code that is quite mistifying to me.
I've simplified it for this question, but the behavior is the same.
$a = "3109584483";
$b = (int) $a;
var_dump($a);
var_dump($b);
And this is the output:
string(10) "3109584483"
int(2147483647)
I have looked at all the similar questions about this, but they all mention
Casting a float (not my case)
Getting the result in a non-10 base (I tried converting 2147483647 from and to common bases like HEX, OCT and BIN, but I can't seem to find a match that would justify the result)
What is the reason of this wrong casting?
Thanks in advance for your help!

UTC to local time adjustment woes with PHP [duplicate]

This question already has answers here:
Timezone conversion in php
(9 answers)
Closed 8 years ago.
Can someone explain why the following doesn't work?
I have a PHP script that runs on a shared hosting account. The script should output my local time (for the time zone where I'm located, i.e. GMT-8:00, or PST.)
I'm reading documentation for time() PHP method that is supposed to return a Unix timestamp in UTC (or GMT). OK, good. So I need to subtract 8 hrs to get my local time, right?
So I'm doing this:
echo(date("n/j/Y, g:i:s A", time() - 28800)); //8hrs = 28800 seconds
But I'm getting time that is 5 hrs behind!
What can be wrong in a one-line statement as such?
PS. Sorry, if I'm asking the obvious. I'm coming from the world of .NET. I'm new to PHP.
PHP's time() function will always return seconds since the epoch. The culprit for getting unexpected behavior is actually the date() function.
PHP's date() function will automatically convert the date to the current default timezone of PHP, which must be GMT - 3. This is why when you subtract 8 hours you're 5 hours behind.
Instead of using date(), you probably want to check in to gmdate(). Something like:
echo(gmdate("n/j/Y, g:i:s A", time() - 28800));
may be what you need.

convert date to time stamp by php [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Unix timestamp before 1970 (even before 1700), using PHP
as you know we have date element in HTML5,It can return something like it 1000-10-05,now I need to make this as time stamp,I try to do it by mktime() but It doesn't return true value.
now How can I do that?
mktime() is timestamp based. On 32 bit systems, timestamps can't reach dates that far back - a signed int can reach from ca. 1900 to 2038.
If you need to do operations with pre-1900 dates, consider using the DateTime library instead, available in PHP 5.2 and newer. It works with 64-bit data internally and can manage any date.
use
strtotime($yourHTML$DateString);
If your problem is not the timestamp range issue as discussed, try strtotime instead of mktime.
strtotime('1000-10-05') must do it. but it supports only 1970 and >

PHP: parsing a date of any format (especially: 2008-10-20, 2008/10/20, 2008.10.20, Nov. 20/08)

strtotime("25/03/1957") returns false. what will satisfy all of these date formats? i can't imagine how long it would take to actually make my own, so i hope there's already one out there you know of.
thanks!
Considering some dates are valid but can point to two different actual dates, no function will ever be able to "guess" the right format at all times...
To help with that, with PHP >= 5.3, a new function has been added : date_create_from_format -- but it doesn't exist with PHP < 5.3, unfortunately...
(See also DateTime::createFromFormat)
Still, in the example you took, the year 1957 is a possible source of problems : PHP generally works with UNIX Timestamps, when it comes to dates...
And, at least on 32-bits systems, those can only represent dates between 1970 and 2038 -- as they count the number of seconds since 1970-01-01.
To avoid this problem, it's often a good idea to use the DateTime class, with which (quoting) :
The date and time information is
internally stored as an 64-bit number
so all imaginable dates (including
negative years) are supported. The
range is from about 292 billion years
in the past to the same in the future.
(It will not solve the parsing problems with PHP < 5.3 ; but it'll solve the date-range problem...)
I've found that dateTime objects support a wider range of formats than the strtotime() function, and the timezone settings of your server also make a difference; but I ended up building a function that would replace '/' with '-' before using the string to date methods. I also test for valid, then try swapping the apparent dd and mm (25-03-2001 => 03-25-2001) if invalid before testing again.

Calculate date difference having a large range with PHP

I need to calculate the number of decades between 2 dates possible spanning form 1708 until today - the limitations are as far as I can gather are 1970/1901 within PHP native functions.
Can anyone advise?
Thanks!
If you have PHP >= 5.3.0:
$before = new DateTime('1708-02-02');
$decades = $before->diff(new DateTime())->y / 10;
You could use Zend_Date. It's part of the Zend Framework but can be used standalone, you don't need to install the whole framework to use it. It can work with dates beyond 1901 if the bcmath extension is installed into your PHP. From Zend_date's theory of operation:
This was only possible, because Zend_Date is not limited to UNIX timestamps nor integer values. The BCMath extension is required to support extremely large dates outside of the range Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. Additional, tiny math errors may arise due to the inherent limitations of float data types and rounding, unless using the BCMath extension.
I like to recommend Zend components because they are well-groomed, high-quality code. There are other solutions to this, though, for example using the mySQL date functions.
I would write a custom method.
PHP date functions are based on the epoch ( 1969 ) so there won't be much help there.
If your just looking at years that is simple math
higher year - earlier year = years
years / 10 = decades.
If you mean decades from an even 01-10, 11-20 then you could do some rounding. (round early date up, later date down to nearest 10.

Categories