2006-12-02 04:36:16 +00:00
< ? php
/*
2009-10-30 17:59:32 +00:00
* e107 website system
*
2009-11-18 01:49:18 +00:00
* Copyright ( C ) 2008 - 2009 e107 Inc ( e107 . org )
2009-10-30 17:59:32 +00:00
* Released under the terms and conditions of the
* GNU General Public License ( http :// gnu . org ) .
*
2006-12-02 04:36:16 +00:00
*/
2019-01-11 11:39:20 +01:00
2006-12-02 04:36:16 +00:00
if ( ! defined ( 'e107_INIT' )) { exit ; }
2019-01-11 11:39:20 +01:00
//e107::includeLan(e_LANGUAGEDIR.e_LANGUAGE."/lan_date.php");
2019-01-11 11:47:00 +01:00
e107 :: coreLan ( 'date' );
2006-12-02 04:36:16 +00:00
2018-05-11 08:39:01 -07:00
class e_date
2006-12-02 04:36:16 +00:00
{
2012-05-26 12:21:39 +00:00
function __construct ()
{
2018-03-04 13:02:50 -08:00
2012-05-26 12:21:39 +00:00
}
2015-07-26 17:33:15 -07:00
/**
* Build the datetimepicker () locale , since it must match strftime () values for accurate conversion .
*/
function buildDateLocale ()
{
$text = '
( function ( $ ){
$ . fn . datetimepicker . dates [ " '.e_LAN.' " ] = { ' ;
$dates = array ();
for ( $i = 1 ; $i < 8 ; $i ++ )
{
$day = strftime ( '%A' , mktime ( 1 , 1 , 1 , 1 , $i , 2012 ));
$dates [ 'days' ][] = $day ;
$dates [ 'daysShort' ][] = strftime ( '%a' , mktime ( 1 , 1 , 1 , 1 , $i , 2012 ));
$dates [ 'daysMin' ][] = substr ( $day , 0 , 2 );
}
for ( $i = 1 ; $i < 13 ; $i ++ )
{
$dates [ 'months' ][] = strftime ( '%B' , mktime ( 1 , 1 , 1 , $i , 2 , 2013 ));
$dates [ 'monthsShort' ][] = strftime ( '%h' , mktime ( 1 , 1 , 1 , $i , 2 , 2013 ));
}
foreach ( $dates as $key => $type )
{
$d = array ();
$text .= " \n " . $key . " : [ " ;
foreach ( $type as $val )
{
$d [] = '"' . $val . '"' ;
}
$text .= implode ( " , " , $d );
$text .= " ], " ;
}
$text .= '
meridiem : [ " am " , " pm " ]
};
}( jQuery )); ' ;
return $text ;
}
2014-06-02 17:07:02 -07:00
/**
* Return an array of language terms representing months
* @ param $type string : month , month - short , day , day - short , day - shortest
2018-05-11 08:39:01 -07:00
* @ return array | bool
2014-06-02 17:07:02 -07:00
*/
public function terms ( $type = 'month' )
{
if ( $type == 'month' || $type == 'month-short' )
{
$val = ( $type == 'month-short' ) ? '%b' : '%B' ; //eg. 'Aug' / 'August'
$marray = array ();
for ( $i = 1 ; $i < 13 ; $i ++ )
{
2017-04-06 11:02:54 -07:00
$marray [ $i ] = strftime ( $val , mktime ( 1 , 1 , 1 , $i , 1 , 2000 ));
2014-06-02 17:07:02 -07:00
}
return $marray ;
}
if ( substr ( $type , 0 , 3 ) == 'day' )
{
$days = array ();
for ( $i = 2 ; $i < 9 ; $i ++ )
{
switch ( $type )
{
case 'day-shortest' : // eg. 'Tu'
$days [] = substr ( strftime ( '%a' , mktime ( 1 , 1 , 1 , 6 , $i , 2014 )), 0 , 2 );
break ;
case 'day-short' : // eg. 'Tue'
$days [] = strftime ( '%a' , mktime ( 1 , 1 , 1 , 6 , $i , 2014 ));
break ;
default : // eg. 'Tuesday'
$days [] = strftime ( '%A' , mktime ( 1 , 1 , 1 , 6 , $i , 2014 ));
break ;
}
}
return $days ;
}
return false ;
}
2009-10-22 19:14:26 +00:00
/**
* Convert datestamp to human readable date .
* System time offset is considered .
*
* @ param integer $datestamp unix stamp
2013-04-26 21:49:09 -07:00
* @ param string $mask [ optional ] long | short | forum | relative or any strftime () valid string
2009-10-22 19:14:26 +00:00
*
* @ return string parsed date
*/
2016-09-28 10:26:39 -07:00
function convert_date ( $datestamp , $mask = '' )
{
2009-10-22 19:14:26 +00:00
if ( empty ( $mask ))
{
$mask = 'long' ;
}
2018-03-04 13:02:50 -08:00
2009-10-22 19:14:26 +00:00
switch ( $mask )
{
case 'long' :
$mask = e107 :: getPref ( 'longdate' );
2012-09-03 23:01:50 +00:00
// $datestamp += TIMEOFFSET;
2009-10-22 19:14:26 +00:00
break ;
case 'short' :
$mask = e107 :: getPref ( 'shortdate' );
2012-09-03 23:01:50 +00:00
// $datestamp += TIMEOFFSET;
2009-10-22 19:14:26 +00:00
break ;
2012-05-26 12:21:39 +00:00
case 'input' :
case 'inputdate' :
$mask = e107 :: getPref ( 'inputdate' , '%d/%m/%Y %H:%M' );
// $mask .= " ".e107::getPref('inputtime', '%H:%M');
break ;
case 'inputdatetime' :
$mask = e107 :: getPref ( 'inputdate' , '%d/%m/%Y %H:%M' );
$mask .= " " . e107 :: getPref ( 'inputtime' , '%H:%M' );
break ;
case 'inputtime' :
2018-05-15 21:54:43 +02:00
$mask = e107 :: getPref ( 'inputtime' , '%H:%M' );
2009-10-30 17:59:32 +00:00
break ;
2009-10-22 19:14:26 +00:00
case 'forum' : // DEPRECATED - temporary here from BC reasons only
2012-12-05 16:43:53 -08:00
// default:
2009-10-22 19:14:26 +00:00
//BC - old 'forum' call
2009-10-23 13:06:21 +00:00
if ( strpos ( $mask , '%' ) === FALSE )
2009-10-22 19:14:26 +00:00
{
$mask = e107 :: getPref ( 'forumdate' );
}
2012-09-03 23:01:50 +00:00
// $datestamp += TIMEOFFSET;
2009-10-22 19:14:26 +00:00
break ;
2013-01-22 00:20:44 -08:00
2013-04-26 21:49:09 -07:00
case 'relative' :
2019-02-09 16:15:32 +01:00
return $this -> computeLapse ( $datestamp , time (), false , true , 'short' ) ;
2013-04-26 21:49:09 -07:00
break ;
2013-01-22 00:20:44 -08:00
default :
if ( strpos ( $mask , '%' ) === FALSE )
{
2013-01-22 13:11:29 -08:00
$mask = $this -> toMask ( $mask , true );
2013-01-22 00:20:44 -08:00
}
break ;
2009-10-22 19:14:26 +00:00
}
2016-09-20 11:28:17 +02:00
$dateString = strftime ( $mask , $datestamp );
2016-09-28 10:26:39 -07:00
if ( ! e107 :: getParser () -> isUTF8 ( $dateString ))
{
2016-09-20 11:28:17 +02:00
$dateString = utf8_encode ( $dateString );
}
return $dateString ;
}
2009-11-27 21:42:46 +00:00
2012-05-26 12:21:39 +00:00
/**
2016-05-31 13:25:43 -07:00
* @ deprecated - for internal use only .
* @ see $tp -> toDate () as a replacement .
2012-05-26 12:21:39 +00:00
* Converts between unix timestamp and human - readable date - time OR vice - versa . ( auto - detected )
2015-02-15 02:37:36 -08:00
* @ param string $string unix timestamp OR human - readable date - time .
2012-05-26 12:21:39 +00:00
* @ param string $mask ( optional ) long | short | input
2015-02-15 02:37:36 -08:00
* @ return bool | int | string
2012-05-26 12:21:39 +00:00
*/
function convert ( $string = null , $mask = 'inputdate' )
{
if ( $string == null ) return false ;
2018-03-04 13:02:50 -08:00
return is_numeric ( $string ) ? $this -> convert_date ( $string , $mask ) : $this -> toTime ( $string , $mask );
2012-05-26 12:21:39 +00:00
}
2012-12-05 16:43:53 -08:00
2018-05-24 07:39:54 -07:00
2012-12-05 16:43:53 -08:00
/**
2018-05-11 14:58:04 -07:00
* Converts to new date - mask format or vice - versa when $legacy is TRUE
2018-05-24 07:39:54 -07:00
*
* string $mask
* string | bool $legacy false = strftime > datetimepicker , true = datetimepicker > strftime , 'DateTime' = strftime > DateTime format .
* @ see https :// secure . php . net / manual / en / function . strftime . php
2018-05-11 14:58:04 -07:00
* @ see https :// github . com / AuspeXeu / bootstrap - datetimepicker
2018-05-24 07:39:54 -07:00
* @ see https :// secure . php . net / manual / en / datetime . createfromformat . php
2012-12-05 16:43:53 -08:00
*/
2013-01-22 00:20:44 -08:00
function toMask ( $mask , $legacy = false )
2012-12-05 16:43:53 -08:00
{
2018-05-24 07:39:54 -07:00
//strftime() -> datetimepicker format.
2012-12-05 16:43:53 -08:00
$convert = array (
2018-05-24 07:39:54 -07:00
'%Y' => 'yyyy' , // Year 4-digits '2013'
'%d' => 'dd' , // day of the month 2-digits
'%m' => 'mm' , // month number 2-digits
2012-12-05 16:43:53 -08:00
'%B' => 'MM' , // Full month name, based on the locale
'%A' => 'DD' , // A full textual representation of the day
2013-03-16 18:35:20 -07:00
'%y' => 'yy' ,
2012-12-05 16:43:53 -08:00
'%a' => 'D' , // An abbreviated textual representation of the day
'%b' => 'M' , // Abbreviated month name, based on the locale
'%h' => 'M' , // Abbreviated month name, based on the locale (an alias of %b)
2013-05-25 16:03:53 -07:00
'%I' => 'HH' , // Two digit representation of the hour in 12-hour format
'%l' => 'H' , // 12 hour format - no leading zero
2012-12-05 16:43:53 -08:00
'%H' => 'hh' , // 24 hour format - leading zero
2013-05-25 16:03:53 -07:00
'%M' => 'ii' , // Two digit representation of the minute
'%S' => 'ss' , // Two digit representation of the second
2018-03-15 22:27:25 -07:00
'%P' => 'p' , // %P lower-case 'am' or 'pm' based on the given time
2018-05-24 07:39:54 -07:00
'%p' => 'P' , // %p UPPER-CASE 'AM' or 'PM' based on the given time
2018-03-15 22:27:25 -07:00
2012-12-05 16:43:53 -08:00
'%T' => 'hh:mm:ss' ,
'%r' => " hh:mmm:ss TT " // 12 hour format
);
2018-05-24 07:39:54 -07:00
// strftime() > DateTime::
if ( $legacy === 'DateTime' )
{
$convert = array (
'%Y' => 'Y' , // Year 4-digits '2013'
'%d' => 'd' , // Two-digit day of the month (with leading zeros) (01 through 31)
'%e' => 'j' , // Day of the month, with a space preceding single digits. Not implemented on Windows with strftime.
'%m' => 'm' , // Two digit representation of the month (01 throught 12)
'%B' => 'F' , // Full month name, based on the locale
'%A' => 'l' , // A full textual representation of the day
'%y' => 'y' ,
'%a' => 'D' , // An abbreviated textual representation of the day
'%b' => 'M' , // Abbreviated month name, based on the locale
'%h' => 'M' , // Abbreviated month name, based on the locale (an alias of %b)
'%k' => 'G' , // Hour in 24-hour format, with a space preceding single digits (0 through 23)
'%I' => 'h' , // Two digit representation of the hour in 12-hour format ( 01 through 12)
'%l' => 'g' , // 12 hour format - no leading zero (1 through 12)
'%H' => 'H' , // Two digit representation of the hour in 24-hour format (00 through 23)
'%M' => 'i' , // Two digit representation of the minute (00 through 59)
'%S' => 's' , // Two digit representation of the second (00 through 59)
'%P' => 'a' , // lower-case 'am' or 'pm' based on the given time
'%p' => 'A' , // UPPER-CASE 'AM' or 'PM' based on the given time
'%Z' => 'e' , // The time zone abbreviation. Not implemented as described on Windows with strftime.
// TODO Add anything that is missing.
// '%T' => 'hh:mm:ss',
// '%r' => "hh:mmm:ss TT" // 12 hour format
);
}
2012-12-05 16:43:53 -08:00
$s = array_keys ( $convert );
$r = array_values ( $convert );
2018-05-11 14:58:04 -07:00
if ( strpos ( $mask , '%' ) === false && $legacy === true )
2012-12-05 16:43:53 -08:00
{
2018-05-11 14:58:04 -07:00
$ret = str_replace ( $r , $s , $mask );
return str_replace ( '%%p' , '%P' , $ret ); // quick fix.
2012-12-05 16:43:53 -08:00
}
2018-05-11 14:58:04 -07:00
elseif ( strpos ( $mask , '%' ) !== false )
2013-03-05 06:11:05 -08:00
{
2018-05-11 14:58:04 -07:00
return str_replace ( $s , $r , $mask );
2013-03-05 06:11:05 -08:00
}
2012-12-05 16:43:53 -08:00
2013-03-05 06:11:05 -08:00
return $mask ;
2012-12-05 16:43:53 -08:00
// Keep this info here:
/*
* $options allowed keys :
*
* d - day of month ( no leading zero )
dd - day of month ( two digit )
o - day of the year ( no leading zeros )
oo - day of the year ( three digit )
D - day name short
DD - day name long
m - month of year ( no leading zero )
mm - month of year ( two digit )
M - month name short
MM - month name long
y - year ( two digit )
yy - year ( four digit )
@ - Unix timestamp ( ms since 01 / 01 / 1970 )
! - Windows ticks ( 100 ns since 01 / 01 / 0001 )
'...' - literal text
'' - single quote
anything else - literal text
ATOM - 'yy-mm-dd' ( Same as RFC 3339 / ISO 8601 )
COOKIE - 'D, dd M yy'
ISO_8601 - 'yy-mm-dd'
RFC_822 - 'D, d M y' ( See RFC 822 )
RFC_850 - 'DD, dd-M-y' ( See RFC 850 )
RFC_1036 - 'D, d M y' ( See RFC 1036 )
RFC_1123 - 'D, d M yy' ( See RFC 1123 )
RFC_2822 - 'D, d M yy' ( See RFC 2822 )
RSS - 'D, d M y' ( Same as RFC 822 )
TICKS - '!'
TIMESTAMP - '@'
W3C - 'yy-mm-dd' ( Same as ISO 8601 )
*
* h Hour with no leading 0
* hh Hour with leading 0
* m Minute with no leading 0
* mm Minute with leading 0
* s Second with no leading 0
* ss Second with leading 0
* l Milliseconds always with leading 0
* t a or p for AM / PM
* T A or P for AM / PM
* tt am or pm for AM / PM
* TT AM or PM for AM / PM
*/
}
2012-05-26 12:21:39 +00:00
2009-10-30 17:59:32 +00:00
2009-11-10 19:13:07 +00:00
/**
* Convert date string back to integer ( unix timestamp )
* NOTE : after some tests , strptime ( compat mode ) is adding + 1 sec . after parsing to time , investigate !
*
2015-07-31 22:30:26 +03:00
* @ param string $date_string
* @ param string $mask [ optional ]
2009-12-13 21:52:32 +00:00
* @ return integer
2009-11-10 19:13:07 +00:00
*/
function toTime ( $date_string , $mask = 'input' )
2009-10-30 17:59:32 +00:00
{
2009-11-10 19:13:07 +00:00
switch ( $mask )
{
case 'long' :
$mask = e107 :: getPref ( 'longdate' );
break ;
case 'short' :
$mask = e107 :: getPref ( 'shortdate' );
break ;
2012-05-26 12:21:39 +00:00
case 'input' :
case 'inputdate' :
$mask = e107 :: getPref ( 'inputdate' , '%Y/%m/%d' );
break ;
case 'inputdatetime' :
$mask = e107 :: getPref ( 'inputdate' , '%Y/%m/%d' );
$mask .= " " . e107 :: getPref ( 'inputtime' , '%H:%M' );
break ;
case 'inputtime' :
$mask = e107 :: getPref ( 'inputtime' , '%H:%M' );
2009-11-10 19:13:07 +00:00
break ;
}
2018-05-24 07:39:54 -07:00
// convert to PHP 5+ @see https://secure.php.net/manual/en/datetime.createfromformat.php
$newMask = $this -> toMask ( $mask , 'DateTime' );
$tdata = date_parse_from_format ( $newMask , $date_string );
return mktime (
$tdata [ 'hour' ],
$tdata [ 'minute' ],
$tdata [ 'second' ],
$tdata [ 'month' ] ,
$tdata [ 'day' ],
$tdata [ 'year' ]
);
// also in php compat handler for plugins that might use it.
/*
2012-05-26 12:21:39 +00:00
$tdata = $this -> strptime ( $date_string , $mask );
2009-11-10 19:13:07 +00:00
if ( empty ( $tdata ))
{
2013-11-22 01:30:06 -08:00
if ( ! empty ( $date_string ) && ADMIN )
{
e107 :: getMessage () -> addDebug ( " PROBLEM WITH CONVERSION from " . $date_string . " to unix timestamp " );
}
2009-11-10 19:13:07 +00:00
return null ;
}
2013-11-09 20:08:41 -08:00
2014-02-18 05:41:58 -08:00
if ( STRPTIME_COMPAT !== TRUE ) // returns months from 0 - 11 on Unix so we need to +1
{
$tdata [ 'tm_mon' ] = $tdata [ 'tm_mon' ] + 1 ;
}
2009-11-10 19:13:07 +00:00
$unxTimestamp = mktime (
$tdata [ 'tm_hour' ],
$tdata [ 'tm_min' ],
$tdata [ 'tm_sec' ],
2014-02-18 05:41:58 -08:00
$tdata [ 'tm_mon' ] ,
2009-11-10 19:13:07 +00:00
$tdata [ 'tm_mday' ],
2012-05-26 12:21:39 +00:00
( $tdata [ 'tm_year' ] + 1900 )
2009-11-10 19:13:07 +00:00
);
2012-09-03 23:01:50 +00:00
2009-11-10 19:13:07 +00:00
return $unxTimestamp ;
2018-05-24 07:39:54 -07:00
*/
2009-10-30 17:59:32 +00:00
}
2006-12-02 04:36:16 +00:00
2012-05-26 12:21:39 +00:00
// -----------------------
2009-11-27 21:42:46 +00:00
/**
* Tolerant date / time input routine - doesn ' t force use of specific delimiters , and can sometimes allow no delimiters at all
* The format string defines the critical day / month / year order .
* As examples , with suitable format specifiers all of the following will be interpreted into valid ( and sensible ) dates / times :
* 09122003 153045 -> 9 - 12 - 03 at 15 : 30 : 45 ( requires dmy or mdy format specifier )
* 20031209 12 : 30 : 32 -> 9 - 12 - 03 at 12 : 30 : 32 ( requires ymd specifier )
* 091203 1530 -> 9 - 12 - 09 at 15 : 30 : 00
* 9 / 12 / 3 12 -> 9 - 12 - 09 at 12 : 00 : 00
* 6 - 3 / 4 15 - 45 : 27 -> 6 - 3 - 04 at 15 : 45 : 27
*
* @ param string $input - usually date / time string with numeric values for relevant fields , and almost any separator . e . g . dd - mm - yy hh : mm
* Date and time must be separated by one or more spaces . In times , minutes and seconds are optional , and default to zero
* One special value is allowed - 'now'
2009-12-01 20:05:54 +00:00
* @ param string $decode - one of 'date' , 'time' , 'datetime' , 'timedate'
2009-11-27 21:42:46 +00:00
* @ param string $format - sets field order for dates . Valid strings are dmy , mdy , ymd . Add suffix 'z' to return UTC / GMT
* @ param boolean $endDay - if TRUE , and no time entered , includes a time of 23 : 59 : 59 in the entered date
*
* @ return integer time stamp . returns zero on any error
*/
public function decodeDateTime ( $input , $decode = 'date' , $format = 'dmy' , $endDay = FALSE )
{
2009-12-01 20:05:54 +00:00
if ( $input == 'now' ) return time (); // Current time TODO: option to return UTC or local time here
2009-11-27 21:42:46 +00:00
$useLocale = TRUE ;
if ( substr ( $format , - 1 , 1 ) == 'z' )
{
$useLocale = FALSE ;
$format = substr ( $format , 0 , - 1 ); // Remove local disable string
}
switch ( $decode )
{
case 'date' :
$timeString = '' ;
2009-12-01 20:05:54 +00:00
$dateString = $input ;
2009-11-27 21:42:46 +00:00
break ;
case 'time' :
2009-12-01 20:05:54 +00:00
$timeString = $input ;
2009-11-27 21:42:46 +00:00
$dateString = '' ;
break ;
2009-12-01 20:05:54 +00:00
case 'datetime' : // Date then time, separated by space
2009-11-27 21:42:46 +00:00
$input = str_replace ( ' ' , ' ' , $input );
list ( $dateString , $timeString ) = explode ( ' ' , $input , 2 );
2009-12-01 20:05:54 +00:00
break ;
case 'timedate' : // Time then date, separated by space
$input = str_replace ( ' ' , ' ' , $input );
list ( $timeString , $dateString ) = explode ( ' ' , $input , 2 );
2009-11-27 21:42:46 +00:00
break ;
default :
return 0 ;
}
2009-12-01 20:05:54 +00:00
$timeString = trim ( $timeString );
$dateString = trim ( $dateString );
2009-11-27 21:42:46 +00:00
$dateVals = array ( 1 => 0 , 2 => 0 , 3 => 0 ); // Preset date in case
$timeVals = array ( 1 => 0 , 2 => 0 , 3 => 0 ); // Preset time in case
if ( $dateString )
{
if ( is_numeric ( $dateString ))
{
if ( strlen ( $dateString ) == 6 )
{ // Probably fixed format numeric without separators
$dateVals = array ( 1 => substr ( $dateString , 0 , 2 ), 2 => substr ( $dateString , 2 , 2 ), 3 => substr ( $dateString , - 2 ));
}
elseif ( strlen ( $dateString ) == 8 )
{ // Trickier - year may be first or last!
if ( $format == 'ymd' )
{
$dateVals = array ( 1 => substr ( $dateString , 0 , 4 ), 2 => substr ( $dateString , 4 , 2 ), 3 => substr ( $dateString , - 2 ));
}
else
{
$dateVals = array ( 1 => substr ( $dateString , 0 , 2 ), 2 => substr ( $dateString , 2 , 2 ), 3 => substr ( $dateString , - 4 ));
}
}
}
else
{ // Assume standard 'nn-nn-nn', 'nnnn-nn-nn' or 'nn-nn-nnnn' type format
if ( ! preg_match ( '#(\d{1,4})\D(\d{1,2})\D(\d{1,4})#' , $dateString , $dateVals ))
{
return 0 ; // Can't decode date
}
}
}
if ( $timeString )
{
if ( is_numeric ( $timeString ))
{
if ( strlen ( $timeString ) == 6 )
{ // Assume hhmmss
$timeVals = array ( 1 => substr ( $timeString , 0 , 2 ), 2 => substr ( $timeString , 2 , 2 ), 3 => substr ( $timeString , - 2 ));
}
elseif ( strlen ( $timeString ) == 4 )
{ // Assume hhmm
$timeVals = array ( 1 => substr ( $timeString , 0 , 2 ), 2 => substr ( $timeString , - 2 ), 3 => 0 );
}
else
{ // Hope its just hours!
if ( $timeString < 24 )
{
$timeVals [ 1 ] = $timeString ;
}
}
}
else
{
2020-12-18 13:07:45 -08:00
preg_match ( '#(\d{1,2})(?:\D(\d{1,2}))?(?:\D(\d{1,2}))?#' , $timeString , $timeVals );
2009-11-27 21:42:46 +00:00
}
}
elseif ( $endDay )
{
$timeVals = array ( 1 => 23 , 2 => 59 , 3 => 59 ); // Last second of day
}
// Got all the values now - the rest is simple!
switch ( $format )
{
case 'dmy' :
$month = $dateVals [ 2 ]; $day = $dateVals [ 1 ]; $year = $dateVals [ 3 ]; break ;
case 'mdy' :
$month = $dateVals [ 1 ]; $day = $dateVals [ 2 ]; $year = $dateVals [ 3 ]; break ;
case 'ymd' :
$month = $dateVals [ 2 ]; $day = $dateVals [ 3 ]; $year = $dateVals [ 1 ]; break ;
default :
echo " Unsupported format string: { $format } <br /> " ;
return 0 ;
}
if ( $useLocale )
{
return mktime ( $timeVals [ 1 ], $timeVals [ 2 ], $timeVals [ 3 ], $month , $day , $year );
}
return gmmktime ( $timeVals [ 1 ], $timeVals [ 2 ], $timeVals [ 3 ], $month , $day , $year );
}
/**
* Calculate difference between two dates for display in terms of years / months / weeks ....
*
* @ param integer $older_date - time stamp
* @ param integer | boolean $newer_date - time stamp . Defaults to current time if FALSE
* @ param boolean $mode - if TRUE , return value is an array . Otherwise return value is a string
* @ param boolean $show_secs
* @ param string $format - controls display format . 'short' misses off year . 'long' includes everything
* @ return array | string according to $mode , array or string detailing the time difference
*/
2006-12-02 04:36:16 +00:00
function computeLapse ( $older_date , $newer_date = FALSE , $mode = FALSE , $show_secs = TRUE , $format = 'long' )
2019-01-15 20:30:18 -08:00
{
if ( $newer_date === false )
{
$newer_date = time ();
}
2006-12-02 04:36:16 +00:00
2019-01-15 20:30:18 -08:00
if ( $format === 'short' )
2006-12-02 04:36:16 +00:00
{
$sec = LANDT_09 ;
$secs = LANDT_09s ;
$min = LANDT_08 ;
$mins = LANDT_08s ;
}
else
{
$sec = LANDT_07 ;
$secs = LANDT_07s ;
$min = LANDT_06 ;
$mins = LANDT_06s ;
}
2019-01-15 20:30:18 -08:00
$dateString1 = date ( " Y-m-d H:i:s " , $older_date );
$dateString2 = date ( " Y-m-d H:i:s " , $newer_date );
$date1 = new DateTime ( $dateString1 );
$date2 = new DateTime ( $dateString2 );
$interval = $date1 -> diff ( $date2 );
$result = array (
'years' => array ( $interval -> y , LANDT_01 , LANDT_01s ),
'months' => array ( $interval -> m , LANDT_02 , LANDT_02s ),
'weeks' => array ( floor ( $interval -> d / 7 ), LANDT_03 , LANDT_03s ),
'days' => array ( $interval -> d % 7 , LANDT_04 , LANDT_04s ),
'hours' => array ( $interval -> h , LANDT_05 , LANDT_05s ),
'minutes' => array ( $interval -> i , $min , $mins ),
'seconds' => array ( $interval -> s , $sec , $secs ),
);
if ( $show_secs !== true )
{
unset ( $result [ 'seconds' ]);
}
$ret = array ();
foreach ( $result as $val )
{
if ( $val [ 0 ] < 1 )
{
continue ;
}
$ret [] = ( $val [ 0 ] == 1 ) ? $val [ 0 ] . " " . $val [ 1 ] : $val [ 0 ] . " " . $val [ 2 ];
if ( $format === 'short' ) { break ; }
}
2020-05-02 11:39:03 -07:00
if ( strpos ( $ret [ 0 ], $sec ) !== false )
2019-01-15 20:30:18 -08:00
{
$justNow = deftrue ( 'LANDT_10' , " Just now " );
return $mode ? array ( $justNow ) : $justNow ;
}
2019-02-11 11:03:15 +01:00
if ( $older_date < $newer_date ) // past
{
$replace = implode ( " , " , $ret );
$xago = e107 :: getParser () -> lanVars ( LANDT_XAGO , $replace );
return ( $mode ? $ret : $xago );
}
else // future
{
$replace = implode ( " , " , $ret );
$inx = e107 :: getParser () -> lanVars ( LANDT_INX , $replace );
return ( $mode ? $ret : $inx );
}
2019-02-09 17:24:49 +01:00
2019-01-15 20:30:18 -08:00
// print_r($ret);
2006-12-22 20:46:53 +00:00
/*
If we want an absolutely accurate result , main problems arise from the varying numbers of days in a month .
If we go over a month boundary , then we need to add days to end of start month , plus days in 'end' month
If start day > end day , we cross a month boundary . Calculate last day of start date . Otherwise we can just do a simple difference .
*/
2019-01-15 20:30:18 -08:00
/*
2009-11-27 21:42:46 +00:00
$newer_date = ( $newer_date === FALSE ? ( time ()) : $newer_date );
2006-12-23 15:44:31 +00:00
if ( $older_date > $newer_date )
{ // Just in case the wrong way round
$tmp = $newer_date ;
$newer_date = $older_date ;
$older_date = $tmp ;
}
2006-12-22 20:46:53 +00:00
$new_date = getdate ( $newer_date );
$old_date = getdate ( $older_date );
$result = array ();
$outputArray = array ();
$params = array (
6 => array ( 'seconds' , 60 , $sec , $secs ),
5 => array ( 'minutes' , 60 , $min , $mins ),
4 => array ( 'hours' , 24 , LANDT_05 , LANDT_05s ),
3 => array ( 'mday' , - 1 , LANDT_04 , LANDT_04s ),
2 => array ( '' , - 3 , LANDT_03 , LANDT_03s ),
1 => array ( 'mon' , 12 , LANDT_02 , LANDT_02s ),
0 => array ( 'year' , - 2 , LANDT_01 , LANDT_01s )
);
2006-12-02 04:36:16 +00:00
2006-12-22 20:46:53 +00:00
$cy = 0 ;
foreach ( $params as $parkey => $parval )
2006-12-02 04:36:16 +00:00
{
2006-12-22 20:46:53 +00:00
if ( $parkey == 2 )
{
$result [ '2' ] = floor ( $result [ '3' ] / 7 );
$result [ '3' ] = fmod ( $result [ '3' ], 7 );
}
else
{
$tmp = $new_date [ $parval [ 0 ]] - $old_date [ $parval [ 0 ]] - $cy ;
$scy = $cy ;
$cy = 0 ;
if ( $tmp < 0 )
{
switch ( $parval [ 1 ])
{
case - 1 : // Wrapround on months - special treatment
$tempdate = getdate ( mktime ( 0 , 0 , 0 , $old_date [ 'mon' ] + 1 , 1 , $old_date [ 'year' ]) - 1 ); // Last day of month
$tmp = $tempdate [ 'mday' ] - $old_date [ 'mday' ] + $new_date [ 'mday' ] - $scy ;
$cy = 1 ;
break ;
case - 2 : // Year wraparound - shouldn't happen
case - 3 : // Week processing - this shouldn't happen either
echo " Code bug!<br /> " ;
break ;
default :
$cy = 1 ;
$tmp += $parval [ 1 ];
}
}
$result [ $parkey ] = $tmp ;
}
2006-12-02 04:36:16 +00:00
}
2006-12-22 20:46:53 +00:00
// Generate output array, add text
for ( $i = 0 ; $i < ( $show_secs ? 7 : 6 ); $i ++ )
2006-12-02 04:36:16 +00:00
{
2007-02-18 13:10:26 +00:00
if (( $i > 4 ) || ( $result [ $i ] != 0 ))
{ // Only show non-zero values, except always show minutes/seconds
2013-03-12 04:22:09 -07:00
$outputArray [] = $result [ $i ] . " " . ( $result [ $i ] == 1 ? $params [ $i ][ 2 ] : $params [ $i ][ 3 ]) ;
2013-07-08 10:37:40 -07:00
2007-02-18 13:10:26 +00:00
}
2013-07-08 10:37:40 -07:00
if ( $format == 'short' && count ( $outputArray ) == 1 ) { break ; }
2006-12-02 04:36:16 +00:00
}
2016-05-21 09:45:15 -07:00
if ( empty ( $outputArray [ 1 ]) && ( $outputArray [ 0 ] == " 0 " . $mins ))
{
return deftrue ( 'LANDT_10' , " Just now " );
}
2019-01-11 11:39:20 +01:00
// Check if it is 'past' or 'future'
if ( $older_date > $newer_date ) // past
{
return ( $mode ? $outputArray : implode ( " , " , $outputArray ) . " " . LANDT_AGO );
}
else // future
{
return ( $mode ? $outputArray : LANDT_IN . " " . implode ( " , " , $outputArray ));
}
2019-01-15 20:30:18 -08:00
*/
2006-12-02 04:36:16 +00:00
}
2012-05-26 12:21:39 +00:00
/**
2020-01-22 09:07:00 +01:00
* Parse a time / date generated with strftime ()
* With extra output keys for localized month
2012-05-26 12:21:39 +00:00
*
2020-01-22 09:07:00 +01:00
* @ deprecated Use eShims :: strptime () instead
* @ see eShims :: strptime ()
2012-05-26 12:21:39 +00:00
* @ param string $str date string to parse ( e . g . returned from strftime ()) .
2018-05-11 08:39:01 -07:00
* @ param $format
2018-03-04 13:02:50 -08:00
* @ return array | bool Returns an array with the < code > $str </ code > parsed , or < code > false </ code > on error .
2012-05-26 12:21:39 +00:00
*/
public function strptime ( $str , $format )
{
2020-01-22 09:07:00 +01:00
$vals = eShims :: strptime ( $str , $format ); // PHP5 is more accurate than below.
$vals [ 'tm_amon' ] = strftime ( '%b' , mktime ( 0 , 0 , 0 , $vals [ 'tm_mon' ] + 1 ));
$vals [ 'tm_fmon' ] = strftime ( '%B' , mktime ( 0 , 0 , 0 , $vals [ 'tm_mon' ] + 1 ));
return $vals ;
}
2012-05-26 12:21:39 +00:00
function supported ( $mode = FALSE )
{
$strftimeFormats = array (
'A' => 'A full textual representation of the day' ,
'B' => 'Full month name, based on the locale' ,
'C' => 'Two digit representation of the century (year divided by 100, truncated to an integer)' ,
'D' => 'Same as "%m/%d/%y"' ,
'E' => '' ,
'F' => 'Same as "%Y-%m-%d"' ,
'G' => 'The full four-digit version of %g' ,
'H' => 'Two digit representation of the hour in 24-hour format' ,
'I' => 'Two digit representation of the hour in 12-hour format' ,
'J' => '' ,
'K' => '' ,
'L' => '' ,
'M' => 'Two digit representation of the minute' ,
'N' => '' ,
'O' => '' ,
'P' => 'lower-case "am" or "pm" based on the given time' ,
'Q' => '' ,
'R' => 'Same as "%H:%M"' ,
'S' => 'Two digit representation of the second' ,
'T' => 'Same as "%H:%M:%S"' ,
'U' => 'Week number of the given year, starting with the first Sunday as the first week' ,
'V' => 'ISO-8601:1988 week number of the given year, starting with the first week of the year with at least 4 weekdays, with Monday being the start of the week' ,
'W' => 'A numeric representation of the week of the year, starting with the first Monday as the first week' ,
'X' => 'Preferred time representation based on locale, without the date' ,
'Y' => 'Four digit representation for the year' ,
2019-01-17 12:44:33 +01:00
//'Z' => 'The time zone offset/abbreviation option NOT given by %z (depends on operating system)',
2012-05-26 12:21:39 +00:00
'a' => 'An abbreviated textual representation of the day' ,
'b' => 'Abbreviated month name, based on the locale' ,
'c' => 'Preferred date and time stamp based on local' ,
'd' => 'Two-digit day of the month (with leading zeros)' ,
'e' => 'Day of the month, with a space preceding single digits' ,
'f' => '' ,
'g' => 'Two digit representation of the year going by ISO-8601:1988 standards (see %V)' ,
'h' => 'Abbreviated month name, based on the locale (an alias of %b)' ,
'i' => '' ,
'j' => 'Day of the year, 3 digits with leading zeros' ,
'k' => '' ,
'l' => 'Hour in 12-hour format, with a space preceeding single digits' ,
'm' => 'Two digit representation of the month' ,
'n' => 'A newline character ("\n")' ,
'o' => '' ,
'p' => 'UPPER-CASE "AM" or "PM" based on the given time' ,
'q' => '' ,
'r' => 'Same as "%I:%M:%S %p"' ,
's' => 'Unix Epoch Time timestamp' ,
't' => 'A Tab character ("\t")' ,
'u' => 'ISO-8601 numeric representation of the day of the week' ,
'v' => '' ,
'w' => 'Numeric representation of the day of the week' ,
'x' => 'Preferred date representation based on locale, without the time' ,
'y' => 'Two digit representation of the year' ,
2019-01-17 12:44:33 +01:00
//'z' => 'Either the time zone offset from UTC or the abbreviation (depends on operating system)',
2012-05-26 12:21:39 +00:00
'%' => 'A literal percentage character ("%")' ,
);
2019-01-17 12:44:33 +01:00
if ( stripos ( PHP_OS , 'WIN' ) === false )
{
// This formats are not avaiilable on windows and will make the script fail on use.
$strftimeFormats [ 'Z' ] = 'The time zone offset/abbreviation option NOT given by %z (depends on operating system)' ;
$strftimeFormats [ 'z' ] = 'Either the time zone offset from UTC or the abbreviation (depends on operating system)' ;
}
2012-05-26 12:21:39 +00:00
// Results.
$strftimeValues = array ();
// Evaluate the formats whilst suppressing any errors.
foreach ( $strftimeFormats as $format => $description )
{
2019-01-17 12:44:33 +01:00
//if (False !== ($value = @strftime("%{$format}")))
$value = @ strftime ( " % { $format } " );
if ( False !== $value )
2012-05-26 12:21:39 +00:00
{
$strftimeValues [ $format ] = $value ;
}
}
// Find the longest value.
$maxValueLength = 2 + max ( array_map ( 'strlen' , $strftimeValues ));
$ret = array (
'enabled' => array (),
'disabled' => array ()
);
// Report known formats.
foreach ( $strftimeValues as $format => $value )
{
$ret [ 'enabled' ][] = $format ;
echo ( $mode == 'list' ) ? " Known format : ' { $format } ' = " . str_pad ( " ' { $value } ' " , $maxValueLength ) . " ( { $strftimeFormats [ $format ] } )<br /> " : " " ;
}
// Report unknown formats.
foreach ( array_diff_key ( $strftimeFormats , $strftimeValues ) as $format => $description )
{
$ret [ 'disabled' ][] = $format ;
echo ( $mode == 'list' ) ? " Unknown format : ' { $format } ' " . str_pad ( ' ' , $maxValueLength ) . ( $description ? " ( { $description } ) " : '' ) . " <br /> " : " " ;
}
return in_array ( $mode , $ret [ 'enabled' ]);
}
2015-08-24 17:39:28 -07:00
/**
* Check if TimeZone is valid
* @ param $timezone
* @ return bool
*/
function isValidTimezone ( $timezone )
{
return in_array ( $timezone , timezone_identifiers_list ());
}
2007-01-17 20:47:41 +00:00
}
2018-05-11 08:39:01 -07:00
/**
* BC Fix convert
*/
class convert extends e_date
{
}