dear friends

    m using strtotime () function to compare two dates , but it is not working for the date greater then '19-jan-2038'.my os is win 7 32 bit and php 5.2 and apache 2.2.plz if any body having any solution , kindly share it

Dani AI

Generated

Short answer: the root cause is the 32‑bit Unix timestamp limit (the “Year‑2038” problem). Using PHP DateTime objects (created from date strings) and comparing DateTime objects avoids the timestamp overflow — but avoid any code that converts back to a 32‑bit Unix timestamp (strtotime(), time(), mktime(), DateTime::getTimestamp() / format('U')) on a 32‑bit PHP build. (en.wikipedia.org)

Practical approach (build on ’s suggestion): always create DateTime objects from strings and compare those objects directly instead of converting to integers. Example pattern (different from the snippet already posted in the thread):

$left  = DateTime::createFromFormat('Y-m-d', '2045-06-10');
$right = DateTime::createFromFormat('Y-m-d', '2050-01-01');

if ($left < $right) {
    echo $left->format('Y-m-d') . " is before " . $right->format('Y-m-d');
}

Don’t call getTimestamp() or format('U') on a 32‑bit PHP build for dates beyond 2038 — those will fail or return false. Using DateTime methods (format other than 'U', diff(), direct comparison) gives reliable results. (ourcodeworld.com)

Troubleshooting checklist (what can try now)

  • Check PHP integer size: echo PHP_INT_SIZE; — 4 = 32‑bit, 8 = 64‑bit. If you’re on 32‑bit, strtotime()/time()/mktime will hit the limit. (learn.microsoft.com)
  • If you control the server, install a 64‑bit PHP build (or upgrade PHP) so Unix‑timestamp functions work past 2038.
  • For database storage, prefer DATETIME (or BIGINT epoch) rather than TIMESTAMP (TIMESTAMP is limited to 1970–2038 in many setups). (stackoverflow.com)

Short plan: switch comparisons to DateTime objects created from ISO strings, avoid Unix‑timestamp conversions on 32‑bit PHP, or move to 64‑bit PHP / use DATETIME in DB. This follows and ’s diagnosis but gives the concrete workaround and checks to use immediately.

Recommended Answers

All 6 Replies

This is the best way strtotime() works for me. A string like so '05/23/2014' in a mm/dd/yyyy format converts to 1400821200 in time and the datatype should be int. To convert back to date format, you can do date('d-m-Y',1400821200)

your script pls?

Member Avatar for Member #120589

Use DateTime() as this does not use internal integers and gets around the 32-bit issue.

The maximum value of a 32-bit integer is 2,147,483,647. 2,147,483,647 seconds from 01-01-1970 00:00:00 is January 19, 2038. If you add one more second, you get a date somewhere in 1902... so, your answer would by what @diafol said

but i am still facing problem to retrive current date ( which is after '19-jan-2038') by datetime function. my code is as follows:

<?php 
$date= new datetime();
echo $date->format('d-m-Y');
?>

diafol sir plz give any solution

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.