Date Class

A component to handle date and time.
This documentation is available in english | français.
You can help by translating this documentation; contact me for the the doc source.

Informations

Version : 0.5
Author : Leo West
Licence : GPL - Send me a mail if you enjoy this component!

Installation

Download date-0.5.zip
You should unzip and install Date/date.php in your shared PHP librairies directory,
for example /usr/lib/php or /home/www/lib.

Documentation

There may be some methods i have not documented yet, see the source :)

Table of content

Constructeurs

Date( [timestamp] )

Create a new Date instance.
The optional parameter timestamp is provided to set the Unix timestamp (number of seconds since 1/1/1970)
By default, the date is initialized with current date & time.

fromDatetime( string datetime )

Construit un objet date a partir d'une chaine au format iso-8901.

$date = Date::fromDatetime( "2001-08-01T12:30:59" )

datetime est une chaine au format ISO comportant les parties date et heure
Retour un objet Date si ok, NULL si format non reconnu.

Le format ISO est YYYY MM DD "T" hh mm ss [TZ]
Le code de timezone optionnel TZ est ignoré dans cette version.
Accepte plusieurs variantes avec ou sans séparateur:

20010801123059 => OK
20010801T123059Z => OK
2001-08-01T12:30:59 => OK
2001-08-01 => error
2001-08-01T01:30 => error
2001-08-01T1:30:59 => error

setHours( int )

setMinutes( int )

setSeconds( int )

Set the hours, minutes, seconds fields

setTime( hours, minutes, seconds=0 )

Shortcut to set all time fields at once. seconds are optional, 0 is assumed

setYear( int )

setMonth( int )

setDay(int)

setWeekDay( int )

Set the year, month of year, day of month, day of week fields.

The day of week is a number between 0 (sunday) and 6 (saturday). You may use the WDAY_* constants defined, see § Constants.
When both setDay() and setWeekday() are called, the last call takes over.

Getters

getTimestamp

Return the Unix timestamp (number of seconds since 1/1/1970)

getYear

Return the year field

getMonth

Return the month field

getDay

Return the day (in the month) field

getWeekday

Return the day of the week field, from 0 for sunday to 6 for saturday

getYearday

Return the day in the year (1-366)

getHours

Return the hours field

getMinutes

Return the minutes field

getSeconds

Return the seconds field

getSecondsInDay

Return the number of seconds elapsed since 00:00:00.

DaysInMonth

Return the number of days the current month has.

Adding and substract

addYears( $n )

addMonths( $n )

addDays( $n )

addHours( $n )

addMinutes( $n )

Add resp. years, months, days, hours, minutes to the current date.
$n is an integer and be negative to substract.
Overflows are handled automatically: if you add 2 days to a Date[ 30/12/2000 ], you'l obtain a Date[ 01/01/2001 ]

Comparators

compareTo( Date otherdate )

Compare the date with another.
Return a number of seconds representing the difference thisDate - otherDate
otherdate a valid Date object

if( $date1->compareTo( $date2 ) == 0 ) echo "date1 and date2 are equals";

daysTo( Date otherdate )

Return the number of days between this date and otherDate, negative if otherDate is before thisDate.
otherdate a valid Date object

Format & Display

toString( format )

Return the date formatted according to the given format, using the same codes as strftime().
$time = $date->toString( "%H hours %M minutes %S seconds" );
See § Constants for some predefined formats.

Format( format, timestamp )

A static method that format the given Unix timestamp according to format.
Usefull when you have many timestamps to format.

$today = Date::Format( "%a %d %m %Y", time() );
$yesterday = Date::Format( "%a %d %m %Y", time() - 86400 );

Constants

Some usefull constants are defined, for use with the setWeekDay (WDAY_*) abd toString (FMT_*) methods.

Symbol Description Value
WDAY_SUNDAY dimanche 0
WDAY_MONDAY lundi 1
WDAY_TUESDAY mardi 2
WDAY_WENESDAY mercredi 3
WDAY_THURSDAY jeudi 4
WDAY_FRIDAY vendredi 5
WDAY_SATURDAY samedi 6
SEC_MINUTE nombre de secondes dans 1 minute 60
SEC_HOUR nombre de secondes dans 1 heure 3600
SEC_DAY nombre de secondes dans 1 jour 86400
FMT_DATEISO format d'une date iso8601 "%Y%m%dT%H%M%S"
FMT_DATELDAP format d'une date LDAP "%Y%m%d%H%M%SZ"
FMT_DATERFC822 format d'une date RFC 822 (Mime) "%a, %d %b %Y %H:%M:%S"
FMT_DATEFR date au format français (jj/mm/aaaa) "%d/%m/%Y"
FMT_DATEEN date au format US (mm/jj/aaaa) "%m/%d/%Y"
FMT_TIME heure au format hh:mm, sans les secondes "%H:%M"