Php: Strtotime Is Returning False for a Future Date

PHP: strtotime is returning false for a future date?

If you want to work with dates that fall outside the 32-bit integer date range, then use PHP's dateTime objects

try {
$date = new DateTime('2110-07-16 10:07:47');
} catch (Exception $e) {
echo $e->getMessage();
exit(1);
}

echo $date->format('Y-m-d');

strtotime returns false on certain dates

We have such problem on our project and we solve it by using DateTime::createFromFormat

$dateTime = \DateTime::createFromFormat('d/m/Y', '12/2/2016');
// now you can receive absolutely correct timestamp
$dateTime->getTimestamp()

PHP strtotime returning false for dates less than 1900

AFAIK, you are limited to these two solutions.

You should update to 64-bit system if you can afford it, and if it solves a problem then that's better.

Edit: Just had to check:
http://php.net/manual/en/function.runkit-function-redefine.php
You could probably try and redefine strtotime() to work with dates prior to 1901.

If statement returning false result using date and strtotime

date('d/m/y') gives you a string like 30/11/20. Comparing dates in this format really means you're comparing strings, which only works if the dates are in YYYYMMDD (or YYYY-MM-DD) format.

It's much easier to just use strtotime() to turn the English-like date into a Unix timestamp (just a big integer) and compare them directly:

if ( 
strtotime('now') >= strtotime('1 January 2021')
&&
strtotime('now') <= strtotime('31 January 2021')
) {
echo 'true';
} else {
echo 'false';
}

strtotime is not working proper in php for future date?

The maximum date allowed is Tue, 19 Jan 2038 03:14:07 UTC on 32 bit system

From the strotime docs:

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.

If you want it to work for 32 bit system then try like this using DateTime:

$date = new DateTime($_POST['re_date']);

echo $date->format('Y-m-d');

strtotime return false while converting date string

2050 cannot be represented internally on 32 bit systems.

Timestamp have a limit to 2038 because the max value for a 32-bit integer is 2,147,483,647, that is: 2038-01-19T03:14:08+0000Z

You have just experienced the year2038 bug.

How to fix

Don't use timestamp. Instead use a more robust library such as new \DateTime('2050-10-13'); and of course on your Database use Date field.

Strange behaviour of strtotime for dates far in the future

This is because of the fact year 2050 is not in the 32-bit Unix epoch (which ends somewhere in 2038). See more on the documentation page for strtotime().

PHP IF statement with Date not returning False

As far as I remember its 2014. Why not use DateTime object and do why not do it right way??

$raw = '04-05-2010';
$theDate= \DateTime::createFromFormat('d-m-Y', $raw);
$raw2 = '02-05-2010';
$anotherDate = \DateTime::createFromFormat('d-m-Y', $raw2);

echo 'theDatedate: ' . $theDate->format('m/d/Y') . "<br>";
echo 'anotherDate date: ' . $anotherDate ->format('m/d/Y') . "<br>";

if ($theDate > $anotherDate ) {
echo "Greater than!<br />";
}

If you are learning php please check this resource, is worth reading.

strtotime(fifth monday september 2017) returns 2nd October, 2017?

PHP's strtotime is based on GNU's date parsing C function (IIRC it actually calls this C function instead of using its own implementation). From the GNU documentation:

The explicit mention of a day of the week will forward the date (only if necessary) to reach that day of the week in the future... A number may precede a day of the week item to move forward supplementary weeks.

Therefore you will not get a false result directly from strtotime with the example you provide. If you want to confirm the result is in the same month, you can do something like:

$timestamp = strtotime("fifth monday september 2017");
$month = date('n', strtotime("september 1, 2017"));
$same_month = (date('n', $timestamp) === $month);

Also from the GNU documentation, to add some perspective:

Our units of temporal measurement, from seconds on up to months, are so complicated, asymmetrical and disjunctive so as to make coherent mental reckoning in time all but impossible... Unlike the more successful patterns of language and science, which enable us to face experience boldly or at least level-headedly, our system of temporal calculation silently and persistently encourages our terror of time... It is no wonder then that we often look into our own immediate past or future, last Tuesday or a week from Sunday, with feelings of helpless confusion.

-Robert Grudin, Time and the Art of Living



Related Topics



Leave a reply



Submit