2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Represents a date field.
|
2009-04-02 19:17:04 +02:00
|
|
|
* The field currently supports New Zealand date format (DD/MM/YYYY),
|
|
|
|
* or an ISO 8601 formatted date (YYYY-MM-DD).
|
|
|
|
* Alternatively you can set a timestamp that is evaluated through
|
|
|
|
* PHP's built-in date() function according to your system locale.
|
|
|
|
*
|
|
|
|
* @todo Add localization support, see http://open.silverstripe.com/ticket/2931
|
|
|
|
*
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage model
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class Date extends DBField {
|
|
|
|
|
|
|
|
function setValue($value) {
|
2009-10-21 04:30:07 +02:00
|
|
|
// @todo This needs tidy up (what if you only specify a month and a year, for example?)
|
|
|
|
if(is_array($value)) {
|
|
|
|
if(!empty($value['Day']) && !empty($value['Month']) && !empty($value['Year'])) {
|
|
|
|
$this->value = $value['Year'] . '-' . $value['Month'] . '-' . $value['Day'];
|
|
|
|
return;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-02-12 05:57:17 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Default to NZ date format - strtotime expects a US date
|
2009-02-12 05:57:17 +01:00
|
|
|
if(ereg('^([0-9]+)/([0-9]+)/([0-9]+)$', $value, $parts)) {
|
|
|
|
$value = "$parts[2]/$parts[1]/$parts[3]";
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is_numeric($value)) {
|
|
|
|
$this->value = date('Y-m-d', $value);
|
|
|
|
} elseif(is_string($value)) {
|
|
|
|
$this->value = date('Y-m-d', strtotime($value));
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the date in the format dd/mm/yy
|
|
|
|
*/
|
|
|
|
function Nice() {
|
|
|
|
if($this->value) return date('d/m/Y', strtotime($this->value));
|
|
|
|
}
|
|
|
|
|
2009-03-10 23:17:26 +01:00
|
|
|
function NiceUS() {
|
|
|
|
if($this->value) return date('m/d/Y', strtotime($this->value));
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns the year from the given date
|
|
|
|
*/
|
|
|
|
function Year() {
|
|
|
|
if($this->value) return date('Y', strtotime($this->value));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the Full day, of the given date.
|
|
|
|
*/
|
|
|
|
function Day(){
|
|
|
|
if($this->value) return date('l', strtotime($this->value));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the month
|
|
|
|
*/
|
|
|
|
function ShortMonth() {
|
|
|
|
if($this->value) return date('M', strtotime($this->value));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the date of the month
|
|
|
|
*/
|
|
|
|
function DayOfMonth() {
|
|
|
|
if($this->value) return date('j', strtotime($this->value));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the date in the format 24 May 2006
|
|
|
|
*/
|
|
|
|
function Long() {
|
|
|
|
if($this->value) return date('j F Y', strtotime($this->value));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-04-21 00:32:13 +02:00
|
|
|
* Return the date using a particular formatting string.
|
|
|
|
*
|
|
|
|
* @param string $format Format code string. e.g. "d M Y"
|
|
|
|
* @return string The date in the requested format
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-04-21 00:32:13 +02:00
|
|
|
function Format($format) {
|
|
|
|
if($this->value) return date($format, strtotime($this->value));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2009-03-04 04:44:11 +01:00
|
|
|
/**
|
|
|
|
* Return the date formatted using the given strftime formatting string.
|
|
|
|
*
|
2009-04-29 03:20:24 +02:00
|
|
|
* strftime obeys the current LC_TIME/LC_ALL when printing lexical values
|
2009-03-04 04:44:11 +01:00
|
|
|
* like day- and month-names
|
|
|
|
*/
|
|
|
|
function FormatI18N($formattingString) {
|
|
|
|
if($this->value) return strftime($formattingString, strtotime($this->value));
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/*
|
|
|
|
* Return a string in the form "12 - 16 Sept" or "12 Aug - 16 Sept"
|
|
|
|
*/
|
|
|
|
function RangeString($otherDateObj) {
|
|
|
|
$d1 = $this->DayOfMonth();
|
|
|
|
$d2 = $otherDateObj->DayOfMonth();
|
|
|
|
$m1 = $this->ShortMonth();
|
|
|
|
$m2 = $otherDateObj->ShortMonth();
|
|
|
|
$y1 = $this->Year();
|
|
|
|
$y2 = $otherDateObj->Year();
|
|
|
|
|
|
|
|
if($y1 != $y2) return "$d1 $m1 $y1 - $d2 $m2 $y2";
|
|
|
|
else if($m1 != $m2) return "$d1 $m1 - $d2 $m2 $y1";
|
|
|
|
else return "$d1 - $d2 $m1 $y1";
|
|
|
|
}
|
|
|
|
|
|
|
|
function Rfc822() {
|
|
|
|
if($this->value) return date('r', strtotime($this->value));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of seconds/minutes/hours/days or months since the timestamp
|
|
|
|
*/
|
|
|
|
function Ago() {
|
|
|
|
if($this->value) {
|
2009-07-02 00:27:18 +02:00
|
|
|
if(strtotime($this->value) == time() || time() > strtotime($this->value)) {
|
2009-01-05 07:19:48 +01:00
|
|
|
return sprintf(
|
|
|
|
_t(
|
|
|
|
'Date.TIMEDIFFAGO',
|
|
|
|
"%s ago",
|
|
|
|
PR_MEDIUM,
|
|
|
|
'Natural language time difference, e.g. 2 hours ago'
|
|
|
|
),
|
|
|
|
$this->TimeDiff()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return sprintf(
|
|
|
|
_t(
|
|
|
|
'Date.TIMEDIFFAWAY',
|
|
|
|
"%s away",
|
|
|
|
PR_MEDIUM,
|
|
|
|
'Natural language time difference, e.g. 2 hours away'
|
|
|
|
),
|
|
|
|
$this->TimeDiff()
|
|
|
|
);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function TimeDiff() {
|
2007-11-07 07:05:52 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($this->value) {
|
|
|
|
$ago = abs(time() - strtotime($this->value));
|
|
|
|
|
|
|
|
if($ago < 60) {
|
|
|
|
$span = $ago;
|
2009-01-05 07:19:48 +01:00
|
|
|
return ($span != 1) ? "{$span} "._t("Date.SECS", " secs") : "{$span} "._t("Date.SEC", " sec");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
if($ago < 3600) {
|
|
|
|
$span = round($ago/60);
|
2009-01-05 07:19:48 +01:00
|
|
|
return ($span != 1) ? "{$span} "._t("Date.MINS", " mins") : "{$span} "._t("Date.MIN", " min");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
if($ago < 86400) {
|
|
|
|
$span = round($ago/3600);
|
2009-01-05 07:19:48 +01:00
|
|
|
return ($span != 1) ? "{$span} "._t("Date.HOURS", " hours") : "{$span} "._t("Date.HOUR", " hour");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
if($ago < 86400*30) {
|
|
|
|
$span = round($ago/86400);
|
2009-01-05 07:19:48 +01:00
|
|
|
return ($span != 1) ? "{$span} "._t("Date.DAYS", " days") : "{$span} "._t("Date.DAY", " day");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
if($ago < 86400*365) {
|
|
|
|
$span = round($ago/86400/30);
|
2009-01-05 07:19:48 +01:00
|
|
|
return ($span != 1) ? "{$span} "._t("Date.MONTHS", " months") : "{$span} "._t("Date.MONTH", " month");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
if($ago > 86400*365) {
|
|
|
|
$span = round($ago/86400/365);
|
2009-01-05 07:19:48 +01:00
|
|
|
return ($span != 1) ? "{$span} "._t("Date.YEARS", " years") : "{$span} "._t("Date.YEAR", " year");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the time difference, but always returns it in a certain format
|
|
|
|
* @param string $format The format, could be one of these:
|
|
|
|
* 'seconds', 'minutes', 'hours', 'days', 'months', 'years'.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function TimeDiffIn($format) {
|
|
|
|
if($this->value) {
|
|
|
|
$ago = abs(time() - strtotime($this->value));
|
|
|
|
|
|
|
|
switch($format) {
|
|
|
|
case "seconds":
|
|
|
|
$span = $ago;
|
|
|
|
return ($span != 1) ? "{$span} seconds" : "{$span} second";
|
|
|
|
break;
|
|
|
|
case "minutes":
|
|
|
|
$span = round($ago/60);
|
|
|
|
return ($span != 1) ? "{$span} minutes" : "{$span} minute";
|
|
|
|
break;
|
|
|
|
case "hours":
|
|
|
|
$span = round($ago/3600);
|
|
|
|
return ($span != 1) ? "{$span} hours" : "{$span} hour";
|
|
|
|
break;
|
|
|
|
case "days":
|
|
|
|
$span = round($ago/86400);
|
|
|
|
return ($span != 1) ? "{$span} days" : "{$span} day";
|
|
|
|
break;
|
|
|
|
case "months":
|
|
|
|
$span = round($ago/86400/30);
|
|
|
|
return ($span != 1) ? "{$span} months" : "{$span} month";
|
|
|
|
break;
|
|
|
|
case "years":
|
|
|
|
$span = round($ago/86400/365);
|
|
|
|
return ($span != 1) ? "{$span} years" : "{$span} year";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
function requireField() {
|
2009-09-30 00:00:36 +02:00
|
|
|
$parts=Array('datatype'=>'date', 'arrayValue'=>$this->arrayValue);
|
2008-11-23 02:01:03 +01:00
|
|
|
$values=Array('type'=>'date', 'parts'=>$parts);
|
|
|
|
DB::requireField($this->tableName, $this->name, $values);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2009-01-12 03:02:45 +01:00
|
|
|
/**
|
|
|
|
* Returns true if date is in the past.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function InPast() {
|
2009-01-12 03:02:45 +01:00
|
|
|
return strtotime($this->value) < time();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if date is in the future.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function InFuture() {
|
|
|
|
return strtotime($this->value) > time();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if date is today.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function IsToday() {
|
|
|
|
return (date('Y-m-d', strtotime($this->value)) == date('Y-m-d', time()));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a date suitable for insertion into a URL and use by the system.
|
|
|
|
*/
|
|
|
|
function URLDate() {
|
|
|
|
return date('Y-m-d', strtotime($this->value));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function days_between($fyear, $fmonth, $fday, $tyear, $tmonth, $tday){
|
|
|
|
return abs((mktime ( 0, 0, 0, $fmonth, $fday, $fyear) - mktime ( 0, 0, 0, $tmonth, $tday, $tyear))/(60*60*24));
|
|
|
|
}
|
|
|
|
|
|
|
|
function day_before($fyear, $fmonth, $fday){
|
|
|
|
return date ("Y-m-d", mktime (0,0,0,$fmonth,$fday-1,$fyear));
|
|
|
|
}
|
|
|
|
|
|
|
|
function next_day($fyear, $fmonth, $fday){
|
|
|
|
return date ("Y-m-d", mktime (0,0,0,$fmonth,$fday+1,$fyear));
|
|
|
|
}
|
|
|
|
|
|
|
|
function weekday($fyear, $fmonth, $fday){ // 0 is a Monday
|
|
|
|
return (((mktime ( 0, 0, 0, $fmonth, $fday, $fyear) - mktime ( 0, 0, 0, 7, 17, 2006))/(60*60*24))+700000) % 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
function prior_monday($fyear, $fmonth, $fday){
|
|
|
|
return date ("Y-m-d", mktime (0,0,0,$fmonth,$fday-weekday($fyear, $fmonth, $fday),$fyear));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-02-10 06:56:07 +01:00
|
|
|
* Return the nearest date in the past, based on day and month.
|
|
|
|
* Automatically attaches the correct year.
|
2007-07-19 12:40:28 +02:00
|
|
|
*
|
2009-02-10 06:56:07 +01:00
|
|
|
* This is useful for determining a financial year start or end date.
|
|
|
|
*
|
|
|
|
* @param $fmonth int The number of the month (e.g. 3 is March, 4 is April)
|
|
|
|
* @param $fday int The day of the month
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param $fyear int Determine historical value
|
2009-02-10 06:56:07 +01:00
|
|
|
* @return string Date in YYYY-MM-DD format
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
static function past_date($fmonth, $fday = 1, $fyear = null) {
|
|
|
|
if(!$fyear) $fyear = date('Y');
|
2009-02-10 06:56:07 +01:00
|
|
|
$fday = (int) $fday;
|
|
|
|
$fmonth = (int) $fmonth;
|
|
|
|
$fyear = (int) $fyear;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2009-02-10 06:56:07 +01:00
|
|
|
$pastDate = mktime(0, 0, 0, $fmonth, $fday, $fyear);
|
|
|
|
$curDate = mktime(0, 0, 0, date('m'), date('d'), $fyear);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
if($pastDate < $curDate) {
|
2009-02-10 06:56:07 +01:00
|
|
|
return date('Y-m-d', mktime(0, 0, 0, $fmonth, $fday, $fyear));
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2009-02-10 06:56:07 +01:00
|
|
|
return date('Y-m-d', mktime(0, 0, 0, $fmonth, $fday, $fyear - 1));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2008-08-06 04:43:46 +02:00
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
public function scaffoldFormField($title = null, $params = null) {
|
2008-08-06 04:43:46 +02:00
|
|
|
return new DateField($this->name, $title);
|
|
|
|
}
|
2009-04-21 00:32:13 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-04-21 00:32:13 +02:00
|
|
|
?>
|