Working with Dates beyond 2038 in PHP4/5

I've spent a lot of time searching the net in order to find a library that can be used as a replacement for the php strtotime() functions. I was looking for something that would at least allow me to convert dates like 2 Jan 2039 to a timestamp. My closest find was the ADODB Date library, which can be used to replace the other date functions (like mktime, etc) but not strtotime. As a result of this, I've decided to create me own little Date class library as a first start.

This Date Class library makes use of the ADODB Date library in order to support timestamps before 1970 and after 2038. Here's a short list of supported features:

  • Supports both php4 and php5.
  • Handles dates before and 1970 and afer 2038.
  • Supports over four different date input formats.
  • Compatible with existing php date functions (e.g date,strtotime,etc).

To start working with the Date class you will first need to download and extract the dateclass.zip file to a folder. Copy the files date.class.php and adodb_date.php to a folder on your website or application. You will only need to include the file date.class.php inside your PHP page.

Note: The ADODB Date Library is a standalone library and can easily updated when a new version is available from the ADODB website. This file is only included for convenience.

Example:
include_once "/includes/date.class.php";
$date = new Date("March 20, 2039 11:30 PM");
echo $date->format("d-M-Y");

You can also use the format() function to directly format date value that's passed in as the second parameter.

Example:
$date = new Date();
echo $date->format("M d, Y","2079/12/20 12:30:23");
Supported Date Input Formats:
MM/DD/YYYYMM-DD-YYYYMM.DD.YYYY
DD/MM/YYYYDD-MM-YYYYDD.MM.YYYY
YYYY/MM/DDYYYY-MM-DDYYYY.MM.DD
MMM DD, YYYYDD MMM YYYYDD MMM YYYY HH:NN:SS AM/PM

Links:
For more information about the ADODB Date library please visit the ADDB Date Library website.

The Date class library is licensed under the LGPL. You're free to use and improve where necessary. I gladly welcome your feedack, suggestions and ideas. You may post your comments below.

Updated: New version 1.0.2 released with updates to the ADODB date/time functions (version 0.33).


Write a comment

  • Required fields are marked with *.

If you have trouble reading the code, click on the code itself to generate a new random code.
Security Code:
 
Showing comments 1 to 10 of 16 | Next | Last
tim
Posts: 16
Comment
Re: Handle PHP Dates beyond 2038
Reply #16 on : Tue January 05, 2010, 23:27:31
Very useful. Thanks Raymond.

I use the following to calculate depreciation and replacement dates into the future. It took me a while to work out where to put the timestamp!

[code]
require_once('includes/date.class.php');
$replacementpercent = row_rsAsset['ReplacementPercent'];
$rate = $row_rsAsset['DepreciationRatePerYear'];
$startdate = $row_rsAsset['DepreciationStartDate'];
$yearsuntilreplacement = (100-$replacementpercent)/$rate;
$secondsuntilreplacement = $yearsuntilreplacement*365.25*24*60*60;
$replacementdate = mktime(0,0,0,date('m',strtotime($startdate)),date('d',strtotime($startdate)),date('Y',strtotime($startdate)))+$secondsuntilreplacement;
$date = new Date($replacementdate);
echo $date->format('d M Y');

[/code]
stridge
Posts: 16
Comment
Thank you!
Reply #15 on : Mon August 24, 2009, 21:21:07
Thanks a lot for this simple yet effective wrapper. Pre-pending all my date functions with adodb_ would have looked silly, so I started writing almost the same as you.

No need now, as it works out the box, exactly as illustrated!
v
Posts: 16
Comment
it's good but cannot add year.
Reply #14 on : Sun July 26, 2009, 19:16:30
i cannot add more than 28 years anyway.
i want to add year from now >30 years but no way to do.
how can i use this class when i can't add 30 years/
xwisdom
Posts: 16
Comment
re: The compareTo function
Reply #13 on : Sun March 29, 2009, 09:16:57
Hi Herman,

Thanks for sharing.
I'll have a look at the code and the possibility of how it can be included.
Hernan Nardelli
Posts: 16
Comment
The compareTo function
Reply #12 on : Fri March 20, 2009, 10:39:10
I added the compareTo(..) function based on the java.util.Date.compareTo(..) Java implementation. I hope you consider to include in your class:

[code]
function compareTo($anotherDate) {
$thisTime = $this->getTimestamp();
$anotherTime= $anotherDate->getTimestamp();

return ($thisTime<$anotherTime ? -1 : ($thisTime==$anotherTime ? 0 : 1));
}
[/code]
Hernan
Posts: 16
Comment
Congratulations
Reply #11 on : Wed March 18, 2009, 13:25:28
I begun to develop my own Date class when i decided to access the PHP.NET to search for one. I found there a reference to your site. Its very useful. Congratulations for your initiative.
Anonymous
Posts: 16
Comment
Re: Handle PHP Dates beyond 2038
Reply #10 on : Mon February 09, 2009, 02:12:38
nice
Anis
Posts: 16
Comment
really good work
Reply #9 on : Fri January 09, 2009, 09:01:25
was thinking what will be easiest way to fix this year 2038 problem and then found your class by googling..working staright in my project.it has saved my time. cool...thanks.
Sebastian
Posts: 16
Comment
Re: Handle PHP Dates beyond 2038
Reply #8 on : Wed September 03, 2008, 15:57:18
Excelent Work,
I has working in my site www.hotels-buenos-aires-argentina.com
epool86
Posts: 16
Comment
wow
Reply #7 on : Tue July 15, 2008, 03:26:19
great work! thank you so much.. i'll use it in my latest project..
Showing comments 1 to 10 of 16 | Next | Last