2008-08-29 22:18:28 +02:00
< ? php
2011-05-25 15:27:07 +02:00
/* Copyright ( C ) 2004 - 2011 Laurent Destailleur < eldy @ users . sourceforge . net >
2012-12-30 15:13:49 +01:00
* Copyright ( C ) 2005 - 2011 Regis Houssin < regis . houssin @ capnetworks . com >
2011-03-09 02:35:25 +01:00
* Copyright ( C ) 2011 Juanjo Menent < jmenent @ 2 byte . es >
2008-08-29 22:18:28 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2008-08-29 22:18:28 +02:00
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2011-08-01 01:24:38 +02:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2008-08-29 22:18:28 +02:00
* or see http :// www . gnu . org /
*/
/**
2011-10-24 11:25:54 +02:00
* \file htdocs / core / lib / date . lib . php
2010-11-21 17:11:52 +01:00
* \brief Set of function to manipulate dates
2008-08-29 22:18:28 +02:00
*/
2011-06-10 22:31:10 +02:00
/**
* Return an array with timezone values
2011-09-16 19:19:24 +02:00
*
2011-06-10 22:31:10 +02:00
* @ return array Array with timezone values
*/
function get_tz_array ()
{
2012-01-22 02:12:11 +01:00
$tzarray = array (
- 11 => " Pacific/Midway " ,
- 10 => " Pacific/Fakaofo " ,
- 9 => " America/Anchorage " ,
- 8 => " America/Los_Angeles " ,
- 7 => " America/Dawson_Creek " ,
- 6 => " America/Chicago " ,
- 5 => " America/Bogota " ,
- 4 => " America/Anguilla " ,
- 3 => " America/Araguaina " ,
- 2 => " America/Noronha " ,
- 1 => " Atlantic/Azores " ,
0 => " Africa/Abidjan " ,
1 => " Europe/Paris " ,
2 => " Europe/Helsinki " ,
3 => " Europe/Moscow " ,
4 => " Asia/Dubai " ,
5 => " Asia/Karachi " ,
6 => " Indian/Chagos " ,
7 => " Asia/Jakarta " ,
8 => " Asia/Hong_Kong " ,
9 => " Asia/Tokyo " ,
10 => " Australia/Sydney " ,
11 => " Pacific/Noumea " ,
12 => " Pacific/Auckland " ,
13 => " Pacific/Enderbury "
);
2011-06-10 22:31:10 +02:00
return $tzarray ;
}
2011-11-01 04:57:45 +01:00
/**
2012-02-05 15:13:31 +01:00
* Return server timezone string
2011-11-01 04:57:45 +01:00
*
2012-02-05 15:13:31 +01:00
* @ return string PHP server timezone string ( 'Europe/Paris' )
2011-11-01 04:57:45 +01:00
*/
2012-02-06 05:31:19 +01:00
function getServerTimeZoneString ()
2011-11-01 04:57:45 +01:00
{
2014-03-21 18:05:08 +01:00
return @ date_default_timezone_get ();
2012-02-05 15:13:31 +01:00
}
/**
* Return server timezone int .
*
2012-05-14 16:46:48 +02:00
* @ param string $refgmtdate Reference period for timezone ( timezone differs on winter and summer . May be 'now' , 'winter' or 'summer' )
2012-02-07 10:25:58 +01:00
* @ return int An offset in hour ( + 1 for Europe / Paris on winter and + 2 for Europe / Paris on summer )
2012-02-05 15:13:31 +01:00
*/
2012-02-06 05:31:19 +01:00
function getServerTimeZoneInt ( $refgmtdate = 'now' )
2012-02-05 15:13:31 +01:00
{
global $conf ;
2014-03-21 15:02:27 +01:00
if ( method_exists ( 'DateTimeZone' , 'getOffset' ))
2012-02-05 15:13:31 +01:00
{
// Method 1 (include daylight)
2012-05-15 00:48:59 +02:00
$gmtnow = dol_now ( 'gmt' ); $yearref = dol_print_date ( $gmtnow , '%Y' ); $monthref = dol_print_date ( $gmtnow , '%m' ); $dayref = dol_print_date ( $gmtnow , '%d' );
if ( $refgmtdate == 'now' ) $newrefgmtdate = $yearref . '-' . $monthref . '-' . $dayref ;
2013-01-18 11:28:06 +01:00
elseif ( $refgmtdate == 'summer' ) $newrefgmtdate = $yearref . '-08-01' ;
2012-05-15 00:48:59 +02:00
else $newrefgmtdate = $yearref . '-01-01' ;
2012-02-06 05:31:19 +01:00
$localtz = new DateTimeZone ( getServerTimeZoneString ());
2012-05-15 00:48:59 +02:00
$localdt = new DateTime ( $newrefgmtdate , $localtz );
2012-02-05 15:13:31 +01:00
$tmp =- 1 * $localtz -> getOffset ( $localdt );
2012-05-15 00:48:59 +02:00
//print $refgmtdate.'='.$tmp;
2012-02-05 15:13:31 +01:00
}
else
{
2014-03-21 18:05:08 +01:00
dol_print_error ( '' , 'PHP version must be 5.3+' );
2014-03-21 15:02:27 +01:00
/*
2012-05-14 16:46:48 +02:00
// Method 2 (does not include daylight, not supported by adodb)
if ( $refgmtdate == 'now' )
{
2012-06-05 12:42:32 +02:00
if ( ini_get ( " date.timezone " ) == 'UTC' ) return 0 ;
2014-03-21 15:02:27 +01:00
// We don't know server timezone string, so we don't know location, so we can't guess daylight. We assume we use same than client but this may be a bug.
2012-05-15 00:48:59 +02:00
$gmtnow = dol_now ( 'gmt' ); $yearref = dol_print_date ( $gmtnow , '%Y' ); $monthref = dol_print_date ( $gmtnow , '%m' ); $dayref = dol_print_date ( $gmtnow , '%d' );
2012-05-14 16:46:48 +02:00
if ( dol_stringtotime ( $_SESSION [ 'dol_dst_first' ]) <= $gmtnow && $gmtnow < dol_stringtotime ( $_SESSION [ 'dol_dst_second' ])) $daylight = 1 ;
else $daylight = 0 ;
2012-05-15 00:48:59 +02:00
$tmp = dol_mktime ( 0 , 0 , 0 , $monthref , $dayref , $yearref , false , 0 ) - dol_mktime ( 0 , 0 , 0 , $monthref , $dayref , $yearref , true , 0 ) - ( $daylight * 3600 );
2012-06-05 12:52:25 +02:00
return 'unknown' ; // For true result
2012-05-15 00:48:59 +02:00
}
elseif ( $refgmtdate == 'summer' )
{
2012-06-05 12:42:32 +02:00
if ( ini_get ( " date.timezone " ) == 'UTC' ) return 0 ;
2014-03-21 15:02:27 +01:00
// We don't know server timezone string, so we don't know location, so we can't guess daylight. We assume we use same than client but this may be a bug.
2012-05-15 00:48:59 +02:00
$gmtnow = dol_now ( 'gmt' ); $yearref = dol_print_date ( $gmtnow , '%Y' ); $monthref = '08' ; $dayref = '01' ;
if ( dol_stringtotime ( $_SESSION [ 'dol_dst_first' ]) <= dol_stringtotime ( $yearref . '-' . $monthref . '-' . $dayref ) && dol_stringtotime ( $yearref . '-' . $monthref . '-' . $dayref ) < dol_stringtotime ( $_SESSION [ 'dol_dst_second' ])) $daylight = 1 ;
else $daylight = 0 ;
$tmp = dol_mktime ( 0 , 0 , 0 , $monthref , $dayref , $yearref , false , 0 ) - dol_mktime ( 0 , 0 , 0 , $monthref , $dayref , $yearref , true , 0 ) - ( $daylight * 3600 );
2012-06-05 12:52:25 +02:00
return 'unknown' ; // For true result
2012-05-14 16:46:48 +02:00
}
else $tmp = dol_mktime ( 0 , 0 , 0 , 1 , 1 , 1970 );
2014-03-21 15:02:27 +01:00
*/
2012-02-05 15:13:31 +01:00
}
2012-05-15 00:48:59 +02:00
$tz = round (( $tmp < 0 ? 1 :- 1 ) * abs ( $tmp / 3600 ));
2011-11-01 04:57:45 +01:00
return $tz ;
}
2010-10-02 01:37:36 +02:00
/**
* Add a delay to a date
2011-09-16 19:19:24 +02:00
*
2013-11-07 21:12:11 +01:00
* @ param int $time Date timestamp ( or string with format YYYY - MM - DD )
2011-09-16 19:19:24 +02:00
* @ param int $duration_value Value of delay to add
2012-01-22 02:12:11 +01:00
* @ param int $duration_unit Unit of added delay ( d , m , y , w )
2013-11-07 21:12:11 +01:00
* @ return int New timestamp
2010-10-02 01:37:36 +02:00
*/
function dol_time_plus_duree ( $time , $duration_value , $duration_unit )
{
2012-01-22 02:12:11 +01:00
if ( $duration_value == 0 ) return $time ;
if ( $duration_unit == 'w' ) return $time + ( 3600 * 24 * 7 * $duration_value );
2011-02-11 01:00:47 +01:00
if ( $duration_value > 0 ) $deltastring = " + " . abs ( $duration_value );
if ( $duration_value < 0 ) $deltastring = " - " . abs ( $duration_value );
if ( $duration_unit == 'd' ) { $deltastring .= " day " ; }
if ( $duration_unit == 'm' ) { $deltastring .= " month " ; }
if ( $duration_unit == 'y' ) { $deltastring .= " year " ; }
return strtotime ( $deltastring , $time );
2010-10-02 01:37:36 +02:00
}
2012-02-04 14:39:47 +01:00
/**
* Convert hours and minutes into seconds
2011-09-16 19:19:24 +02:00
*
2012-02-04 14:39:47 +01:00
* @ param int $iHours Hours
* @ param int $iMinutes Minutes
* @ param int $iSeconds Seconds
* @ return int Time into seconds
2008-08-29 22:18:28 +02:00
*/
2012-02-07 10:25:58 +01:00
function convertTime2Seconds ( $iHours = 0 , $iMinutes = 0 , $iSeconds = 0 )
2008-08-29 22:18:28 +02:00
{
$iResult = ( $iHours * 3600 ) + ( $iMinutes * 60 ) + $iSeconds ;
return $iResult ;
}
2010-10-02 01:37:36 +02:00
/** Return , in clear text , value of a number of seconds in days , hours and minutes
2011-09-16 19:19:24 +02:00
*
* @ param int $iSecond Number of seconds
2013-10-30 21:44:04 +01:00
* @ param string $format Output format ( all : total delay days hour : min like " 2 days 12:30 " " , allhourmin: total delay hours:min like " 60 : 30 " , allhour: total delay hours without min/sec like " 60 : 30 " , fullhour: total delay hour decimal like " 60.5 " for 60:30, hour: only hours part " 12 " , min: only minutes part " 30 " , sec: only seconds part, month: only month part, year: only year part);
2011-09-16 19:19:24 +02:00
* @ param int $lengthOfDay Length of day ( default 86400 seconds for 1 day , 28800 for 8 hour )
* @ param int $lengthOfWeek Length of week ( default 7 )
* @ return sTime Formated text of duration
* Example : 0 return 00 : 00 , 3600 return 1 : 00 , 86400 return 1 d , 90000 return 1 Day 01 : 00
2008-08-29 22:18:28 +02:00
*/
2013-10-30 21:44:04 +01:00
function convertSecondToTime ( $iSecond , $format = 'all' , $lengthOfDay = 86400 , $lengthOfWeek = 7 )
2008-11-05 20:46:09 +01:00
{
2008-11-03 20:24:41 +01:00
global $langs ;
2009-11-17 12:25:11 +01:00
2011-02-19 15:14:05 +01:00
if ( empty ( $lengthOfDay )) $lengthOfDay = 86400 ; // 1 day = 24 hours
if ( empty ( $lengthOfWeek )) $lengthOfWeek = 7 ; // 1 week = 7 days
2008-11-05 20:46:09 +01:00
2013-10-30 21:44:04 +01:00
if ( $format == 'all' || $format == 'allhour' || $format == 'allhourmin' )
2008-11-05 20:46:09 +01:00
{
2010-07-10 01:49:41 +02:00
if ( $iSecond === 0 ) return '0' ; // This is to avoid having 0 return a 12:00 AM for en_US
2011-02-19 15:14:05 +01:00
$sTime = '' ;
2013-10-30 21:44:04 +01:00
$sDay = 0 ;
2011-02-19 15:14:05 +01:00
$sWeek = '' ;
2009-10-29 09:57:05 +01:00
if ( $iSecond >= $lengthOfDay )
2008-11-03 20:24:41 +01:00
{
2009-11-17 12:25:11 +01:00
for ( $i = $iSecond ; $i >= $lengthOfDay ; $i -= $lengthOfDay )
2009-10-29 09:57:05 +01:00
{
$sDay ++ ;
2009-11-16 22:55:03 +01:00
$iSecond -= $lengthOfDay ;
2009-10-29 09:57:05 +01:00
}
2008-11-03 20:24:41 +01:00
$dayTranslate = $langs -> trans ( " Day " );
2009-10-29 09:57:05 +01:00
if ( $iSecond >= ( $lengthOfDay * 2 )) $dayTranslate = $langs -> trans ( " Days " );
2008-11-03 20:24:41 +01:00
}
2011-02-19 15:14:05 +01:00
if ( $lengthOfWeek < 7 )
{
if ( $sDay )
{
if ( $sDay >= $lengthOfWeek )
{
2011-09-20 11:40:27 +02:00
$sWeek = ( int ) (( $sDay - $sDay % $lengthOfWeek ) / $lengthOfWeek );
2011-02-19 15:14:05 +01:00
$sDay = $sDay % $lengthOfWeek ;
$weekTranslate = $langs -> trans ( " DurationWeek " );
if ( $sWeek >= 2 ) $weekTranslate = $langs -> trans ( " DurationWeeks " );
$sTime .= $sWeek . ' ' . $weekTranslate . ' ' ;
}
}
}
2012-06-10 12:42:19 +02:00
if ( $sDay > 0 )
{
$dayTranslate = $langs -> trans ( " Day " );
if ( $sDay > 1 ) $dayTranslate = $langs -> trans ( " Days " );
$sTime .= $sDay . ' ' . $dayTranslate . ' ' ;
}
2011-02-19 15:14:05 +01:00
2013-10-30 21:44:04 +01:00
if ( $format == 'all' )
{
if ( $iSecond || empty ( $sDay ))
{
$sTime .= dol_print_date ( $iSecond , 'hourduration' , true );
}
}
if ( $format == 'allhourmin' )
{
return sprintf ( " %02d " ,( $sWeek * $lengthOfWeek * 24 + $sDay * 24 + ( int ) floor ( $iSecond / 3600 ))) . ':' . sprintf ( " %02d " ,(( int ) floor (( $iSecond % 3600 ) / 60 )));
}
if ( $format == 'allhour' )
2009-10-29 08:53:33 +01:00
{
2013-10-30 21:44:04 +01:00
return sprintf ( " %02d " ,( $sWeek * $lengthOfWeek * 24 + $sDay * 24 + ( int ) floor ( $iSecond / 3600 )));
2009-10-29 08:53:33 +01:00
}
}
2013-10-30 21:44:04 +01:00
else if ( $format == 'hour' ) // only hour part
2009-10-29 08:53:33 +01:00
{
2009-11-17 12:27:55 +01:00
$sTime = dol_print_date ( $iSecond , '%H' , true );
2009-10-29 08:53:33 +01:00
}
2013-05-15 14:19:16 +02:00
else if ( $format == 'fullhour' )
{
if ( ! empty ( $iSecond )) {
$iSecond = $iSecond / 3600 ;
}
else {
$iSecond = 0 ;
}
$sTime = $iSecond ;
}
2013-10-30 21:44:04 +01:00
else if ( $format == 'min' ) // only min part
2009-10-29 08:53:33 +01:00
{
2009-12-03 20:25:35 +01:00
$sTime = dol_print_date ( $iSecond , '%M' , true );
2008-08-29 22:18:28 +02:00
}
2013-10-30 21:44:04 +01:00
else if ( $format == 'sec' ) // only sec part
2011-06-10 22:31:10 +02:00
{
$sTime = dol_print_date ( $iSecond , '%S' , true );
}
2013-10-30 21:44:04 +01:00
else if ( $format == 'month' ) // only month part
2011-08-20 17:11:31 +02:00
{
$sTime = dol_print_date ( $iSecond , '%m' , true );
}
2013-10-30 21:44:04 +01:00
else if ( $format == 'year' ) // only year part
2011-08-20 17:11:31 +02:00
{
$sTime = dol_print_date ( $iSecond , '%Y' , true );
}
return trim ( $sTime );
2008-08-29 22:18:28 +02:00
}
2012-01-21 14:10:22 +01:00
/**
* Convert a string date into a GM Timestamps date
*
* @ param string $string Date in a string
* YYYYMMDD
* YYYYMMDDHHMMSS
* YYYYMMDDTHHMMSSZ
* YYYY - MM - DDTHH : MM : SSZ ( RFC3339 )
* DD / MM / YY or DD / MM / YYYY ( this format should not be used anymore )
* DD / MM / YY HH : MM : SS or DD / MM / YYYY HH : MM : SS ( this format should not be used anymore )
2012-01-22 02:12:11 +01:00
* @ param int $gm 1 = Input date is GM date ,
* 0 = Input date is local date using PHP server timezone
2013-11-07 21:12:11 +01:00
* @ return int Date as a timestamp
2012-01-22 02:12:11 +01:00
* 19700101020000 -> 7200 with gm = 1
2012-01-21 14:10:22 +01:00
*
* @ see dol_print_date , dol_mktime , dol_getdate
*/
2013-06-17 14:12:59 +02:00
function dol_stringtotime ( $string , $gm = 1 )
2012-01-21 14:10:22 +01:00
{
// Convert date with format DD/MM/YYY HH:MM:SS. This part of code should not be used.
if ( preg_match ( '/^([0-9]+)\/([0-9]+)\/([0-9]+)\s?([0-9]+)?:?([0-9]+)?:?([0-9]+)?/i' , $string , $reg ))
{
dol_syslog ( " dol_stringtotime call to function with deprecated parameter " , LOG_WARNING );
// Date est au format 'DD/MM/YY' ou 'DD/MM/YY HH:MM:SS'
// Date est au format 'DD/MM/YYYY' ou 'DD/MM/YYYY HH:MM:SS'
$sday = $reg [ 1 ];
$smonth = $reg [ 2 ];
$syear = $reg [ 3 ];
$shour = $reg [ 4 ];
$smin = $reg [ 5 ];
$ssec = $reg [ 6 ];
if ( $syear < 50 ) $syear += 1900 ;
if ( $syear >= 50 && $syear < 100 ) $syear += 2000 ;
$string = sprintf ( " %04d%02d%02d%02d%02d%02d " , $syear , $smonth , $sday , $shour , $smin , $ssec );
}
2013-06-27 16:02:52 +02:00
else if (
preg_match ( '/^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})Z$/i' , $string , $reg ) // Convert date with format RFC3339
|| preg_match ( '/^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$/i' , $string , $reg ) // Convert date with format YYYY-MM-DD HH:MM:SS
2013-10-30 21:44:04 +01:00
|| preg_match ( '/^([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2})([0-9]{2})([0-9]{2})Z$/i' , $string , $reg ) // Convert date with format YYYYMMDDTHHMMSSZ
2013-06-27 16:02:52 +02:00
)
2012-01-21 14:10:22 +01:00
{
$syear = $reg [ 1 ];
$smonth = $reg [ 2 ];
$sday = $reg [ 3 ];
$shour = $reg [ 4 ];
$smin = $reg [ 5 ];
$ssec = $reg [ 6 ];
$string = sprintf ( " %04d%02d%02d%02d%02d%02d " , $syear , $smonth , $sday , $shour , $smin , $ssec );
}
$string = preg_replace ( '/([^0-9])/i' , '' , $string );
$tmp = $string . '000000' ;
2012-01-22 02:12:11 +01:00
$date = dol_mktime ( substr ( $tmp , 8 , 2 ), substr ( $tmp , 10 , 2 ), substr ( $tmp , 12 , 2 ), substr ( $tmp , 4 , 2 ), substr ( $tmp , 6 , 2 ), substr ( $tmp , 0 , 4 ),( $gm ? 1 : 0 ));
2012-01-21 14:10:22 +01:00
return $date ;
}
2011-02-02 16:51:18 +01:00
/** Return previous day
2011-09-16 19:19:24 +02:00
*
* @ param int $day Day
* @ param int $month Month
* @ param int $year Year
* @ return array Previous year , month , day
2011-02-02 16:51:18 +01:00
*/
function dol_get_prev_day ( $day , $month , $year )
{
2011-02-11 01:00:47 +01:00
$time = dol_mktime ( 12 , 0 , 0 , $month , $day , $year , 1 , 0 );
$time -= 24 * 60 * 60 ;
$tmparray = dol_getdate ( $time , true );
return array ( 'year' => $tmparray [ 'year' ], 'month' => $tmparray [ 'mon' ], 'day' => $tmparray [ 'mday' ]);
2011-02-02 16:51:18 +01:00
}
/** Return next day
2011-09-16 19:19:24 +02:00
*
* @ param int $day Day
* @ param int $month Month
* @ param int $year Year
* @ return array Next year , month , day
2011-02-02 16:51:18 +01:00
*/
function dol_get_next_day ( $day , $month , $year )
{
2011-02-11 01:00:47 +01:00
$time = dol_mktime ( 12 , 0 , 0 , $month , $day , $year , 1 , 0 );
$time += 24 * 60 * 60 ;
$tmparray = dol_getdate ( $time , true );
return array ( 'year' => $tmparray [ 'year' ], 'month' => $tmparray [ 'mon' ], 'day' => $tmparray [ 'mday' ]);
2011-02-02 16:51:18 +01:00
}
2010-10-02 01:37:36 +02:00
/** Return previous month
2011-09-16 19:19:24 +02:00
*
* @ param int $month Month
* @ param int $year Year
* @ return array Previous year , month
2008-08-29 22:18:28 +02:00
*/
function dol_get_prev_month ( $month , $year )
{
if ( $month == 1 )
{
$prev_month = 12 ;
$prev_year = $year - 1 ;
}
else
{
$prev_month = $month - 1 ;
$prev_year = $year ;
}
return array ( 'year' => $prev_year , 'month' => $prev_month );
}
2010-10-02 01:37:36 +02:00
/** Return next month
2011-09-16 19:19:24 +02:00
*
* @ param int $month Month
* @ param int $year Year
* @ return array Next year , month
2008-08-29 22:18:28 +02:00
*/
2010-01-12 00:25:54 +01:00
function dol_get_next_month ( $month , $year )
2008-08-29 22:18:28 +02:00
{
if ( $month == 12 )
{
$next_month = 1 ;
$next_year = $year + 1 ;
}
else
{
$next_month = $month + 1 ;
$next_year = $year ;
}
return array ( 'year' => $next_year , 'month' => $next_month );
}
2011-03-09 02:35:25 +01:00
/** Return previous week
2011-09-16 19:19:24 +02:00
*
* @ param int $day Day
* @ param int $week Week
* @ param int $month Month
* @ param int $year Year
* @ return array Previous year , month , day
2011-03-09 02:35:25 +01:00
*/
2011-03-09 17:53:30 +01:00
function dol_get_prev_week ( $day , $week , $month , $year )
2011-03-09 02:35:25 +01:00
{
2011-03-09 17:53:30 +01:00
$tmparray = dol_get_first_day_week ( $day , $month , $year );
2011-05-25 15:27:07 +02:00
2011-03-09 17:53:30 +01:00
$time = dol_mktime ( 12 , 0 , 0 , $month , $tmparray [ 'first_day' ], $year , 1 , 0 );
$time -= 24 * 60 * 60 * 7 ;
$tmparray = dol_getdate ( $time , true );
return array ( 'year' => $tmparray [ 'year' ], 'month' => $tmparray [ 'mon' ], 'day' => $tmparray [ 'mday' ]);
2011-03-09 02:35:25 +01:00
}
2011-05-25 15:27:07 +02:00
2011-03-09 02:35:25 +01:00
/** Return next week
2011-09-16 19:19:24 +02:00
*
* @ param int $day Day
* @ param int $week Week
* @ param int $month Month
* @ param int $year Year
* @ return array Next year , month , day
2011-03-09 02:35:25 +01:00
*/
2011-05-25 15:27:07 +02:00
function dol_get_next_week ( $day , $week , $month , $year )
2011-03-09 02:35:25 +01:00
{
2011-03-09 17:53:30 +01:00
$tmparray = dol_get_first_day_week ( $day , $month , $year );
2011-05-25 15:27:07 +02:00
2011-03-09 17:53:30 +01:00
$time = dol_mktime ( 12 , 0 , 0 , $month , $tmparray [ 'first_day' ], $year , 1 , 0 );
$time += 24 * 60 * 60 * 7 ;
$tmparray = dol_getdate ( $time , true );
2011-05-25 15:27:07 +02:00
2011-03-09 17:53:30 +01:00
return array ( 'year' => $tmparray [ 'year' ], 'month' => $tmparray [ 'mon' ], 'day' => $tmparray [ 'mday' ]);
2011-05-25 15:27:07 +02:00
2011-03-09 02:35:25 +01:00
}
2008-08-29 22:18:28 +02:00
2010-10-02 01:37:36 +02:00
/** Return GMT time for first day of a month or year
2011-09-16 19:19:24 +02:00
*
* @ param int $year Year
* @ param int $month Month
2014-03-21 16:22:41 +01:00
* @ param mixed $gm False or 0 or 'server' = Return date to compare with server TZ , True or 1 to compare with GM date .
2011-09-16 19:19:24 +02:00
* Exemple : dol_get_first_day ( 1970 , 1 , false ) will return - 3600 with TZ + 1 , after a dol_print_date will return 1970 - 01 - 01 00 : 00 : 00
* Exemple : dol_get_first_day ( 1970 , 1 , true ) will return 0 whatever is TZ , after a dol_print_date will return 1970 - 01 - 01 00 : 00 : 00
2014-04-23 15:48:20 +02:00
* @ return int Date for first day
2009-12-03 20:25:35 +01:00
*/
2010-02-14 20:38:20 +01:00
function dol_get_first_day ( $year , $month = 1 , $gm = false )
2009-12-03 20:25:35 +01:00
{
2010-02-03 03:22:15 +01:00
return dol_mktime ( 0 , 0 , 0 , $month , 1 , $year , $gm );
2009-12-03 20:25:35 +01:00
}
2010-10-02 01:37:36 +02:00
/** Return GMT time for last day of a month or year
2011-09-16 19:19:24 +02:00
*
* @ param int $year Year
* @ param int $month Month
2014-03-21 16:22:41 +01:00
* @ param boolean $gm False or 0 or 'server' = Return date to compare with server TZ , True or 1 to compare with GM date .
2014-04-23 15:48:20 +02:00
* @ return int Date for first day
2009-12-03 20:25:35 +01:00
*/
2010-02-14 20:38:20 +01:00
function dol_get_last_day ( $year , $month = 12 , $gm = false )
2009-12-03 20:25:35 +01:00
{
if ( $month == 12 )
{
$month = 1 ;
$year += 1 ;
}
else
{
$month += 1 ;
}
2010-01-12 00:25:54 +01:00
2009-12-03 20:25:35 +01:00
// On se deplace au debut du mois suivant, et on retire un jour
2010-02-03 03:22:15 +01:00
$datelim = dol_mktime ( 23 , 59 , 59 , $month , 1 , $year , $gm );
2009-12-03 20:25:35 +01:00
$datelim -= ( 3600 * 24 );
2010-01-12 00:25:54 +01:00
return $datelim ;
2009-12-03 20:25:35 +01:00
}
2011-03-09 17:53:30 +01:00
/** Return first day of week for a date
2011-09-16 19:19:24 +02:00
*
* @ param int $day Day
* @ param int $month Month
* @ param int $year Year
2014-03-21 16:22:41 +01:00
* @ param int $gm False or 0 or 'server' = Return date to compare with server TZ , True or 1 to compare with GM date .
2011-09-16 19:19:24 +02:00
* @ return array year , month , week , first_day , prev_year , prev_month , prev_day
2011-03-09 02:35:25 +01:00
*/
2011-05-25 15:27:07 +02:00
function dol_get_first_day_week ( $day , $month , $year , $gm = false )
2011-03-09 02:35:25 +01:00
{
2011-03-09 17:53:30 +01:00
global $conf ;
2011-05-25 15:27:07 +02:00
2011-03-09 23:56:50 +01:00
$date = dol_mktime ( 0 , 0 , 0 , $month , $day , $year , $gm );
2011-05-25 15:27:07 +02:00
2011-03-09 23:56:50 +01:00
//Checking conf of start week
$start_week = ( isset ( $conf -> global -> MAIN_START_WEEK ) ? $conf -> global -> MAIN_START_WEEK : 1 );
2011-05-25 15:27:07 +02:00
2011-03-09 23:56:50 +01:00
$tmparray = dol_getdate ( $date , true );
2011-05-25 15:27:07 +02:00
//Calculate days to count
2011-03-09 23:56:50 +01:00
$days = $start_week - $tmparray [ 'wday' ];
if ( $days >= 1 ) $days = 7 - $days ;
$days = abs ( $days );
$seconds = $days * 24 * 60 * 60 ;
2011-05-25 15:27:07 +02:00
2011-03-09 23:56:50 +01:00
//Get first day of week
$tmpday = date ( $tmparray [ 0 ]) - $seconds ;
2011-03-09 17:53:30 +01:00
$tmpday = date ( " d " , $tmpday );
2011-05-25 15:27:07 +02:00
2011-03-09 23:56:50 +01:00
//Check first day of week is form this month or not
2011-03-09 17:53:30 +01:00
if ( $tmpday > $day )
{
$prev_month = $month - 1 ;
$prev_year = $year ;
2011-05-25 15:27:07 +02:00
2011-03-09 17:53:30 +01:00
if ( $prev_month == 0 )
{
$prev_month = 12 ;
$prev_year = $year - 1 ;
}
}
2011-05-25 15:27:07 +02:00
else
2011-03-09 17:53:30 +01:00
{
$prev_month = $month ;
$prev_year = $year ;
}
2011-03-09 23:56:50 +01:00
2011-05-25 15:27:07 +02:00
//Get first day of next week
2011-03-09 17:53:30 +01:00
$tmptime = dol_mktime ( 12 , 0 , 0 , $month , $tmpday , $year , 1 , 0 );
$tmptime -= 24 * 60 * 60 * 7 ;
$tmparray = dol_getdate ( $tmptime , true );
$prev_day = $tmparray [ 'mday' ];
2011-05-25 15:27:07 +02:00
2011-03-09 23:56:50 +01:00
//Check first day of week is form this month or not
2011-03-09 17:53:30 +01:00
if ( $prev_day > $tmpday )
{
$prev_month = $month - 1 ;
$prev_year = $year ;
2011-05-25 15:27:07 +02:00
2011-03-09 17:53:30 +01:00
if ( $prev_month == 0 )
{
$prev_month = 12 ;
$prev_year = $year - 1 ;
}
}
2011-05-25 15:27:07 +02:00
2011-03-09 17:53:30 +01:00
$week = date ( " W " , dol_mktime ( 0 , 0 , 0 , $month , $tmpday , $year , $gm ));
2011-05-25 15:27:07 +02:00
2011-03-09 17:53:30 +01:00
return array ( 'year' => $year , 'month' => $month , 'week' => $week , 'first_day' => $tmpday , 'prev_year' => $prev_year , 'prev_month' => $prev_month , 'prev_day' => $prev_day );
2011-03-09 02:35:25 +01:00
}
2009-12-03 20:25:35 +01:00
2008-08-29 22:18:28 +02:00
/**
2014-03-05 21:29:33 +01:00
* Fonction retournant le nombre de jour feries , samedis et dimanches entre 2 dates entrees en timestamp . Dates must be UTC with hour , day , min to 0
2011-05-25 15:27:07 +02:00
* Called by function num_open_day
2011-09-16 19:19:24 +02:00
*
2014-04-23 15:48:20 +02:00
* @ param int $timestampStart Timestamp de debut
* @ param int $timestampEnd Timestamp de fin
2011-09-16 19:19:24 +02:00
* @ param string $countrycode Country code
* @ return int Nombre de jours feries
2008-08-29 22:18:28 +02:00
*/
function num_public_holiday ( $timestampStart , $timestampEnd , $countrycode = 'FR' )
{
$nbFerie = 0 ;
2014-03-05 21:29:33 +01:00
// Check to ensure we use correct parameters
if ((( $timestampEnd - $timestampStart ) % 86400 ) != 0 ) return 'ErrorDates must use same hour and be GMT dates' ;
while ( $timestampStart < $timestampEnd ) // Loop end when equals
2008-08-29 22:18:28 +02:00
{
$ferie = false ;
$countryfound = 0 ;
$jour = date ( " d " , $timestampStart );
$mois = date ( " m " , $timestampStart );
$annee = date ( " Y " , $timestampStart );
if ( $countrycode == 'FR' )
{
$countryfound = 1 ;
// Definition des dates feriees fixes
if ( $jour == 1 && $mois == 1 ) $ferie = true ; // 1er janvier
if ( $jour == 1 && $mois == 5 ) $ferie = true ; // 1er mai
if ( $jour == 8 && $mois == 5 ) $ferie = true ; // 5 mai
if ( $jour == 14 && $mois == 7 ) $ferie = true ; // 14 juillet
if ( $jour == 15 && $mois == 8 ) $ferie = true ; // 15 aout
if ( $jour == 1 && $mois == 11 ) $ferie = true ; // 1 novembre
if ( $jour == 11 && $mois == 11 ) $ferie = true ; // 11 novembre
if ( $jour == 25 && $mois == 12 ) $ferie = true ; // 25 decembre
// Calcul du jour de paques
$date_paques = easter_date ( $annee );
$jour_paques = date ( " d " , $date_paques );
$mois_paques = date ( " m " , $date_paques );
if ( $jour_paques == $jour && $mois_paques == $mois ) $ferie = true ;
// Paques
// Calcul du jour de l ascension (38 jours apres Paques)
2012-04-02 18:37:37 +02:00
$date_ascension = mktime (
2011-09-16 19:19:24 +02:00
date ( " H " , $date_paques ),
date ( " i " , $date_paques ),
date ( " s " , $date_paques ),
date ( " m " , $date_paques ),
date ( " d " , $date_paques ) + 38 ,
date ( " Y " , $date_paques )
2012-05-25 11:53:53 +02:00
);
2008-08-29 22:18:28 +02:00
$jour_ascension = date ( " d " , $date_ascension );
$mois_ascension = date ( " m " , $date_ascension );
if ( $jour_ascension == $jour && $mois_ascension == $mois ) $ferie = true ;
//Ascension
// Calcul de Pentecote (11 jours apres Paques)
2012-05-25 11:53:53 +02:00
$date_pentecote = mktime (
2011-09-16 19:19:24 +02:00
date ( " H " , $date_ascension ),
date ( " i " , $date_ascension ),
date ( " s " , $date_ascension ),
date ( " m " , $date_ascension ),
date ( " d " , $date_ascension ) + 11 ,
date ( " Y " , $date_ascension )
2012-05-25 11:53:53 +02:00
);
2008-08-29 22:18:28 +02:00
$jour_pentecote = date ( " d " , $date_pentecote );
$mois_pentecote = date ( " m " , $date_pentecote );
if ( $jour_pentecote == $jour && $mois_pentecote == $mois ) $ferie = true ;
//Pentecote
// Calul des samedis et dimanches
$jour_julien = unixtojd ( $timestampStart );
$jour_semaine = jddayofweek ( $jour_julien , 0 );
if ( $jour_semaine == 0 || $jour_semaine == 6 ) $ferie = true ;
//Samedi (6) et dimanche (0)
}
2011-02-11 01:00:47 +01:00
// Pentecoste and Ascensione in Italy go to the sunday after: isn't holiday.
// Pentecoste is 50 days after Easter, Ascensione 40
if ( $countrycode == 'IT' )
{
$countryfound = 1 ;
// Definition des dates feriees fixes
if ( $jour == 1 && $mois == 1 ) $ferie = true ; // Capodanno
if ( $jour == 6 && $mois == 1 ) $ferie = true ; // Epifania
if ( $jour == 25 && $mois == 4 ) $ferie = true ; // Anniversario Liberazione
if ( $jour == 1 && $mois == 5 ) $ferie = true ; // Festa del Lavoro
if ( $jour == 2 && $mois == 6 ) $ferie = true ; // Festa della Repubblica
if ( $jour == 15 && $mois == 8 ) $ferie = true ; // Ferragosto
if ( $jour == 1 && $mois == 11 ) $ferie = true ; // Tutti i Santi
if ( $jour == 8 && $mois == 12 ) $ferie = true ; // Immacolata Concezione
if ( $jour == 25 && $mois == 12 ) $ferie = true ; // 25 decembre
if ( $jour == 26 && $mois == 12 ) $ferie = true ; // Santo Stefano
// Calcul du jour de paques
$date_paques = easter_date ( $annee );
$jour_paques = date ( " d " , $date_paques );
$mois_paques = date ( " m " , $date_paques );
if ( $jour_paques == $jour && $mois_paques == $mois ) $ferie = true ;
// Paques
2008-08-29 22:18:28 +02:00
2011-02-11 01:00:47 +01:00
// Calul des samedis et dimanches
$jour_julien = unixtojd ( $timestampStart );
2014-07-10 12:06:09 +02:00
$jour_semaine = jddayofweek ( $jour_julien , 0 );
if ( $jour_semaine == 0 || $jour_semaine == 6 ) $ferie = true ;
//Samedi (6) et dimanche (0)
}
if ( $countrycode == 'ES' )
{
$countryfound = 1 ;
// Definition des dates feriees fixes
if ( $jour == 1 && $mois == 1 ) $ferie = true ; // Año nuevo
if ( $jour == 6 && $mois == 1 ) $ferie = true ; // Día Reyes
if ( $jour == 1 && $mois == 5 ) $ferie = true ; // 1 Mayo
if ( $jour == 15 && $mois == 8 ) $ferie = true ; // 15 Agosto
if ( $jour == 12 && $mois == 10 ) $ferie = true ; // Día Hispanidad
if ( $jour == 1 && $mois == 11 ) $ferie = true ; // 1 noviembre
if ( $jour == 6 && $mois == 12 ) $ferie = true ; // Constitución
if ( $jour == 8 && $mois == 12 ) $ferie = true ; // Inmaculada
if ( $jour == 25 && $mois == 12 ) $ferie = true ; // 25 diciembre
// Calcul día de Pascua
$date_paques = easter_date ( $annee );
$jour_paques = date ( " d " , $date_paques );
$mois_paques = date ( " m " , $date_paques );
if ( $jour_paques == $jour && $mois_paques == $mois ) $ferie = true ;
// Paques
// Viernes Santo
$date_viernes = mktime (
date ( " H " , $date_paques ),
date ( " i " , $date_paques ),
date ( " s " , $date_paques ),
date ( " m " , $date_paques ),
date ( " d " , $date_paques ) - 2 ,
date ( " Y " , $date_paques )
);
$jour_viernes = date ( " d " , $date_viernes );
$mois_viernes = date ( " m " , $date_viernes );
if ( $jour_viernes == $jour && $mois_viernes == $mois ) $ferie = true ;
//Viernes Santo
// Calul des samedis et dimanches
$jour_julien = unixtojd ( $timestampStart );
2011-02-11 01:00:47 +01:00
$jour_semaine = jddayofweek ( $jour_julien , 0 );
if ( $jour_semaine == 0 || $jour_semaine == 6 ) $ferie = true ;
//Samedi (6) et dimanche (0)
}
2008-08-29 22:18:28 +02:00
// Cas pays non defini
if ( ! $countryfound )
{
// Calul des samedis et dimanches
$jour_julien = unixtojd ( $timestampStart );
$jour_semaine = jddayofweek ( $jour_julien , 0 );
if ( $jour_semaine == 0 || $jour_semaine == 6 ) $ferie = true ;
//Samedi (6) et dimanche (0)
}
// On incremente compteur
if ( $ferie ) $nbFerie ++ ;
2014-03-05 21:29:33 +01:00
// Increase number of days (on go up into loop)
2008-08-29 22:18:28 +02:00
$jour ++ ;
2014-03-05 21:29:33 +01:00
$timestampStart = dol_mktime ( 0 , 0 , 0 , $mois , $jour , $annee , 1 ); // Generate GMT date for next day
2008-08-29 22:18:28 +02:00
}
return $nbFerie ;
}
/**
2014-03-05 20:52:27 +01:00
* Function to return number of days between two dates ( date must be UTC date ! )
2012-09-24 21:37:19 +02:00
* Example : 2012 - 01 - 01 2012 - 01 - 02 => 1 if lastday = 0 , 2 if lastday = 1
2011-09-16 19:19:24 +02:00
*
2014-04-23 15:48:20 +02:00
* @ param int $timestampStart Timestamp start UTC
* @ param int $timestampEnd Timestamp end UTC
2012-09-24 21:37:19 +02:00
* @ param int $lastday Last day is included , 0 : non , 1 : oui
* @ return int Number of days
2008-08-29 22:18:28 +02:00
*/
function num_between_day ( $timestampStart , $timestampEnd , $lastday = 0 )
{
if ( $timestampStart < $timestampEnd )
{
if ( $lastday == 1 )
{
$bit = 0 ;
}
else
{
$bit = 1 ;
}
2012-09-24 21:37:19 +02:00
$nbjours = ( int ) floor (( $timestampEnd - $timestampStart ) / ( 60 * 60 * 24 )) + 1 - $bit ;
2008-08-29 22:18:28 +02:00
}
2012-09-24 21:37:19 +02:00
//print ($timestampEnd - $timestampStart) - $lastday;
2008-08-29 22:18:28 +02:00
return $nbjours ;
}
/**
2012-12-18 13:12:11 +01:00
* Function to return number of working days ( and text of units ) between two dates ( working days )
2011-09-16 19:19:24 +02:00
*
2014-04-23 15:48:20 +02:00
* @ param int $timestampStart Timestamp for start date ( date must be UTC to avoid calculation errors )
* @ param int $timestampEnd Timestamp for end date ( date must be UTC to avoid calculation errors )
2014-03-05 20:52:27 +01:00
* @ param int $inhour 0 : return number of days , 1 : return number of hours
2012-12-18 13:12:11 +01:00
* @ param int $lastday We include last day , 0 : no , 1 : yes
* @ param int $halfday Tag to define half day when holiday start and end
* @ return int Number of days or hours
2008-08-29 22:18:28 +02:00
*/
2012-12-18 13:12:11 +01:00
function num_open_day ( $timestampStart , $timestampEnd , $inhour = 0 , $lastday = 0 , $halfday = 0 )
2008-08-29 22:18:28 +02:00
{
global $langs ;
2012-09-24 21:37:19 +02:00
dol_syslog ( 'num_open_day timestampStart=' . $timestampStart . ' timestampEnd=' . $timestampEnd . ' bit=' . $lastday );
2013-01-18 11:28:06 +01:00
2012-11-20 13:14:16 +01:00
// Check parameters
if ( ! is_int ( $timestampStart ) && ! is_float ( $timestampStart )) return 'ErrorBadParameter_num_open_day' ;
if ( ! is_int ( $timestampEnd ) && ! is_float ( $timestampEnd )) return 'ErrorBadParameter_num_open_day' ;
2012-09-24 21:37:19 +02:00
//print 'num_open_day timestampStart='.$timestampStart.' timestampEnd='.$timestampEnd.' bit='.$lastday;
2008-08-29 22:18:28 +02:00
if ( $timestampStart < $timestampEnd )
{
2012-09-24 21:37:19 +02:00
$nbOpenDay = num_between_day ( $timestampStart , $timestampEnd , $lastday ) - num_public_holiday ( $timestampStart , $timestampEnd , $lastday );
2012-11-20 13:14:16 +01:00
$nbOpenDay .= " " . $langs -> trans ( " Days " );
2008-08-29 22:18:28 +02:00
if ( $inhour == 1 && $nbOpenDay <= 3 ) $nbOpenDay = $nbOpenDay * 24 . $langs -> trans ( " HourShort " );
2012-12-18 13:12:11 +01:00
return $nbOpenDay - (( $inhour == 1 ? 12 : 0.5 ) * abs ( $halfday ));
2008-08-29 22:18:28 +02:00
}
2012-09-24 21:37:19 +02:00
elseif ( $timestampStart == $timestampEnd )
{
$nbOpenDay = $lastday ;
if ( $inhour == 1 ) $nbOpenDay = $nbOpenDay * 24 . $langs -> trans ( " HourShort " );
2012-12-18 13:12:11 +01:00
return $nbOpenDay - (( $inhour == 1 ? 12 : 0.5 ) * abs ( $halfday ));
2012-09-24 21:37:19 +02:00
}
2008-08-29 22:18:28 +02:00
else
{
return $langs -> trans ( " Error " );
}
}
2011-08-17 18:07:41 +02:00
/**
2012-03-14 23:11:49 +01:00
* Return array of translated months or selected month .
* This replace old function monthArrayOrSelected .
2011-08-17 18:07:41 +02:00
*
2012-02-04 14:39:47 +01:00
* @ param Translate $outputlangs Object langs
2012-01-24 09:19:28 +01:00
* @ return array Month string or array if selected < 0
2011-08-17 18:07:41 +02:00
*/
2012-01-24 09:19:28 +01:00
function monthArray ( $outputlangs )
2011-08-17 18:07:41 +02:00
{
2012-01-24 09:19:28 +01:00
$montharray = array (
1 => $outputlangs -> trans ( " January " ),
2 => $outputlangs -> trans ( " February " ),
3 => $outputlangs -> trans ( " March " ),
4 => $outputlangs -> trans ( " April " ),
5 => $outputlangs -> trans ( " May " ),
6 => $outputlangs -> trans ( " June " ),
7 => $outputlangs -> trans ( " July " ),
8 => $outputlangs -> trans ( " August " ),
9 => $outputlangs -> trans ( " September " ),
10 => $outputlangs -> trans ( " October " ),
11 => $outputlangs -> trans ( " November " ),
12 => $outputlangs -> trans ( " December " )
2011-08-17 18:07:41 +02:00
);
2012-01-24 09:19:28 +01:00
return $montharray ;
2011-08-17 18:07:41 +02:00
}