The timestamp to dates problem for PHP >= 5.1 - php

I may say I'm not a PHP programmer. I've been reading at http://php.net/manual/en/function.date.php that:
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).
I've database full of 1070-based-timestamps. How can I recover them with PHP >= 5.1?

My understanding is that your timestamps will work just fine. The way I read it, before PHP 5.1.0, negative timestamps were not allowed. A timestamp representing a date before 1970 requires a negative 32-bit integer.
In other words, timestamps pre- or post PHP 5.1.0 are all relative to Unix epoch, that is, 1970-01-01.

Related

PHP strtotime result is diffent in php7.3.2 and php7.3.1

just less than 1900,more than is normal.
php version is 7
example:
date_default_timezone_set('PRC');
$sbegintime = strtotime('1900-01-01 00:00:00');
var_dump($sbegintime);exit;
php7.3:-2209017600
php7.2:-2209017943
why?
Demo ~ https://3v4l.org/MHvIs
The famous SO question relating to a historical event in China wherein the clocks were set back 352 seconds on New Years Day, 1928, meaning you can get unexpected results when dealing with dates before that time. Apparantly PHP has updated the timezone offset for that timezone (for pre 1928 dates) , from +8:00 to +8:05. (Some other change that I can't find right now, but is mentioned in the linked question, accounts for the offset changing from 352 to 343).
/* php7.2 (+8:00) */
(new DateTime('1900-01-01 00:00:00', new DateTimezone("PRC")))->format(\DateTime::ATOM);
// 1900-01-01T00:00:00+08:00"
/* php7.3: (+8:05) */
(new DateTime('1900-01-01 00:00:00', new DateTimezone("PRC")))->format(\DateTimeInterface::ATOM);
// "1900-01-01T00:00:00+08:05"
The php7.3 changes don't call out this change specifically, but if you look at the changes to php7.2.12 it says
"Upgraded timelib to 2017.08."
and for 7.3.8 it says
"Updated timelib to 2018.02."
Since timelib is the underlying library supporting PHP DateTime, a change implemented between those versions would be resposible, unfortunately I can't find a changelog.
this notes from php strtotime documentation could help you.
The valid range of a timestamp is typically from Fri, 13 Dec 1901
20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (These are the dates
that correspond to the minimum and maximum values for a 32-bit signed
integer.)
Prior to PHP 5.1.0, not all platforms support negative timestamps,
therefore your date range may be limited to no earlier than the Unix
epoch. This means that e.g. dates prior to Jan 1, 1970 will not work
on Windows, some Linux distributions, and a few other operating
systems.

I am trying to find the retirement date from joining date at the age of 58 years in php

I am trying to find the retirement date from joining date at the age of 58 years in php.
$retire_date = date('Y-m-d', strtotime($joining_date. '+58 years'));
it's showing 1970-01-01 , Up to "+40 years" it's showing correctly.can anyone contribute to find this one
The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.) Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems. PHP 5.1.0 and newer versions overcome this limitation though.
For 64-bit versions of PHP, the valid range of a timestamp is effectively infinite, as 64 bits can represent approximately 293 billion years in either direction.
If the number of the year is specified in a two digit format, the values between 00-69 are mapped to 2000-2069 and 70-99 to 1970-1999. See the notes below for possible differences on 32bit systems (possible dates might end on 2038-01-19 03:14:07).
Source
Beside of autista_z's answer,
you can stumple upon this if you use an incorrect date format or something like this
below an example
$joining_date = "1976-14-02";
$timeToAdd = "+ 58 years";
$objDateTime = DateTime::createFromFormat("Y-m-d",$joining_date);
$objDateTime->modify($timeToAdd);
echo "My Retire Date is ".$objDateTime->format("Y-m-d")."<br />";
$retire_date = date('Y-m-d', strtotime($joining_date.$timeToAdd));
echo $retire_date;
die;
This leads with strtotime to a result like 1970-01-01.
This is also the reason why i prefer the Datetime function createFromFormat if you know your format, because the outcome is absolutely predictable.
(in this particulary example you'll see - datetime tries to find a correct value and interprets it as 1977-02-02)
Although it doesn't really explain why +40 years would work, but maybe you tested it with different data.
$retire_date = date('Y-m-d', strtotime('+58 years', strtotime($joining_date)));
Correct answer
I think this will work for you.
$retire_date = date('Y-m-d', strtotime('+58 years', strtotime($joining_date)));

How can i fix strotime for the year 1800

I have a little trouble with strtotime, I have a date 18 july 1868 and when I record in the database, I make $date = strtotime (18-07-1868). But when I appear it, I dated ('Y-m-d', $date) but it does not show me 18-07-1868 but 13-12-1901.
How can I fix this problem?
Thx,
From the manual:
The valid range of a timestamp is typically from Fri, 13 Dec 1901
20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC.
(These are the dates that correspond to the minimum and maximum values for a 32-bit signed
integer.)
Additionally, not all platforms support negative timestamps,
therefore your date range may be limited to no earlier than the Unix
epoch. This means that e.g. dates prior to Jan 1, 1970 will not work
on Windows, some Linux distributions, and a few other operating
systems. PHP 5.1.0 and newer versions overcome this limitation though.
So it depends on the platform you're running it on. It needs to be 64 bit to exceed the range stated in the manual.
You are not putting a string (single quote or double quote) in strtotime() function...
Try This...
$x = strtotime('18-07-1868');
echo date('Y-m-d', $x);

PHP date() only from 1970 - 2038

First off, this is not a question about how to fix a problem because my date is outputting 1969.
This is a question about why time does not exist before 1970 or after 2038 when using date().
I've tried seaching SO and Google but all that turns up is people getting errors when using date() incorrectly, resulting in the familiar output of December 31, 1969 5:00 pm
Does anyone know why it can't go before 1970? Should we stop using date() since it will be unusable after 2038? What's the history on this? What's the work around for working with dates outside of this range?
It's 2038 problem
and look, I'm in year 1653
This is explained on the PHP manual page for date():
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).
The fact that you're getting December 31, 1969 means that you're likely supplying an odd timestamp parameter to date(), resulting in a date that isn't what you expect. Like #Mitch Wheat said in his comment, this relates to Unix time since it is relative from January 1, 1970.
Compare the number 2^31 and the number of seconds between the two date.

Is string valid for use with strtotime()?

PHP has strtotime() which assumes the input given is a string that represents a valid time. Before using strtotime(), how can I verify that the string about to be given to it is in one of its valid formats?
strtotime() returns false if it can't understand your date format, so you can check its return value. The function doesn't have any side effects, so trying to call it won't hurt.
// $format = ...
if (($timestamp = strtotime($format)) !== false) {
// Obtained a valid $timestamp; $format is valid
} else {
// $format is invalid
}
Be warned, however, that strtotime() only supports the same range of Unix timestamps as the server architecture does. This means it will incorrectly return false for certain date ranges. From the manual:
Note:
The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.) Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems. PHP 5.1.0 and newer versions overcome this limitation though.
For 64-bit versions of PHP, the valid range of a timestamp is effectively infinite, as 64 bits can represent approximately 293 billion years in either direction.
Because server architectures can vary, you may be limited to 32-bit Unix timestamps depending on your use case, even though 64-bit timestamps cover a practically infinite period of time.
Actually you can't check that before executing the function, but you could take a look at the official php.net page. The only thing you can do is checking whether your function returns "false".
Here's a link strtotime().
There are a lot of examples of what you can do with strtotime().

Categories