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.


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:
 
epool86
Posts: 7
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..
Mike
Posts: 7
Comment
Re: Handle PHP Dates beyond 2038
Reply #6 on : Thu July 10, 2008, 17:24:21
Very usefull!

Mike S.
<a href="http://www.yourerpsoftware.com/">http://www.yourerpsoftware.com/</a>
Hoteles Mar del Plata
Posts: 7
Comment
Hoteles Mar del Plata
Reply #5 on : Mon June 30, 2008, 16:45:35
Good proces for manager time in the php, very easy Date time is value very hard...
http://www.hotelesmardel-plata.com.ar/
xwisdom
Posts: 7
Comment
Insert type datetime in the mysql
Reply #4 on : Mon June 02, 2008, 21:19:04
Hi,

To insert dates into MySQL you can use the following format:

$mysqlDateValue = $date->format("Y-m-d");
Last Edit: June 02, 2008, 21:21:16 by xwisdom  
Hoteles en bariloche
Posts: 7
Comment
Insert type datetime in the mysql
Reply #3 on : Wed May 21, 2008, 14:34:15
Good article, only no example iunsert record in the mysql
Anonymous
Posts: 7
Comment
Re: Handle PHP Dates beyond 2038
Reply #2 on : Mon May 05, 2008, 23:41:08
Good work!
Jesús
Posts: 7
Comment
Re: Handle PHP Dates beyond 2038
Reply #1 on : Wed April 16, 2008, 07:38:42
Thank you very much for your great work