2
0

Date.php 178 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Date
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * Include needed Date classes
  23. */
  24. require_once 'Zend/Date/DateObject.php';
  25. require_once 'Zend/Locale.php';
  26. require_once 'Zend/Locale/Format.php';
  27. require_once 'Zend/Locale/Math.php';
  28. /**
  29. * @category Zend
  30. * @package Zend_Date
  31. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Date extends Zend_Date_DateObject
  35. {
  36. private $_locale = null;
  37. // Fractional second variables
  38. private $_fractional = 0;
  39. private $_precision = 3;
  40. private static $_options = array(
  41. 'format_type' => 'iso', // format for date strings 'iso' or 'php'
  42. 'fix_dst' => true, // fix dst on summer/winter time change
  43. 'extend_month' => false, // false - addMonth like SQL, true like excel
  44. 'cache' => null, // cache to set
  45. 'timesync' => null // timesync server to set
  46. );
  47. // Class wide Date Constants
  48. const DAY = 'dd';
  49. const DAY_SHORT = 'd';
  50. const DAY_SUFFIX = 'SS';
  51. const DAY_OF_YEAR = 'D';
  52. const WEEKDAY = 'EEEE';
  53. const WEEKDAY_SHORT = 'EEE';
  54. const WEEKDAY_NARROW = 'E';
  55. const WEEKDAY_NAME = 'EE';
  56. const WEEKDAY_8601 = 'eee';
  57. const WEEKDAY_DIGIT = 'e';
  58. const WEEK = 'ww';
  59. const MONTH = 'MM';
  60. const MONTH_SHORT = 'M';
  61. const MONTH_DAYS = 'ddd';
  62. const MONTH_NAME = 'MMMM';
  63. const MONTH_NAME_SHORT = 'MMM';
  64. const MONTH_NAME_NARROW = 'MMMMM';
  65. const YEAR = 'y';
  66. const YEAR_SHORT = 'yy';
  67. const YEAR_8601 = 'Y';
  68. const YEAR_SHORT_8601 = 'YY';
  69. const LEAPYEAR = 'l';
  70. const MERIDIEM = 'a';
  71. const SWATCH = 'B';
  72. const HOUR = 'HH';
  73. const HOUR_SHORT = 'H';
  74. const HOUR_AM = 'hh';
  75. const HOUR_SHORT_AM = 'h';
  76. const MINUTE = 'mm';
  77. const MINUTE_SHORT = 'm';
  78. const SECOND = 'ss';
  79. const SECOND_SHORT = 's';
  80. const MILLISECOND = 'S';
  81. const TIMEZONE_NAME = 'zzzz';
  82. const DAYLIGHT = 'I';
  83. const GMT_DIFF = 'Z';
  84. const GMT_DIFF_SEP = 'ZZZZ';
  85. const TIMEZONE = 'z';
  86. const TIMEZONE_SECS = 'X';
  87. const ISO_8601 = 'c';
  88. const RFC_2822 = 'r';
  89. const TIMESTAMP = 'U';
  90. const ERA = 'G';
  91. const ERA_NAME = 'GGGG';
  92. const ERA_NARROW = 'GGGGG';
  93. const DATES = 'F';
  94. const DATE_FULL = 'FFFFF';
  95. const DATE_LONG = 'FFFF';
  96. const DATE_MEDIUM = 'FFF';
  97. const DATE_SHORT = 'FF';
  98. const TIMES = 'WW';
  99. const TIME_FULL = 'TTTTT';
  100. const TIME_LONG = 'TTTT';
  101. const TIME_MEDIUM = 'TTT';
  102. const TIME_SHORT = 'TT';
  103. const ATOM = 'OOO';
  104. const COOKIE = 'CCC';
  105. const RFC_822 = 'R';
  106. const RFC_850 = 'RR';
  107. const RFC_1036 = 'RRR';
  108. const RFC_1123 = 'RRRR';
  109. const RFC_3339 = 'RRRRR';
  110. const RSS = 'SSS';
  111. const W3C = 'WWW';
  112. /**
  113. * Generates the standard date object, could be a unix timestamp, localized date,
  114. * string, integer, array and so on. Also parts of dates or time are supported
  115. * Always set the default timezone: http://php.net/date_default_timezone_set
  116. * For example, in your bootstrap: date_default_timezone_set('America/Los_Angeles');
  117. * For detailed instructions please look in the docu.
  118. *
  119. * @param string|integer|Zend_Date|array $date OPTIONAL Date value or value of date part to set
  120. * ,depending on $part. If null the actual time is set
  121. * @param string $part OPTIONAL Defines the input format of $date
  122. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  123. * @return Zend_Date
  124. * @throws Zend_Date_Exception
  125. */
  126. public function __construct($date = null, $part = null, $locale = null)
  127. {
  128. if (($date !== null) and !($date instanceof Zend_TimeSync_Protocol) and (Zend_Locale::isLocale($date, true, false))) {
  129. $locale = $date;
  130. $date = null;
  131. $part = null;
  132. } else if (($part !== null) and (Zend_Locale::isLocale($part, null, false))) {
  133. $locale = $part;
  134. $part = null;
  135. }
  136. $this->setLocale($locale);
  137. if (is_string($date) && ($part === null) && (strlen($date) <= 5)) {
  138. $part = $date;
  139. $date = null;
  140. }
  141. if ($date === null) {
  142. if ($part === null) {
  143. $date = time();
  144. } else if ($part !== self::TIMESTAMP) {
  145. $date = self::now($locale);
  146. $date = $date->get($part);
  147. }
  148. }
  149. if ($date instanceof Zend_TimeSync_Protocol) {
  150. $date = $date->getInfo();
  151. $date = $this->_getTime($date['offset']);
  152. $part = null;
  153. } else if (parent::$_defaultOffset != 0) {
  154. $date = $this->_getTime(parent::$_defaultOffset);
  155. }
  156. // set the timezone and offset for $this
  157. $zone = @date_default_timezone_get();
  158. $this->setTimezone($zone);
  159. // try to get timezone from date-string
  160. if (!is_int($date)) {
  161. $zone = $this->getTimezoneFromString($date);
  162. $this->setTimezone($zone);
  163. }
  164. // set datepart
  165. if (($part !== null && $part !== self::TIMESTAMP) or (!is_numeric($date))) {
  166. // switch off dst handling for value setting
  167. $this->setUnixTimestamp($this->getGmtOffset());
  168. $this->set($date, $part, $this->_locale);
  169. // DST fix
  170. if ((is_array($date) === true) and (isset($date['hour']) === true)) {
  171. $hour = $this->toString('H');
  172. $hour = $date['hour'] - $hour;
  173. switch ($hour) {
  174. case 1 :
  175. case -23 :
  176. $this->addTimestamp(3600);
  177. break;
  178. case -1 :
  179. case 23 :
  180. $this->subTimestamp(3600);
  181. break;
  182. case 2 :
  183. case -22 :
  184. $this->addTimestamp(7200);
  185. break;
  186. case -2 :
  187. case 22 :
  188. $this->subTimestamp(7200);
  189. break;
  190. }
  191. }
  192. } else {
  193. $this->setUnixTimestamp($date);
  194. }
  195. }
  196. /**
  197. * Sets class wide options, if no option was given, the actual set options will be returned
  198. *
  199. * @param array $options Options to set
  200. * @throws Zend_Date_Exception
  201. * @return Options array if no option was given
  202. */
  203. public static function setOptions(array $options = array())
  204. {
  205. if (empty($options)) {
  206. return self::$_options;
  207. }
  208. foreach ($options as $name => $value) {
  209. $name = strtolower($name);
  210. if (array_key_exists($name, self::$_options)) {
  211. switch($name) {
  212. case 'format_type' :
  213. if ((strtolower($value) != 'php') && (strtolower($value) != 'iso')) {
  214. require_once 'Zend/Date/Exception.php';
  215. throw new Zend_Date_Exception("Unknown format type ($value) for dates, only 'iso' and 'php' supported", $value);
  216. }
  217. break;
  218. case 'fix_dst' :
  219. if (!is_bool($value)) {
  220. require_once 'Zend/Date/Exception.php';
  221. throw new Zend_Date_Exception("'fix_dst' has to be boolean", $value);
  222. }
  223. break;
  224. case 'extend_month' :
  225. if (!is_bool($value)) {
  226. require_once 'Zend/Date/Exception.php';
  227. throw new Zend_Date_Exception("'extend_month' has to be boolean", $value);
  228. }
  229. break;
  230. case 'cache' :
  231. if (!$value instanceof Zend_Cache_Core) {
  232. require_once 'Zend/Date/Exception.php';
  233. throw new Zend_Date_Exception("Instance of Zend_Cache expected");
  234. }
  235. parent::$_cache = $value;
  236. Zend_Locale_Data::setCache($value);
  237. break;
  238. case 'timesync' :
  239. if (!$value instanceof Zend_TimeSync_Protocol) {
  240. require_once 'Zend/Date/Exception.php';
  241. throw new Zend_Date_Exception("Instance of Zend_TimeSync expected");
  242. }
  243. $date = $value->getInfo();
  244. parent::$_defaultOffset = $date['offset'];
  245. break;
  246. }
  247. self::$_options[$name] = $value;
  248. }
  249. else {
  250. require_once 'Zend/Date/Exception.php';
  251. throw new Zend_Date_Exception("Unknown option: $name = $value");
  252. }
  253. }
  254. }
  255. /**
  256. * Returns this object's internal UNIX timestamp (equivalent to Zend_Date::TIMESTAMP).
  257. * If the timestamp is too large for integers, then the return value will be a string.
  258. * This function does not return the timestamp as an object.
  259. * Use clone() or copyPart() instead.
  260. *
  261. * @return integer|string UNIX timestamp
  262. */
  263. public function getTimestamp()
  264. {
  265. return $this->getUnixTimestamp();
  266. }
  267. /**
  268. * Returns the calculated timestamp
  269. * HINT: timestamps are always GMT
  270. *
  271. * @param string $calc Type of calculation to make
  272. * @param string|integer|array|Zend_Date $stamp Timestamp to calculate, when null the actual timestamp is calculated
  273. * @return Zend_Date|integer
  274. * @throws Zend_Date_Exception
  275. */
  276. private function _timestamp($calc, $stamp)
  277. {
  278. if ($stamp instanceof Zend_Date) {
  279. // extract timestamp from object
  280. $stamp = $stamp->get(self::TIMESTAMP, true);
  281. }
  282. if (is_array($stamp)) {
  283. if (isset($stamp['timestamp']) === true) {
  284. $stamp = $stamp['timestamp'];
  285. } else {
  286. require_once 'Zend/Date/Exception.php';
  287. throw new Zend_Date_Exception('no timestamp given in array');
  288. }
  289. }
  290. if ($calc === 'set') {
  291. $return = $this->setUnixTimestamp($stamp);
  292. } else {
  293. $return = $this->_calcdetail($calc, $stamp, self::TIMESTAMP, null);
  294. }
  295. if ($calc != 'cmp') {
  296. return $this;
  297. }
  298. return $return;
  299. }
  300. /**
  301. * Sets a new timestamp
  302. *
  303. * @param integer|string|array|Zend_Date $timestamp Timestamp to set
  304. * @return Zend_Date
  305. * @throws Zend_Date_Exception
  306. */
  307. public function setTimestamp($timestamp)
  308. {
  309. return $this->_timestamp('set', $timestamp);
  310. }
  311. /**
  312. * Adds a timestamp
  313. *
  314. * @param integer|string|array|Zend_Date $timestamp Timestamp to add
  315. * @return Zend_Date
  316. * @throws Zend_Date_Exception
  317. */
  318. public function addTimestamp($timestamp)
  319. {
  320. return $this->_timestamp('add', $timestamp);
  321. }
  322. /**
  323. * Subtracts a timestamp
  324. *
  325. * @param integer|string|array|Zend_Date $timestamp Timestamp to sub
  326. * @return Zend_Date
  327. * @throws Zend_Date_Exception
  328. */
  329. public function subTimestamp($timestamp)
  330. {
  331. return $this->_timestamp('sub', $timestamp);
  332. }
  333. /**
  334. * Compares two timestamps, returning the difference as integer
  335. *
  336. * @param integer|string|array|Zend_Date $timestamp Timestamp to compare
  337. * @return integer 0 = equal, 1 = later, -1 = earlier
  338. * @throws Zend_Date_Exception
  339. */
  340. public function compareTimestamp($timestamp)
  341. {
  342. return $this->_timestamp('cmp', $timestamp);
  343. }
  344. /**
  345. * Returns a string representation of the object
  346. * Supported format tokens are:
  347. * G - era, y - year, Y - ISO year, M - month, w - week of year, D - day of year, d - day of month
  348. * E - day of week, e - number of weekday (1-7), h - hour 1-12, H - hour 0-23, m - minute, s - second
  349. * A - milliseconds of day, z - timezone, Z - timezone offset, S - fractional second, a - period of day
  350. *
  351. * Additionally format tokens but non ISO conform are:
  352. * SS - day suffix, eee - php number of weekday(0-6), ddd - number of days per month
  353. * l - Leap year, B - swatch internet time, I - daylight saving time, X - timezone offset in seconds
  354. * r - RFC2822 format, U - unix timestamp
  355. *
  356. * Not supported ISO tokens are
  357. * u - extended year, Q - quarter, q - quarter, L - stand alone month, W - week of month
  358. * F - day of week of month, g - modified julian, c - stand alone weekday, k - hour 0-11, K - hour 1-24
  359. * v - wall zone
  360. *
  361. * @param string $format OPTIONAL Rule for formatting output. If null the default date format is used
  362. * @param string $type OPTIONAL Type for the format string which overrides the standard setting
  363. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  364. * @return string
  365. */
  366. public function toString($format = null, $type = null, $locale = null)
  367. {
  368. if ((strlen($format) != 2) and ($format !== null) and (Zend_Locale::isLocale($format, null, false))) {
  369. $locale = $format;
  370. $format = null;
  371. }
  372. if (($type !== null) and (Zend_Locale::isLocale($type, null, false))) {
  373. $locale = $type;
  374. $type = null;
  375. }
  376. if ($locale === null) {
  377. $locale = $this->getLocale();
  378. }
  379. if ($format === null) {
  380. $format = Zend_Locale_Format::getDateFormat($locale) . ' ' . Zend_Locale_Format::getTimeFormat($locale);
  381. } else if (((self::$_options['format_type'] == 'php') && ($type === null)) or ($type == 'php')) {
  382. $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
  383. }
  384. return $this->get($format, $locale);
  385. }
  386. /**
  387. * Returns a string representation of the date which is equal with the timestamp
  388. *
  389. * @return string
  390. */
  391. public function __toString()
  392. {
  393. return $this->toString(null, $this->_locale);
  394. }
  395. /**
  396. * Returns a integer representation of the object
  397. * But returns false when the given part is no value f.e. Month-Name
  398. *
  399. * @param string|integer|Zend_Date $part OPTIONAL Defines the date or datepart to return as integer
  400. * @return integer|false
  401. */
  402. public function toValue($part = null)
  403. {
  404. $result = $this->get($part);
  405. if (is_numeric($result)) {
  406. return intval("$result");
  407. } else {
  408. return false;
  409. }
  410. }
  411. /**
  412. * Returns an array representation of the object
  413. *
  414. * @return array
  415. */
  416. public function toArray()
  417. {
  418. return array('day' => $this->get(self::DAY_SHORT),
  419. 'month' => $this->get(self::MONTH_SHORT),
  420. 'year' => $this->get(self::YEAR),
  421. 'hour' => $this->get(self::HOUR_SHORT),
  422. 'minute' => $this->get(self::MINUTE_SHORT),
  423. 'second' => $this->get(self::SECOND_SHORT),
  424. 'timezone' => $this->get(self::TIMEZONE),
  425. 'timestamp' => $this->get(self::TIMESTAMP),
  426. 'weekday' => $this->get(self::WEEKDAY_8601),
  427. 'dayofyear' => $this->get(self::DAY_OF_YEAR),
  428. 'week' => $this->get(self::WEEK),
  429. 'gmtsecs' => $this->get(self::TIMEZONE_SECS));
  430. }
  431. /**
  432. * Returns a representation of a date or datepart
  433. * This could be for example a localized monthname, the time without date,
  434. * the era or only the fractional seconds. There are about 50 different supported date parts.
  435. * For a complete list of supported datepart values look into the docu
  436. *
  437. * @param string $part OPTIONAL Part of the date to return, if null the timestamp is returned
  438. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  439. * @return string date or datepart
  440. */
  441. public function get($part = null, $locale = null)
  442. {
  443. if ($locale === null) {
  444. $locale = $this->getLocale();
  445. }
  446. if (($part !== null) && (strlen($part) !== 2) && (Zend_Locale::isLocale($part, null, false))) {
  447. $locale = $part;
  448. $part = null;
  449. }
  450. if ($part === null) {
  451. $part = self::TIMESTAMP;
  452. }
  453. return $this->date($this->_toToken($part, $locale), $this->getUnixTimestamp(), false);
  454. }
  455. /**
  456. * Internal method to apply tokens
  457. *
  458. * @param string $part
  459. * @param string $locale
  460. * @return string
  461. */
  462. private function _toToken($part, $locale) {
  463. // get format tokens
  464. $comment = false;
  465. $format = '';
  466. $orig = '';
  467. for ($i = 0; $i < strlen($part); ++$i) {
  468. if ($part[$i] == "'") {
  469. $comment = $comment ? false : true;
  470. if (isset($part[$i+1]) && ($part[$i+1] == "'")) {
  471. $comment = $comment ? false : true;
  472. $format .= "\\'";
  473. ++$i;
  474. }
  475. $orig = '';
  476. continue;
  477. }
  478. if ($comment) {
  479. $format .= '\\' . $part[$i];
  480. $orig = '';
  481. } else {
  482. $orig .= $part[$i];
  483. if (!isset($part[$i+1]) || (isset($orig[0]) && ($orig[0] != $part[$i+1]))) {
  484. $format .= $this->_parseIsoToDate($orig, $locale);
  485. $orig = '';
  486. }
  487. }
  488. }
  489. return $format;
  490. }
  491. /**
  492. * Internal parsing method
  493. *
  494. * @param string $token
  495. * @param string $locale
  496. * @return string
  497. */
  498. private function _parseIsoToDate($token, $locale) {
  499. switch($token) {
  500. case self::DAY :
  501. return 'd';
  502. break;
  503. case self::WEEKDAY_SHORT :
  504. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  505. $day = Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday));
  506. return $this->_toComment(iconv_substr($day, 0, 3, 'UTF-8'));
  507. break;
  508. case self::DAY_SHORT :
  509. return 'j';
  510. break;
  511. case self::WEEKDAY :
  512. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  513. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday)));
  514. break;
  515. case self::WEEKDAY_8601 :
  516. return 'N';
  517. break;
  518. case 'ee' :
  519. return $this->_toComment(str_pad($this->date('N', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
  520. break;
  521. case self::DAY_SUFFIX :
  522. return 'S';
  523. break;
  524. case self::WEEKDAY_DIGIT :
  525. return 'w';
  526. break;
  527. case self::DAY_OF_YEAR :
  528. return 'z';
  529. break;
  530. case 'DDD' :
  531. return $this->_toComment(str_pad($this->date('z', $this->getUnixTimestamp(), false), 3, '0', STR_PAD_LEFT));
  532. break;
  533. case 'DD' :
  534. return $this->_toComment(str_pad($this->date('z', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
  535. break;
  536. case self::WEEKDAY_NARROW :
  537. case 'EEEEE' :
  538. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  539. $day = Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday));
  540. return $this->_toComment(iconv_substr($day, 0, 1, 'UTF-8'));
  541. break;
  542. case self::WEEKDAY_NAME :
  543. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  544. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday)));
  545. break;
  546. case 'w' :
  547. $week = $this->date('W', $this->getUnixTimestamp(), false);
  548. return $this->_toComment(($week[0] == '0') ? $week[1] : $week);
  549. break;
  550. case self::WEEK :
  551. return 'W';
  552. break;
  553. case self::MONTH_NAME :
  554. $month = $this->date('n', $this->getUnixTimestamp(), false);
  555. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'wide', $month)));
  556. break;
  557. case self::MONTH :
  558. return 'm';
  559. break;
  560. case self::MONTH_NAME_SHORT :
  561. $month = $this->date('n', $this->getUnixTimestamp(), false);
  562. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month)));
  563. break;
  564. case self::MONTH_SHORT :
  565. return 'n';
  566. break;
  567. case self::MONTH_DAYS :
  568. return 't';
  569. break;
  570. case self::MONTH_NAME_NARROW :
  571. $month = $this->date('n', $this->getUnixTimestamp(), false);
  572. $mon = Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month));
  573. return $this->_toComment(iconv_substr($mon, 0, 1, 'UTF-8'));
  574. break;
  575. case self::LEAPYEAR :
  576. return 'L';
  577. break;
  578. case self::YEAR_8601 :
  579. return 'o';
  580. break;
  581. case self::YEAR :
  582. return 'Y';
  583. break;
  584. case self::YEAR_SHORT :
  585. return 'y';
  586. break;
  587. case self::YEAR_SHORT_8601 :
  588. return $this->_toComment(substr($this->date('o', $this->getUnixTimestamp(), false), -2, 2));
  589. break;
  590. case self::MERIDIEM :
  591. $am = $this->date('a', $this->getUnixTimestamp(), false);
  592. if ($am == 'am') {
  593. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'am'));
  594. }
  595. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'pm'));
  596. break;
  597. case self::SWATCH :
  598. return 'B';
  599. break;
  600. case self::HOUR_SHORT_AM :
  601. return 'g';
  602. break;
  603. case self::HOUR_SHORT :
  604. return 'G';
  605. break;
  606. case self::HOUR_AM :
  607. return 'h';
  608. break;
  609. case self::HOUR :
  610. return 'H';
  611. break;
  612. case self::MINUTE :
  613. return $this->_toComment(str_pad($this->date('i', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
  614. break;
  615. case self::SECOND :
  616. return $this->_toComment(str_pad($this->date('s', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
  617. break;
  618. case self::MINUTE_SHORT :
  619. return 'i';
  620. break;
  621. case self::SECOND_SHORT :
  622. return 's';
  623. break;
  624. case self::MILLISECOND :
  625. return $this->_toComment($this->_fractional);
  626. break;
  627. case self::TIMEZONE_NAME :
  628. case 'vvvv' :
  629. return 'e';
  630. break;
  631. case self::DAYLIGHT :
  632. return 'I';
  633. break;
  634. case self::GMT_DIFF :
  635. case 'ZZ' :
  636. case 'ZZZ' :
  637. return 'O';
  638. break;
  639. case self::GMT_DIFF_SEP :
  640. return 'P';
  641. break;
  642. case self::TIMEZONE :
  643. case 'v' :
  644. case 'zz' :
  645. case 'zzz' :
  646. return 'T';
  647. break;
  648. case self::TIMEZONE_SECS :
  649. return 'Z';
  650. break;
  651. case self::ISO_8601 :
  652. return 'c';
  653. break;
  654. case self::RFC_2822 :
  655. return 'r';
  656. break;
  657. case self::TIMESTAMP :
  658. return 'U';
  659. break;
  660. case self::ERA :
  661. case 'GG' :
  662. case 'GGG' :
  663. $year = $this->date('Y', $this->getUnixTimestamp(), false);
  664. if ($year < 0) {
  665. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '0')));
  666. }
  667. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '1')));
  668. break;
  669. case self::ERA_NARROW :
  670. $year = $this->date('Y', $this->getUnixTimestamp(), false);
  671. if ($year < 0) {
  672. return $this->_toComment(iconv_substr(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '0')), 0, 1, 'UTF-8')) . '.';
  673. }
  674. return $this->_toComment(iconv_substr(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '1')), 0, 1, 'UTF-8')) . '.';
  675. break;
  676. case self::ERA_NAME :
  677. $year = $this->date('Y', $this->getUnixTimestamp(), false);
  678. if ($year < 0) {
  679. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Names', '0')));
  680. }
  681. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Names', '1')));
  682. break;
  683. case self::DATES :
  684. return $this->_toToken(Zend_Locale_Format::getDateFormat($locale), $locale);
  685. break;
  686. case self::DATE_FULL :
  687. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full')), $locale);
  688. break;
  689. case self::DATE_LONG :
  690. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long')), $locale);
  691. break;
  692. case self::DATE_MEDIUM :
  693. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium')), $locale);
  694. break;
  695. case self::DATE_SHORT :
  696. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short')), $locale);
  697. break;
  698. case self::TIMES :
  699. return $this->_toToken(Zend_Locale_Format::getTimeFormat($locale), $locale);
  700. break;
  701. case self::TIME_FULL :
  702. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'full'), $locale);
  703. break;
  704. case self::TIME_LONG :
  705. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'long'), $locale);
  706. break;
  707. case self::TIME_MEDIUM :
  708. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'medium'), $locale);
  709. break;
  710. case self::TIME_SHORT :
  711. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'short'), $locale);
  712. break;
  713. case self::ATOM :
  714. return 'Y\-m\-d\TH\:i\:sP';
  715. break;
  716. case self::COOKIE :
  717. return 'l\, d\-M\-y H\:i\:s e';
  718. break;
  719. case self::RFC_822 :
  720. return 'D\, d M y H\:i\:s O';
  721. break;
  722. case self::RFC_850 :
  723. return 'l\, d\-M\-y H\:i\:s e';
  724. break;
  725. case self::RFC_1036 :
  726. return 'D\, d M y H\:i\:s O';
  727. break;
  728. case self::RFC_1123 :
  729. return 'D\, d M Y H\:i\:s O';
  730. break;
  731. case self::RFC_3339 :
  732. return 'Y\-m\-d\TH\:i\:sP';
  733. break;
  734. case self::RSS :
  735. return 'D\, d M Y H\:i\:s O';
  736. break;
  737. case self::W3C :
  738. return 'Y\-m\-d\TH\:i\:sP';
  739. break;
  740. }
  741. if ($token == '') {
  742. return '';
  743. }
  744. switch ($token[0]) {
  745. case 'y' :
  746. if ((strlen($token) == 4) && (abs($this->getUnixTimestamp()) <= 0x7FFFFFFF)) {
  747. return 'Y';
  748. }
  749. $length = iconv_strlen($token, 'UTF-8');
  750. return $this->_toComment(str_pad($this->date('Y', $this->getUnixTimestamp(), false), $length, '0', STR_PAD_LEFT));
  751. break;
  752. case 'Y' :
  753. if ((strlen($token) == 4) && (abs($this->getUnixTimestamp()) <= 0x7FFFFFFF)) {
  754. return 'o';
  755. }
  756. $length = iconv_strlen($token, 'UTF-8');
  757. return $this->_toComment(str_pad($this->date('o', $this->getUnixTimestamp(), false), $length, '0', STR_PAD_LEFT));
  758. break;
  759. case 'A' :
  760. $length = iconv_strlen($token, 'UTF-8');
  761. $result = $this->_fractional;
  762. $result += $this->date('s', $this->getUnixTimestamp(), false) * 1000;
  763. $result += $this->date('i', $this->getUnixTimestamp(), false) * 60000;
  764. $result += $this->date('H', $this->getUnixTimestamp(), false) * 3600000;
  765. return $this->_toComment(str_pad($result, $length, '0', STR_PAD_LEFT));
  766. break;
  767. }
  768. return $this->_toComment($token);
  769. }
  770. /**
  771. * Private function to make a comment of a token
  772. *
  773. * @param string $token
  774. * @return string
  775. */
  776. private function _toComment($token)
  777. {
  778. $token = str_split($token);
  779. $result = '';
  780. foreach ($token as $tok) {
  781. $result .= '\\' . $tok;
  782. }
  783. return $result;
  784. }
  785. /**
  786. * Return digit from standard names (english)
  787. * Faster implementation than locale aware searching
  788. *
  789. * @param string $name
  790. * @return integer Number of this month
  791. * @throws Zend_Date_Exception
  792. */
  793. private function _getDigitFromName($name)
  794. {
  795. switch($name) {
  796. case "Jan":
  797. return 1;
  798. case "Feb":
  799. return 2;
  800. case "Mar":
  801. return 3;
  802. case "Apr":
  803. return 4;
  804. case "May":
  805. return 5;
  806. case "Jun":
  807. return 6;
  808. case "Jul":
  809. return 7;
  810. case "Aug":
  811. return 8;
  812. case "Sep":
  813. return 9;
  814. case "Oct":
  815. return 10;
  816. case "Nov":
  817. return 11;
  818. case "Dec":
  819. return 12;
  820. default:
  821. require_once 'Zend/Date/Exception.php';
  822. throw new Zend_Date_Exception('Month ($name) is not a known month');
  823. }
  824. }
  825. /**
  826. * Counts the exact year number
  827. * < 70 - 2000 added, >70 < 100 - 1900, others just returned
  828. *
  829. * @param integer $value year number
  830. * @return integer Number of year
  831. */
  832. public static function getFullYear($value)
  833. {
  834. if ($value >= 0) {
  835. if ($value < 70) {
  836. $value += 2000;
  837. } else if ($value < 100) {
  838. $value += 1900;
  839. }
  840. }
  841. return $value;
  842. }
  843. /**
  844. * Sets the given date as new date or a given datepart as new datepart returning the new datepart
  845. * This could be for example a localized dayname, the date without time,
  846. * the month or only the seconds. There are about 50 different supported date parts.
  847. * For a complete list of supported datepart values look into the docu
  848. *
  849. * @param string|integer|array|Zend_Date $date Date or datepart to set
  850. * @param string $part OPTIONAL Part of the date to set, if null the timestamp is set
  851. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  852. * @return integer|string new datepart
  853. * @throws Zend_Date_Exception
  854. */
  855. public function set($date, $part = null, $locale = null)
  856. {
  857. $zone = $this->getTimezoneFromString($date);
  858. $this->setTimezone($zone);
  859. $result = $this->_calculate('set', $date, $part, $locale);
  860. return $result;
  861. }
  862. /**
  863. * Adds a date or datepart to the existing date, by extracting $part from $date,
  864. * and modifying this object by adding that part. The $part is then extracted from
  865. * this object and returned as an integer or numeric string (for large values, or $part's
  866. * corresponding to pre-defined formatted date strings).
  867. * This could be for example a ISO 8601 date, the hour the monthname or only the minute.
  868. * There are about 50 different supported date parts.
  869. * For a complete list of supported datepart values look into the docu.
  870. *
  871. * @param string|integer|array|Zend_Date $date Date or datepart to add
  872. * @param string $part OPTIONAL Part of the date to add, if null the timestamp is added
  873. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  874. * @return integer|string new datepart
  875. * @throws Zend_Date_Exception
  876. */
  877. public function add($date, $part = null, $locale = null)
  878. {
  879. $this->_calculate('add', $date, $part, $locale);
  880. $result = $this->get($part, $locale);
  881. return $result;
  882. }
  883. /**
  884. * Subtracts a date from another date.
  885. * This could be for example a RFC2822 date, the time,
  886. * the year or only the timestamp. There are about 50 different supported date parts.
  887. * For a complete list of supported datepart values look into the docu
  888. * Be aware: Adding -2 Months is not equal to Subtracting 2 Months !!!
  889. *
  890. * @param string|integer|array|Zend_Date $date Date or datepart to subtract
  891. * @param string $part OPTIONAL Part of the date to sub, if null the timestamp is subtracted
  892. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  893. * @return integer|string new datepart
  894. * @throws Zend_Date_Exception
  895. */
  896. public function sub($date, $part = null, $locale = null)
  897. {
  898. $this->_calculate('sub', $date, $part, $locale);
  899. $result = $this->get($part, $locale);
  900. return $result;
  901. }
  902. /**
  903. * Compares a date or datepart with the existing one.
  904. * Returns -1 if earlier, 0 if equal and 1 if later.
  905. *
  906. * @param string|integer|array|Zend_Date $date Date or datepart to compare with the date object
  907. * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is subtracted
  908. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  909. * @return integer 0 = equal, 1 = later, -1 = earlier
  910. * @throws Zend_Date_Exception
  911. */
  912. public function compare($date, $part = null, $locale = null)
  913. {
  914. $compare = $this->_calculate('cmp', $date, $part, $locale);
  915. if ($compare > 0) {
  916. return 1;
  917. } else if ($compare < 0) {
  918. return -1;
  919. }
  920. return 0;
  921. }
  922. /**
  923. * Returns a new instance of Zend_Date with the selected part copied.
  924. * To make an exact copy, use PHP's clone keyword.
  925. * For a complete list of supported date part values look into the docu.
  926. * If a date part is copied, all other date parts are set to standard values.
  927. * For example: If only YEAR is copied, the returned date object is equal to
  928. * 01-01-YEAR 00:00:00 (01-01-1970 00:00:00 is equal to timestamp 0)
  929. * If only HOUR is copied, the returned date object is equal to
  930. * 01-01-1970 HOUR:00:00 (so $this contains a timestamp equal to a timestamp of 0 plus HOUR).
  931. *
  932. * @param string $part Part of the date to compare, if null the timestamp is subtracted
  933. * @param string|Zend_Locale $locale OPTIONAL New object's locale. No adjustments to timezone are made.
  934. * @return Zend_Date
  935. */
  936. public function copyPart($part, $locale = null)
  937. {
  938. $clone = clone $this; // copy all instance variables
  939. $clone->setUnixTimestamp(0); // except the timestamp
  940. if ($locale != null) {
  941. $clone->setLocale($locale); // set an other locale if selected
  942. }
  943. $clone->set($this, $part);
  944. return $clone;
  945. }
  946. /**
  947. * Internal function, returns the offset of a given timezone
  948. *
  949. * @param string $zone
  950. * @return integer
  951. */
  952. public function getTimezoneFromString($zone)
  953. {
  954. if (is_array($zone)) {
  955. return $this->getTimezone();
  956. }
  957. if ($zone instanceof Zend_Date) {
  958. return $zone->getTimezone();
  959. }
  960. $match = array();
  961. preg_match('/\dZ$/', $zone, $match);
  962. if (!empty($match)) {
  963. return "Etc/UTC";
  964. }
  965. preg_match('/([+-]\d{2}):{0,1}\d{2}/', $zone, $match);
  966. if (!empty($match) and ($match[count($match) - 1] <= 12) and ($match[count($match) - 1] >= -12)) {
  967. $zone = "Etc/GMT";
  968. $zone .= ($match[count($match) - 1] < 0) ? "+" : "-";
  969. $zone .= (int) abs($match[count($match) - 1]);
  970. return $zone;
  971. }
  972. preg_match('/([[:alpha:]\/]{3,30})/', $zone, $match);
  973. try {
  974. if (!empty($match) and (!is_int($match[count($match) - 1]))) {
  975. $oldzone = $this->getTimezone();
  976. $this->setTimezone($match[count($match) - 1]);
  977. $result = $this->getTimezone();
  978. $this->setTimezone($oldzone);
  979. if ($result !== $oldzone) {
  980. return $match[count($match) - 1];
  981. }
  982. }
  983. } catch (Exception $e) {
  984. // fall through
  985. }
  986. return $this->getTimezone();
  987. }
  988. /**
  989. * Calculates the date or object
  990. *
  991. * @param string $calc Calculation to make
  992. * @param string|integer $date Date for calculation
  993. * @param string|integer $comp Second date for calculation
  994. * @param boolean|integer $dst Use dst correction if option is set
  995. * @return integer|string|Zend_Date new timestamp or Zend_Date depending on calculation
  996. */
  997. private function _assign($calc, $date, $comp = 0, $dst = false)
  998. {
  999. switch ($calc) {
  1000. case 'set' :
  1001. if (!empty($comp)) {
  1002. $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$sub, $this->getUnixTimestamp(), $comp));
  1003. }
  1004. $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$add, $this->getUnixTimestamp(), $date));
  1005. $value = $this->getUnixTimestamp();
  1006. break;
  1007. case 'add' :
  1008. $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$add, $this->getUnixTimestamp(), $date));
  1009. $value = $this->getUnixTimestamp();
  1010. break;
  1011. case 'sub' :
  1012. $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$sub, $this->getUnixTimestamp(), $date));
  1013. $value = $this->getUnixTimestamp();
  1014. break;
  1015. default :
  1016. // cmp - compare
  1017. return call_user_func(Zend_Locale_Math::$comp, $comp, $date);
  1018. break;
  1019. }
  1020. // dst-correction if 'fix_dst' = true and dst !== false but only for non UTC and non GMT
  1021. if ((self::$_options['fix_dst'] === true) and ($dst !== false) and ($this->_dst === true)) {
  1022. $hour = $this->get(self::HOUR);
  1023. if ($hour != $dst) {
  1024. if (($dst == ($hour + 1)) or ($dst == ($hour - 23))) {
  1025. $value += 3600;
  1026. } else if (($dst == ($hour - 1)) or ($dst == ($hour + 23))) {
  1027. $value -= 3600;
  1028. }
  1029. $this->setUnixTimestamp($value);
  1030. }
  1031. }
  1032. return $this->getUnixTimestamp();
  1033. }
  1034. /**
  1035. * Calculates the date or object
  1036. *
  1037. * @param string $calc Calculation to make, one of: 'add'|'sub'|'cmp'|'copy'|'set'
  1038. * @param string|integer|array|Zend_Date $date Date or datepart to calculate with
  1039. * @param string $part Part of the date to calculate, if null the timestamp is used
  1040. * @param string|Zend_Locale $locale Locale for parsing input
  1041. * @return integer|string|Zend_Date new timestamp
  1042. * @throws Zend_Date_Exception
  1043. */
  1044. private function _calculate($calc, $date, $part, $locale)
  1045. {
  1046. if ($date === null) {
  1047. require_once 'Zend/Date/Exception.php';
  1048. throw new Zend_Date_Exception('parameter $date must be set, null is not allowed');
  1049. }
  1050. if (($part !== null) && (strlen($part) !== 2) && (Zend_Locale::isLocale($part, null, false))) {
  1051. $locale = $part;
  1052. $part = null;
  1053. }
  1054. if ($locale === null) {
  1055. $locale = $this->getLocale();
  1056. }
  1057. $locale = (string) $locale;
  1058. // Create date parts
  1059. $year = $this->get(self::YEAR);
  1060. $month = $this->get(self::MONTH_SHORT);
  1061. $day = $this->get(self::DAY_SHORT);
  1062. $hour = $this->get(self::HOUR_SHORT);
  1063. $minute = $this->get(self::MINUTE_SHORT);
  1064. $second = $this->get(self::SECOND_SHORT);
  1065. // If object extract value
  1066. if ($date instanceof Zend_Date) {
  1067. $date = $date->get($part, $locale);
  1068. }
  1069. if (is_array($date) === true) {
  1070. if (empty($part) === false) {
  1071. switch($part) {
  1072. // Fall through
  1073. case self::DAY:
  1074. case self::DAY_SHORT:
  1075. if (isset($date['day']) === true) {
  1076. $date = $date['day'];
  1077. }
  1078. break;
  1079. // Fall through
  1080. case self::WEEKDAY_SHORT:
  1081. case self::WEEKDAY:
  1082. case self::WEEKDAY_8601:
  1083. case self::WEEKDAY_DIGIT:
  1084. case self::WEEKDAY_NARROW:
  1085. case self::WEEKDAY_NAME:
  1086. if (isset($date['weekday']) === true) {
  1087. $date = $date['weekday'];
  1088. $part = self::WEEKDAY_DIGIT;
  1089. }
  1090. break;
  1091. case self::DAY_OF_YEAR:
  1092. if (isset($date['day_of_year']) === true) {
  1093. $date = $date['day_of_year'];
  1094. }
  1095. break;
  1096. // Fall through
  1097. case self::MONTH:
  1098. case self::MONTH_SHORT:
  1099. case self::MONTH_NAME:
  1100. case self::MONTH_NAME_SHORT:
  1101. case self::MONTH_NAME_NARROW:
  1102. if (isset($date['month']) === true) {
  1103. $date = $date['month'];
  1104. }
  1105. break;
  1106. // Fall through
  1107. case self::YEAR:
  1108. case self::YEAR_SHORT:
  1109. case self::YEAR_8601:
  1110. case self::YEAR_SHORT_8601:
  1111. if (isset($date['year']) === true) {
  1112. $date = $date['year'];
  1113. }
  1114. break;
  1115. // Fall through
  1116. case self::HOUR:
  1117. case self::HOUR_AM:
  1118. case self::HOUR_SHORT:
  1119. case self::HOUR_SHORT_AM:
  1120. if (isset($date['hour']) === true) {
  1121. $date = $date['hour'];
  1122. }
  1123. break;
  1124. // Fall through
  1125. case self::MINUTE:
  1126. case self::MINUTE_SHORT:
  1127. if (isset($date['minute']) === true) {
  1128. $date = $date['minute'];
  1129. }
  1130. break;
  1131. // Fall through
  1132. case self::SECOND:
  1133. case self::SECOND_SHORT:
  1134. if (isset($date['second']) === true) {
  1135. $date = $date['second'];
  1136. }
  1137. break;
  1138. // Fall through
  1139. case self::TIMEZONE:
  1140. case self::TIMEZONE_NAME:
  1141. if (isset($date['timezone']) === true) {
  1142. $date = $date['timezone'];
  1143. }
  1144. break;
  1145. case self::TIMESTAMP:
  1146. if (isset($date['timestamp']) === true) {
  1147. $date = $date['timestamp'];
  1148. }
  1149. break;
  1150. case self::WEEK:
  1151. if (isset($date['week']) === true) {
  1152. $date = $date['week'];
  1153. }
  1154. break;
  1155. case self::TIMEZONE_SECS:
  1156. if (isset($date['gmtsecs']) === true) {
  1157. $date = $date['gmtsecs'];
  1158. }
  1159. break;
  1160. default:
  1161. require_once 'Zend/Date/Exception.php';
  1162. throw new Zend_Date_Exception("datepart for part ($part) not found in array");
  1163. break;
  1164. }
  1165. } else {
  1166. $hours = 0;
  1167. if (isset($date['hour']) === true) {
  1168. $hours = $date['hour'];
  1169. }
  1170. $minutes = 0;
  1171. if (isset($date['minute']) === true) {
  1172. $minutes = $date['minute'];
  1173. }
  1174. $seconds = 0;
  1175. if (isset($date['second']) === true) {
  1176. $seconds = $date['second'];
  1177. }
  1178. $months = 0;
  1179. if (isset($date['month']) === true) {
  1180. $months = $date['month'];
  1181. }
  1182. $days = 0;
  1183. if (isset($date['day']) === true) {
  1184. $days = $date['day'];
  1185. }
  1186. $years = 0;
  1187. if (isset($date['year']) === true) {
  1188. $years = $date['year'];
  1189. }
  1190. return $this->_assign($calc, $this->mktime($hours, $minutes, $seconds, $months, $days, $years, true),
  1191. $this->mktime($hour, $minute, $second, $month, $day, $year, true), $hour);
  1192. }
  1193. }
  1194. // $date as object, part of foreign date as own date
  1195. switch($part) {
  1196. // day formats
  1197. case self::DAY:
  1198. if (is_numeric($date)) {
  1199. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true),
  1200. $this->mktime(0, 0, 0, 1, 1 + intval($day), 1970, true), $hour);
  1201. }
  1202. require_once 'Zend/Date/Exception.php';
  1203. throw new Zend_Date_Exception("invalid date ($date) operand, day expected", $date);
  1204. break;
  1205. case self::WEEKDAY_SHORT:
  1206. $daylist = Zend_Locale_Data::getList($locale, 'day');
  1207. $weekday = (int) $this->get(self::WEEKDAY_DIGIT, $locale);
  1208. $cnt = 0;
  1209. foreach ($daylist as $key => $value) {
  1210. if (strtoupper(iconv_substr($value, 0, 3, 'UTF-8')) == strtoupper($date)) {
  1211. $found = $cnt;
  1212. break;
  1213. }
  1214. ++$cnt;
  1215. }
  1216. // Weekday found
  1217. if ($cnt < 7) {
  1218. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
  1219. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1220. }
  1221. // Weekday not found
  1222. require_once 'Zend/Date/Exception.php';
  1223. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", $date);
  1224. break;
  1225. case self::DAY_SHORT:
  1226. if (is_numeric($date)) {
  1227. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true),
  1228. $this->mktime(0, 0, 0, 1, 1 + intval($day), 1970, true), $hour);
  1229. }
  1230. require_once 'Zend/Date/Exception.php';
  1231. throw new Zend_Date_Exception("invalid date ($date) operand, day expected", $date);
  1232. break;
  1233. case self::WEEKDAY:
  1234. $daylist = Zend_Locale_Data::getList($locale, 'day');
  1235. $weekday = (int) $this->get(self::WEEKDAY_DIGIT, $locale);
  1236. $cnt = 0;
  1237. foreach ($daylist as $key => $value) {
  1238. if (strtoupper($value) == strtoupper($date)) {
  1239. $found = $cnt;
  1240. break;
  1241. }
  1242. ++$cnt;
  1243. }
  1244. // Weekday found
  1245. if ($cnt < 7) {
  1246. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
  1247. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1248. }
  1249. // Weekday not found
  1250. require_once 'Zend/Date/Exception.php';
  1251. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", $date);
  1252. break;
  1253. case self::WEEKDAY_8601:
  1254. $weekday = (int) $this->get(self::WEEKDAY_8601, $locale);
  1255. if ((intval($date) > 0) and (intval($date) < 8)) {
  1256. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true),
  1257. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1258. }
  1259. // Weekday not found
  1260. require_once 'Zend/Date/Exception.php';
  1261. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", $date);
  1262. break;
  1263. case self::DAY_SUFFIX:
  1264. require_once 'Zend/Date/Exception.php';
  1265. throw new Zend_Date_Exception('day suffix not supported', $date);
  1266. break;
  1267. case self::WEEKDAY_DIGIT:
  1268. $weekday = (int) $this->get(self::WEEKDAY_DIGIT, $locale);
  1269. if (is_numeric($date) and (intval($date) >= 0) and (intval($date) < 7)) {
  1270. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $date, 1970, true),
  1271. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1272. }
  1273. // Weekday not found
  1274. require_once 'Zend/Date/Exception.php';
  1275. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", $date);
  1276. break;
  1277. case self::DAY_OF_YEAR:
  1278. if (is_numeric($date)) {
  1279. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $date, 1970, true),
  1280. $this->mktime(0, 0, 0, $month, 1 + $day, 1970, true), $hour);
  1281. }
  1282. require_once 'Zend/Date/Exception.php';
  1283. throw new Zend_Date_Exception("invalid date ($date) operand, day expected", $date);
  1284. break;
  1285. case self::WEEKDAY_NARROW:
  1286. $daylist = Zend_Locale_Data::getList($locale, 'day', array('gregorian', 'format', 'abbreviated'));
  1287. $weekday = (int) $this->get(self::WEEKDAY_DIGIT, $locale);
  1288. $cnt = 0;
  1289. foreach ($daylist as $key => $value) {
  1290. if (strtoupper(iconv_substr($value, 0, 1, 'UTF-8')) == strtoupper($date)) {
  1291. $found = $cnt;
  1292. break;
  1293. }
  1294. ++$cnt;
  1295. }
  1296. // Weekday found
  1297. if ($cnt < 7) {
  1298. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
  1299. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1300. }
  1301. // Weekday not found
  1302. require_once 'Zend/Date/Exception.php';
  1303. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", $date);
  1304. break;
  1305. case self::WEEKDAY_NAME:
  1306. $daylist = Zend_Locale_Data::getList($locale, 'day', array('gregorian', 'format', 'abbreviated'));
  1307. $weekday = (int) $this->get(self::WEEKDAY_DIGIT, $locale);
  1308. $cnt = 0;
  1309. foreach ($daylist as $key => $value) {
  1310. if (strtoupper($value) == strtoupper($date)) {
  1311. $found = $cnt;
  1312. break;
  1313. }
  1314. ++$cnt;
  1315. }
  1316. // Weekday found
  1317. if ($cnt < 7) {
  1318. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
  1319. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1320. }
  1321. // Weekday not found
  1322. require_once 'Zend/Date/Exception.php';
  1323. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", $date);
  1324. break;
  1325. // week formats
  1326. case self::WEEK:
  1327. if (is_numeric($date)) {
  1328. $week = (int) $this->get(self::WEEK, $locale);
  1329. return $this->_assign($calc, parent::mktime(0, 0, 0, 1, 1 + ($date * 7), 1970, true),
  1330. parent::mktime(0, 0, 0, 1, 1 + ($week * 7), 1970, true), $hour);
  1331. }
  1332. require_once 'Zend/Date/Exception.php';
  1333. throw new Zend_Date_Exception("invalid date ($date) operand, week expected", $date);
  1334. break;
  1335. // month formats
  1336. case self::MONTH_NAME:
  1337. $monthlist = Zend_Locale_Data::getList($locale, 'month');
  1338. $cnt = 0;
  1339. foreach ($monthlist as $key => $value) {
  1340. if (strtoupper($value) == strtoupper($date)) {
  1341. $found = $key;
  1342. break;
  1343. }
  1344. ++$cnt;
  1345. }
  1346. $date = array_search($date, $monthlist);
  1347. // Monthname found
  1348. if ($cnt < 12) {
  1349. $fixday = 0;
  1350. if ($calc == 'add') {
  1351. $date += $found;
  1352. $calc = 'set';
  1353. if (self::$_options['extend_month'] == false) {
  1354. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1355. if ($parts['mday'] != $day) {
  1356. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1357. }
  1358. }
  1359. } else if ($calc == 'sub') {
  1360. $date = $month - $found;
  1361. $calc = 'set';
  1362. if (self::$_options['extend_month'] == false) {
  1363. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1364. if ($parts['mday'] != $day) {
  1365. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1366. }
  1367. }
  1368. }
  1369. return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
  1370. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1371. }
  1372. // Monthname not found
  1373. require_once 'Zend/Date/Exception.php';
  1374. throw new Zend_Date_Exception("invalid date ($date) operand, month expected", $date);
  1375. break;
  1376. case self::MONTH:
  1377. if (is_numeric($date)) {
  1378. $fixday = 0;
  1379. if ($calc == 'add') {
  1380. $date += $month;
  1381. $calc = 'set';
  1382. if (self::$_options['extend_month'] == false) {
  1383. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1384. if ($parts['mday'] != $day) {
  1385. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1386. }
  1387. }
  1388. } else if ($calc == 'sub') {
  1389. $date = $month - $date;
  1390. $calc = 'set';
  1391. if (self::$_options['extend_month'] == false) {
  1392. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1393. if ($parts['mday'] != $day) {
  1394. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1395. }
  1396. }
  1397. }
  1398. return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
  1399. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1400. }
  1401. require_once 'Zend/Date/Exception.php';
  1402. throw new Zend_Date_Exception("invalid date ($date) operand, month expected", $date);
  1403. break;
  1404. case self::MONTH_NAME_SHORT:
  1405. $monthlist = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'format', 'abbreviated'));
  1406. $cnt = 0;
  1407. foreach ($monthlist as $key => $value) {
  1408. if (strtoupper($value) == strtoupper($date)) {
  1409. $found = $key;
  1410. break;
  1411. }
  1412. ++$cnt;
  1413. }
  1414. $date = array_search($date, $monthlist);
  1415. // Monthname found
  1416. if ($cnt < 12) {
  1417. $fixday = 0;
  1418. if ($calc == 'add') {
  1419. $date += $found;
  1420. $calc = 'set';
  1421. if (self::$_options['extend_month'] === false) {
  1422. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1423. if ($parts['mday'] != $day) {
  1424. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1425. }
  1426. }
  1427. } else if ($calc == 'sub') {
  1428. $date = $month - $found;
  1429. $calc = 'set';
  1430. if (self::$_options['extend_month'] === false) {
  1431. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1432. if ($parts['mday'] != $day) {
  1433. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1434. }
  1435. }
  1436. }
  1437. return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
  1438. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1439. }
  1440. // Monthname not found
  1441. require_once 'Zend/Date/Exception.php';
  1442. throw new Zend_Date_Exception("invalid date ($date) operand, month expected", $date);
  1443. break;
  1444. case self::MONTH_SHORT:
  1445. if (is_numeric($date) === true) {
  1446. $fixday = 0;
  1447. if ($calc === 'add') {
  1448. $date += $month;
  1449. $calc = 'set';
  1450. if (self::$_options['extend_month'] === false) {
  1451. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1452. if ($parts['mday'] != $day) {
  1453. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1454. }
  1455. }
  1456. } else if ($calc === 'sub') {
  1457. $date = $month - $date;
  1458. $calc = 'set';
  1459. if (self::$_options['extend_month'] === false) {
  1460. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1461. if ($parts['mday'] != $day) {
  1462. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1463. }
  1464. }
  1465. }
  1466. return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
  1467. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1468. }
  1469. require_once 'Zend/Date/Exception.php';
  1470. throw new Zend_Date_Exception("invalid date ($date) operand, month expected", $date);
  1471. break;
  1472. case self::MONTH_DAYS:
  1473. require_once 'Zend/Date/Exception.php';
  1474. throw new Zend_Date_Exception('month days not supported', $date);
  1475. break;
  1476. case self::MONTH_NAME_NARROW:
  1477. $monthlist = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'stand-alone', 'narrow'));
  1478. $cnt = 0;
  1479. foreach ($monthlist as $key => $value) {
  1480. if (strtoupper($value) === strtoupper($date)) {
  1481. $found = $key;
  1482. break;
  1483. }
  1484. ++$cnt;
  1485. }
  1486. $date = array_search($date, $monthlist);
  1487. // Monthname found
  1488. if ($cnt < 12) {
  1489. $fixday = 0;
  1490. if ($calc === 'add') {
  1491. $date += $found;
  1492. $calc = 'set';
  1493. if (self::$_options['extend_month'] === false) {
  1494. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1495. if ($parts['mday'] != $day) {
  1496. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1497. }
  1498. }
  1499. } else if ($calc === 'sub') {
  1500. $date = $month - $found;
  1501. $calc = 'set';
  1502. if (self::$_options['extend_month'] === false) {
  1503. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1504. if ($parts['mday'] != $day) {
  1505. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1506. }
  1507. }
  1508. }
  1509. return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
  1510. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1511. }
  1512. // Monthname not found
  1513. require_once 'Zend/Date/Exception.php';
  1514. throw new Zend_Date_Exception("invalid date ($date) operand, month expected", $date);
  1515. break;
  1516. // year formats
  1517. case self::LEAPYEAR:
  1518. require_once 'Zend/Date/Exception.php';
  1519. throw new Zend_Date_Exception('leap year not supported', $date);
  1520. break;
  1521. case self::YEAR_8601:
  1522. if (is_numeric($date)) {
  1523. if ($calc === 'add') {
  1524. $date += $year;
  1525. $calc = 'set';
  1526. } else if ($calc === 'sub') {
  1527. $date = $year - $date;
  1528. $calc = 'set';
  1529. }
  1530. return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, intval($date), true),
  1531. $this->mktime(0, 0, 0, $month, $day, $year, true), false);
  1532. }
  1533. require_once 'Zend/Date/Exception.php';
  1534. throw new Zend_Date_Exception("invalid date ($date) operand, year expected", $date);
  1535. break;
  1536. case self::YEAR:
  1537. if (is_numeric($date)) {
  1538. if ($calc === 'add') {
  1539. $date += $year;
  1540. $calc = 'set';
  1541. } else if ($calc === 'sub') {
  1542. $date = $year - $date;
  1543. $calc = 'set';
  1544. }
  1545. return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, intval($date), true),
  1546. $this->mktime(0, 0, 0, $month, $day, $year, true), false);
  1547. }
  1548. require_once 'Zend/Date/Exception.php';
  1549. throw new Zend_Date_Exception("invalid date ($date) operand, year expected", $date);
  1550. break;
  1551. case self::YEAR_SHORT:
  1552. if (is_numeric($date)) {
  1553. $date = intval($date);
  1554. if (($calc == 'set') || ($calc == 'cmp')) {
  1555. $date = self::getFullYear($date);
  1556. }
  1557. if ($calc === 'add') {
  1558. $date += $year;
  1559. $calc = 'set';
  1560. } else if ($calc === 'sub') {
  1561. $date = $year - $date;
  1562. $calc = 'set';
  1563. }
  1564. return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, $date, true),
  1565. $this->mktime(0, 0, 0, $month, $day, $year, true), false);
  1566. }
  1567. require_once 'Zend/Date/Exception.php';
  1568. throw new Zend_Date_Exception("invalid date ($date) operand, year expected", $date);
  1569. break;
  1570. case self::YEAR_SHORT_8601:
  1571. if (is_numeric($date)) {
  1572. $date = intval($date);
  1573. if (($calc === 'set') || ($calc === 'cmp')) {
  1574. $date = self::getFullYear($date);
  1575. }
  1576. if ($calc === 'add') {
  1577. $date += $year;
  1578. $calc = 'set';
  1579. } else if ($calc === 'sub') {
  1580. $date = $year - $date;
  1581. $calc = 'set';
  1582. }
  1583. return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, $date, true),
  1584. $this->mktime(0, 0, 0, $month, $day, $year, true), false);
  1585. }
  1586. require_once 'Zend/Date/Exception.php';
  1587. throw new Zend_Date_Exception("invalid date ($date) operand, year expected", $date);
  1588. break;
  1589. // time formats
  1590. case self::MERIDIEM:
  1591. require_once 'Zend/Date/Exception.php';
  1592. throw new Zend_Date_Exception('meridiem not supported', $date);
  1593. break;
  1594. case self::SWATCH:
  1595. if (is_numeric($date)) {
  1596. $rest = intval($date);
  1597. $hours = floor($rest * 24 / 1000);
  1598. $rest = $rest - ($hours * 1000 / 24);
  1599. $minutes = floor($rest * 1440 / 1000);
  1600. $rest = $rest - ($minutes * 1000 / 1440);
  1601. $seconds = floor($rest * 86400 / 1000);
  1602. return $this->_assign($calc, $this->mktime($hours, $minutes, $seconds, 1, 1, 1970, true),
  1603. $this->mktime($hour, $minute, $second, 1, 1, 1970, true), false);
  1604. }
  1605. require_once 'Zend/Date/Exception.php';
  1606. throw new Zend_Date_Exception("invalid date ($date) operand, swatchstamp expected", $date);
  1607. break;
  1608. case self::HOUR_SHORT_AM:
  1609. if (is_numeric($date)) {
  1610. return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
  1611. $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
  1612. }
  1613. require_once 'Zend/Date/Exception.php';
  1614. throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", $date);
  1615. break;
  1616. case self::HOUR_SHORT:
  1617. if (is_numeric($date)) {
  1618. return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
  1619. $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
  1620. }
  1621. require_once 'Zend/Date/Exception.php';
  1622. throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", $date);
  1623. break;
  1624. case self::HOUR_AM:
  1625. if (is_numeric($date)) {
  1626. return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
  1627. $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
  1628. }
  1629. require_once 'Zend/Date/Exception.php';
  1630. throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", $date);
  1631. break;
  1632. case self::HOUR:
  1633. if (is_numeric($date)) {
  1634. return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
  1635. $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
  1636. }
  1637. require_once 'Zend/Date/Exception.php';
  1638. throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", $date);
  1639. break;
  1640. case self::MINUTE:
  1641. if (is_numeric($date)) {
  1642. return $this->_assign($calc, $this->mktime(0, intval($date), 0, 1, 1, 1970, true),
  1643. $this->mktime(0, $minute, 0, 1, 1, 1970, true), false);
  1644. }
  1645. require_once 'Zend/Date/Exception.php';
  1646. throw new Zend_Date_Exception("invalid date ($date) operand, minute expected", $date);
  1647. break;
  1648. case self::SECOND:
  1649. if (is_numeric($date)) {
  1650. return $this->_assign($calc, $this->mktime(0, 0, intval($date), 1, 1, 1970, true),
  1651. $this->mktime(0, 0, $second, 1, 1, 1970, true), false);
  1652. }
  1653. require_once 'Zend/Date/Exception.php';
  1654. throw new Zend_Date_Exception("invalid date ($date) operand, second expected", $date);
  1655. break;
  1656. case self::MILLISECOND:
  1657. if (is_numeric($date)) {
  1658. switch($calc) {
  1659. case 'set' :
  1660. return $this->setMillisecond($date);
  1661. break;
  1662. case 'add' :
  1663. return $this->addMillisecond($date);
  1664. break;
  1665. case 'sub' :
  1666. return $this->subMillisecond($date);
  1667. break;
  1668. }
  1669. return $this->compareMillisecond($date);
  1670. }
  1671. require_once 'Zend/Date/Exception.php';
  1672. throw new Zend_Date_Exception("invalid date ($date) operand, milliseconds expected", $date);
  1673. break;
  1674. case self::MINUTE_SHORT:
  1675. if (is_numeric($date)) {
  1676. return $this->_assign($calc, $this->mktime(0, intval($date), 0, 1, 1, 1970, true),
  1677. $this->mktime(0, $minute, 0, 1, 1, 1970, true), false);
  1678. }
  1679. require_once 'Zend/Date/Exception.php';
  1680. throw new Zend_Date_Exception("invalid date ($date) operand, minute expected", $date);
  1681. break;
  1682. case self::SECOND_SHORT:
  1683. if (is_numeric($date)) {
  1684. return $this->_assign($calc, $this->mktime(0, 0, intval($date), 1, 1, 1970, true),
  1685. $this->mktime(0, 0, $second, 1, 1, 1970, true), false);
  1686. }
  1687. require_once 'Zend/Date/Exception.php';
  1688. throw new Zend_Date_Exception("invalid date ($date) operand, second expected", $date);
  1689. break;
  1690. // timezone formats
  1691. // break intentionally omitted
  1692. case self::TIMEZONE_NAME:
  1693. case self::TIMEZONE:
  1694. case self::TIMEZONE_SECS:
  1695. require_once 'Zend/Date/Exception.php';
  1696. throw new Zend_Date_Exception('timezone not supported', $date);
  1697. break;
  1698. case self::DAYLIGHT:
  1699. require_once 'Zend/Date/Exception.php';
  1700. throw new Zend_Date_Exception('daylight not supported', $date);
  1701. break;
  1702. case self::GMT_DIFF:
  1703. case self::GMT_DIFF_SEP:
  1704. require_once 'Zend/Date/Exception.php';
  1705. throw new Zend_Date_Exception('gmtdiff not supported', $date);
  1706. break;
  1707. // date strings
  1708. case self::ISO_8601:
  1709. // (-)YYYY-MM-dd
  1710. preg_match('/^(-{0,1}\d{4})-(\d{2})-(\d{2})/', $date, $datematch);
  1711. // (-)YY-MM-dd
  1712. if (empty($datematch)) {
  1713. preg_match('/^(-{0,1}\d{2})-(\d{2})-(\d{2})/', $date, $datematch);
  1714. }
  1715. // (-)YYYYMMdd
  1716. if (empty($datematch)) {
  1717. preg_match('/^(-{0,1}\d{4})(\d{2})(\d{2})/', $date, $datematch);
  1718. }
  1719. // (-)YYMMdd
  1720. if (empty($datematch)) {
  1721. preg_match('/^(-{0,1}\d{2})(\d{2})(\d{2})/', $date, $datematch);
  1722. }
  1723. $tmpdate = $date;
  1724. if (!empty($datematch)) {
  1725. $dateMatchCharCount = iconv_strlen($datematch[0], 'UTF-8');
  1726. $tmpdate = iconv_substr($date,
  1727. $dateMatchCharCount,
  1728. iconv_strlen($date, 'UTF-8') - $dateMatchCharCount,
  1729. 'UTF-8');
  1730. }
  1731. // (T)hh:mm:ss
  1732. preg_match('/[T,\s]{0,1}(\d{2}):(\d{2}):(\d{2})/', $tmpdate, $timematch);
  1733. if (empty($timematch)) {
  1734. preg_match('/[T,\s]{0,1}(\d{2})(\d{2})(\d{2})/', $tmpdate, $timematch);
  1735. }
  1736. if (empty($datematch) and empty($timematch)) {
  1737. require_once 'Zend/Date/Exception.php';
  1738. throw new Zend_Date_Exception("unsupported ISO8601 format ($date)", $date);
  1739. }
  1740. if (!empty($timematch)) {
  1741. $timeMatchCharCount = iconv_strlen($timematch[0], 'UTF-8');
  1742. $tmpdate = iconv_substr($tmpdate,
  1743. $timeMatchCharCount,
  1744. iconv_strlen($tmpdate, 'UTF-8') - $timeMatchCharCount,
  1745. 'UTF-8');
  1746. }
  1747. if (empty($datematch)) {
  1748. $datematch[1] = 1970;
  1749. $datematch[2] = 1;
  1750. $datematch[3] = 1;
  1751. } else if (iconv_strlen($datematch[1], 'UTF-8') == 2) {
  1752. $datematch[1] = self::getFullYear($datematch[1]);
  1753. }
  1754. if (empty($timematch)) {
  1755. $timematch[1] = 0;
  1756. $timematch[2] = 0;
  1757. $timematch[3] = 0;
  1758. }
  1759. if (($calc == 'set') || ($calc == 'cmp')) {
  1760. --$datematch[2];
  1761. --$month;
  1762. --$datematch[3];
  1763. --$day;
  1764. $datematch[1] -= 1970;
  1765. $year -= 1970;
  1766. }
  1767. return $this->_assign($calc, $this->mktime($timematch[1], $timematch[2], $timematch[3], 1 + $datematch[2], 1 + $datematch[3], 1970 + $datematch[1], false),
  1768. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false);
  1769. break;
  1770. case self::RFC_2822:
  1771. $result = preg_match('/^\w{3},\s(\d{1,2})\s(\w{3})\s(\d{4})\s(\d{2}):(\d{2}):{0,1}(\d{0,2})\s([+-]{1}\d{4})$/', $date, $match);
  1772. if (!$result) {
  1773. require_once 'Zend/Date/Exception.php';
  1774. throw new Zend_Date_Exception("no RFC 2822 format ($date)", $date);
  1775. }
  1776. $months = $this->_getDigitFromName($match[2]);
  1777. if (($calc == 'set') || ($calc == 'cmp')) {
  1778. --$months;
  1779. --$month;
  1780. --$match[1];
  1781. --$day;
  1782. $match[3] -= 1970;
  1783. $year -= 1970;
  1784. }
  1785. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], false),
  1786. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false);
  1787. break;
  1788. case self::TIMESTAMP:
  1789. if (is_numeric($date)) {
  1790. return $this->_assign($calc, $date, $this->getUnixTimestamp());
  1791. }
  1792. require_once 'Zend/Date/Exception.php';
  1793. throw new Zend_Date_Exception("invalid date ($date) operand, timestamp expected", $date);
  1794. break;
  1795. // additional formats
  1796. // break intentionally omitted
  1797. case self::ERA:
  1798. case self::ERA_NAME:
  1799. require_once 'Zend/Date/Exception.php';
  1800. throw new Zend_Date_Exception('era not supported', $date);
  1801. break;
  1802. case self::DATES:
  1803. try {
  1804. $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
  1805. if (($calc == 'set') || ($calc == 'cmp')) {
  1806. --$parsed['month'];
  1807. --$month;
  1808. --$parsed['day'];
  1809. --$day;
  1810. $parsed['year'] -= 1970;
  1811. $year -= 1970;
  1812. }
  1813. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1814. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1815. } catch (Zend_Locale_Exception $e) {
  1816. require_once 'Zend/Date/Exception.php';
  1817. throw new Zend_Date_Exception($e->getMessage(), $date);
  1818. }
  1819. break;
  1820. case self::DATE_FULL:
  1821. try {
  1822. $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full'));
  1823. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1824. if (($calc == 'set') || ($calc == 'cmp')) {
  1825. --$parsed['month'];
  1826. --$month;
  1827. --$parsed['day'];
  1828. --$day;
  1829. $parsed['year'] -= 1970;
  1830. $year -= 1970;
  1831. }
  1832. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1833. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1834. } catch (Zend_Locale_Exception $e) {
  1835. require_once 'Zend/Date/Exception.php';
  1836. throw new Zend_Date_Exception($e->getMessage(), $date);
  1837. }
  1838. break;
  1839. case self::DATE_LONG:
  1840. try {
  1841. $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long'));
  1842. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1843. if (($calc == 'set') || ($calc == 'cmp')){
  1844. --$parsed['month'];
  1845. --$month;
  1846. --$parsed['day'];
  1847. --$day;
  1848. $parsed['year'] -= 1970;
  1849. $year -= 1970;
  1850. }
  1851. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1852. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1853. } catch (Zend_Locale_Exception $e) {
  1854. require_once 'Zend/Date/Exception.php';
  1855. throw new Zend_Date_Exception($e->getMessage(), $date);
  1856. }
  1857. break;
  1858. case self::DATE_MEDIUM:
  1859. try {
  1860. $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium'));
  1861. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1862. if (($calc == 'set') || ($calc == 'cmp')) {
  1863. --$parsed['month'];
  1864. --$month;
  1865. --$parsed['day'];
  1866. --$day;
  1867. $parsed['year'] -= 1970;
  1868. $year -= 1970;
  1869. }
  1870. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1871. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1872. } catch (Zend_Locale_Exception $e) {
  1873. require_once 'Zend/Date/Exception.php';
  1874. throw new Zend_Date_Exception($e->getMessage(), $date);
  1875. }
  1876. break;
  1877. case self::DATE_SHORT:
  1878. try {
  1879. $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short'));
  1880. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1881. $parsed['year'] = self::getFullYear($parsed['year']);
  1882. if (($calc == 'set') || ($calc == 'cmp')) {
  1883. --$parsed['month'];
  1884. --$month;
  1885. --$parsed['day'];
  1886. --$day;
  1887. $parsed['year'] -= 1970;
  1888. $year -= 1970;
  1889. }
  1890. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1891. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1892. } catch (Zend_Locale_Exception $e) {
  1893. require_once 'Zend/Date/Exception.php';
  1894. throw new Zend_Date_Exception($e->getMessage(), $date);
  1895. }
  1896. break;
  1897. case self::TIMES:
  1898. try {
  1899. if ($calc != 'set') {
  1900. $month = 1;
  1901. $day = 1;
  1902. $year = 1970;
  1903. }
  1904. $parsed = Zend_Locale_Format::getTime($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
  1905. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
  1906. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  1907. } catch (Zend_Locale_Exception $e) {
  1908. require_once 'Zend/Date/Exception.php';
  1909. throw new Zend_Date_Exception($e->getMessage(), $date);
  1910. }
  1911. break;
  1912. case self::TIME_FULL:
  1913. try {
  1914. $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'full'));
  1915. $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1916. if ($calc != 'set') {
  1917. $month = 1;
  1918. $day = 1;
  1919. $year = 1970;
  1920. }
  1921. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], 0, $month, $day, $year, true),
  1922. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  1923. } catch (Zend_Locale_Exception $e) {
  1924. require_once 'Zend/Date/Exception.php';
  1925. throw new Zend_Date_Exception($e->getMessage(), $date);
  1926. }
  1927. break;
  1928. case self::TIME_LONG:
  1929. try {
  1930. $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'long'));
  1931. $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1932. if ($calc != 'set') {
  1933. $month = 1;
  1934. $day = 1;
  1935. $year = 1970;
  1936. }
  1937. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
  1938. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  1939. } catch (Zend_Locale_Exception $e) {
  1940. require_once 'Zend/Date/Exception.php';
  1941. throw new Zend_Date_Exception($e->getMessage(), $date);
  1942. }
  1943. break;
  1944. case self::TIME_MEDIUM:
  1945. try {
  1946. $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'medium'));
  1947. $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1948. if ($calc != 'set') {
  1949. $month = 1;
  1950. $day = 1;
  1951. $year = 1970;
  1952. }
  1953. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
  1954. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  1955. } catch (Zend_Locale_Exception $e) {
  1956. require_once 'Zend/Date/Exception.php';
  1957. throw new Zend_Date_Exception($e->getMessage(), $date);
  1958. }
  1959. break;
  1960. case self::TIME_SHORT:
  1961. try {
  1962. $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'short'));
  1963. $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1964. if ($calc != 'set') {
  1965. $month = 1;
  1966. $day = 1;
  1967. $year = 1970;
  1968. }
  1969. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], 0, $month, $day, $year, true),
  1970. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  1971. } catch (Zend_Locale_Exception $e) {
  1972. require_once 'Zend/Date/Exception.php';
  1973. throw new Zend_Date_Exception($e->getMessage(), $date);
  1974. }
  1975. break;
  1976. // ATOM and RFC_3339 are identical
  1977. case self::ATOM:
  1978. case self::RFC_3339:
  1979. $result = preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\d{0,4}([+-]{1}\d{2}:\d{2}|Z)$/', $date, $match);
  1980. if (!$result) {
  1981. require_once 'Zend/Date/Exception.php';
  1982. throw new Zend_Date_Exception("invalid date ($date) operand, ATOM format expected", $date);
  1983. }
  1984. if (($calc == 'set') || ($calc == 'cmp')) {
  1985. --$match[2];
  1986. --$month;
  1987. --$match[3];
  1988. --$day;
  1989. $match[1] -= 1970;
  1990. $year -= 1970;
  1991. }
  1992. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $match[2], 1 + $match[3], 1970 + $match[1], true),
  1993. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  1994. break;
  1995. case self::COOKIE:
  1996. $result = preg_match("/^\w{6,9},\s(\d{2})-(\w{3})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})\s.{3,20}$/", $date, $match);
  1997. if (!$result) {
  1998. require_once 'Zend/Date/Exception.php';
  1999. throw new Zend_Date_Exception("invalid date ($date) operand, COOKIE format expected", $date);
  2000. }
  2001. $matchStartPos = iconv_strpos($match[0], ' ', 0, 'UTF-8') + 1;
  2002. $match[0] = iconv_substr($match[0],
  2003. $matchStartPos,
  2004. iconv_strlen($match[0], 'UTF-8') - $matchStartPos,
  2005. 'UTF-8');
  2006. $months = $this->_getDigitFromName($match[2]);
  2007. $match[3] = self::getFullYear($match[3]);
  2008. if (($calc == 'set') || ($calc == 'cmp')) {
  2009. --$months;
  2010. --$month;
  2011. --$match[1];
  2012. --$day;
  2013. $match[3] -= 1970;
  2014. $year -= 1970;
  2015. }
  2016. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
  2017. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2018. break;
  2019. case self::RFC_822:
  2020. case self::RFC_1036:
  2021. // new RFC 822 format, identical to RFC 1036 standard
  2022. $result = preg_match('/^\w{0,3},{0,1}\s{0,1}(\d{1,2})\s(\w{3})\s(\d{2})\s(\d{2}):(\d{2}):{0,1}(\d{0,2})\s([+-]{1}\d{4}|\w{1,20})$/', $date, $match);
  2023. if (!$result) {
  2024. require_once 'Zend/Date/Exception.php';
  2025. throw new Zend_Date_Exception("invalid date ($date) operand, RFC 822 date format expected", $date);
  2026. }
  2027. $months = $this->_getDigitFromName($match[2]);
  2028. $match[3] = self::getFullYear($match[3]);
  2029. if (($calc == 'set') || ($calc == 'cmp')) {
  2030. --$months;
  2031. --$month;
  2032. --$match[1];
  2033. --$day;
  2034. $match[3] -= 1970;
  2035. $year -= 1970;
  2036. }
  2037. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], false),
  2038. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false);
  2039. break;
  2040. case self::RFC_850:
  2041. $result = preg_match('/^\w{6,9},\s(\d{2})-(\w{3})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})\s.{3,21}$/', $date, $match);
  2042. if (!$result) {
  2043. require_once 'Zend/Date/Exception.php';
  2044. throw new Zend_Date_Exception("invalid date ($date) operand, RFC 850 date format expected", $date);
  2045. }
  2046. $months = $this->_getDigitFromName($match[2]);
  2047. $match[3] = self::getFullYear($match[3]);
  2048. if (($calc == 'set') || ($calc == 'cmp')) {
  2049. --$months;
  2050. --$month;
  2051. --$match[1];
  2052. --$day;
  2053. $match[3] -= 1970;
  2054. $year -= 1970;
  2055. }
  2056. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
  2057. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2058. break;
  2059. case self::RFC_1123:
  2060. $result = preg_match('/^\w{0,3},{0,1}\s{0,1}(\d{1,2})\s(\w{3})\s(\d{2,4})\s(\d{2}):(\d{2}):{0,1}(\d{0,2})\s([+-]{1}\d{4}|\w{1,20})$/', $date, $match);
  2061. if (!$result) {
  2062. require_once 'Zend/Date/Exception.php';
  2063. throw new Zend_Date_Exception("invalid date ($date) operand, RFC 1123 date format expected", $date);
  2064. }
  2065. $months = $this->_getDigitFromName($match[2]);
  2066. if (($calc == 'set') || ($calc == 'cmp')) {
  2067. --$months;
  2068. --$month;
  2069. --$match[1];
  2070. --$day;
  2071. $match[3] -= 1970;
  2072. $year -= 1970;
  2073. }
  2074. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
  2075. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2076. break;
  2077. case self::RSS:
  2078. $result = preg_match('/^\w{3},\s(\d{2})\s(\w{3})\s(\d{2,4})\s(\d{1,2}):(\d{2}):(\d{2})\s.{1,21}$/', $date, $match);
  2079. if (!$result) {
  2080. require_once 'Zend/Date/Exception.php';
  2081. throw new Zend_Date_Exception("invalid date ($date) operand, RSS date format expected", $date);
  2082. }
  2083. $months = $this->_getDigitFromName($match[2]);
  2084. $match[3] = self::getFullYear($match[3]);
  2085. if (($calc == 'set') || ($calc == 'cmp')) {
  2086. --$months;
  2087. --$month;
  2088. --$match[1];
  2089. --$day;
  2090. $match[3] -= 1970;
  2091. $year -= 1970;
  2092. }
  2093. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
  2094. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2095. break;
  2096. case self::W3C:
  2097. $result = preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})[+-]{1}\d{2}:\d{2}$/', $date, $match);
  2098. if (!$result) {
  2099. require_once 'Zend/Date/Exception.php';
  2100. throw new Zend_Date_Exception("invalid date ($date) operand, W3C date format expected", $date);
  2101. }
  2102. if (($calc == 'set') || ($calc == 'cmp')) {
  2103. --$match[2];
  2104. --$month;
  2105. --$match[3];
  2106. --$day;
  2107. $match[1] -= 1970;
  2108. $year -= 1970;
  2109. }
  2110. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $match[2], 1 + $match[3], 1970 + $match[1], true),
  2111. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2112. break;
  2113. default:
  2114. if (!is_numeric($date) || !empty($part)) {
  2115. try {
  2116. if (self::$_options['format_type'] == 'php') {
  2117. $part = Zend_Locale_Format::convertPhpToIsoFormat($part);
  2118. }
  2119. if (empty($part)) {
  2120. $part = Zend_Locale_Format::getDateFormat($locale) . " ";
  2121. $part .= Zend_Locale_Format::getTimeFormat($locale);
  2122. }
  2123. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $part, 'locale' => $locale, 'fix_date' => true, 'format_type' => 'iso'));
  2124. if ((strpos(strtoupper($part), 'YY') !== false) and (strpos(strtoupper($part), 'YYYY') === false)) {
  2125. $parsed['year'] = self::getFullYear($parsed['year']);
  2126. }
  2127. if (($calc == 'set') || ($calc == 'cmp')) {
  2128. if (isset($parsed['month'])) {
  2129. --$parsed['month'];
  2130. } else {
  2131. $parsed['month'] = 0;
  2132. }
  2133. if (isset($parsed['day'])) {
  2134. --$parsed['day'];
  2135. } else {
  2136. $parsed['day'] = 0;
  2137. }
  2138. if (isset($parsed['year'])) {
  2139. $parsed['year'] -= 1970;
  2140. } else {
  2141. $parsed['year'] = 0;
  2142. }
  2143. }
  2144. return $this->_assign($calc, $this->mktime(
  2145. isset($parsed['hour']) ? $parsed['hour'] : 0,
  2146. isset($parsed['minute']) ? $parsed['minute'] : 0,
  2147. isset($parsed['second']) ? $parsed['second'] : 0,
  2148. 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'],
  2149. false), $this->getUnixTimestamp(), false);
  2150. } catch (Zend_Locale_Exception $e) {
  2151. if (!is_numeric($date)) {
  2152. require_once 'Zend/Date/Exception.php';
  2153. throw new Zend_Date_Exception($e->getMessage(), $date);
  2154. }
  2155. }
  2156. }
  2157. return $this->_assign($calc, $date, $this->getUnixTimestamp(), false);
  2158. break;
  2159. }
  2160. }
  2161. /**
  2162. * Returns true when both date objects or date parts are equal.
  2163. * For example:
  2164. * 15.May.2000 <-> 15.June.2000 Equals only for Day or Year... all other will return false
  2165. *
  2166. * @param string|integer|array|Zend_Date $date Date or datepart to equal with
  2167. * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is used
  2168. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2169. * @return boolean
  2170. * @throws Zend_Date_Exception
  2171. */
  2172. public function equals($date, $part = null, $locale = null)
  2173. {
  2174. $result = $this->compare($date, $part, $locale);
  2175. if ($result == 0) {
  2176. return true;
  2177. }
  2178. return false;
  2179. }
  2180. /**
  2181. * Returns if the given date or datepart is earlier
  2182. * For example:
  2183. * 15.May.2000 <-> 13.June.1999 will return true for day, year and date, but not for month
  2184. *
  2185. * @param string|integer|array|Zend_Date $date Date or datepart to compare with
  2186. * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is used
  2187. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2188. * @return boolean
  2189. * @throws Zend_Date_Exception
  2190. */
  2191. public function isEarlier($date, $part = null, $locale = null)
  2192. {
  2193. $result = $this->compare($date, $part, $locale);
  2194. if ($result == -1) {
  2195. return true;
  2196. }
  2197. return false;
  2198. }
  2199. /**
  2200. * Returns if the given date or datepart is later
  2201. * For example:
  2202. * 15.May.2000 <-> 13.June.1999 will return true for month but false for day, year and date
  2203. * Returns if the given date is later
  2204. *
  2205. * @param string|integer|array|Zend_Date $date Date or datepart to compare with
  2206. * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is used
  2207. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2208. * @return boolean
  2209. * @throws Zend_Date_Exception
  2210. */
  2211. public function isLater($date, $part = null, $locale = null)
  2212. {
  2213. $result = $this->compare($date, $part, $locale);
  2214. if ($result == 1) {
  2215. return true;
  2216. }
  2217. return false;
  2218. }
  2219. /**
  2220. * Returns only the time of the date as new Zend_Date object
  2221. * For example:
  2222. * 15.May.2000 10:11:23 will return a dateobject equal to 01.Jan.1970 10:11:23
  2223. *
  2224. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2225. * @return Zend_Date
  2226. */
  2227. public function getTime($locale = null)
  2228. {
  2229. return $this->copyPart(self::TIME_MEDIUM, $locale);
  2230. }
  2231. /**
  2232. * Returns the calculated time
  2233. *
  2234. * @param string $calc Calculation to make
  2235. * @param string|integer|array|Zend_Date $time Time to calculate with, if null the actual time is taken
  2236. * @param string $format Timeformat for parsing input
  2237. * @param string|Zend_Locale $locale Locale for parsing input
  2238. * @return integer|Zend_Date new time
  2239. * @throws Zend_Date_Exception
  2240. */
  2241. private function _time($calc, $time, $format, $locale)
  2242. {
  2243. if ($time === null) {
  2244. require_once 'Zend/Date/Exception.php';
  2245. throw new Zend_Date_Exception('parameter $time must be set, null is not allowed');
  2246. }
  2247. if ($time instanceof Zend_Date) {
  2248. // extract time from object
  2249. $time = $time->get('HH:mm:ss');
  2250. } else {
  2251. if (is_array($time)) {
  2252. if ((isset($time['hour']) === true) or (isset($time['minute']) === true) or
  2253. (isset($time['second']) === true)) {
  2254. $parsed = $time;
  2255. } else {
  2256. require_once 'Zend/Date/Exception.php';
  2257. throw new Zend_Date_Exception("no hour, minute or second given in array");
  2258. }
  2259. } else {
  2260. if (self::$_options['format_type'] == 'php') {
  2261. $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
  2262. }
  2263. try {
  2264. if ($locale === null) {
  2265. $locale = $this->getLocale();
  2266. }
  2267. $parsed = Zend_Locale_Format::getTime($time, array('date_format' => $format, 'locale' => $locale, 'format_type' => 'iso'));
  2268. } catch (Zend_Locale_Exception $e) {
  2269. require_once 'Zend/Date/Exception.php';
  2270. throw new Zend_Date_Exception($e->getMessage());
  2271. }
  2272. }
  2273. $time = str_pad($parsed['hour'], 2, '0', STR_PAD_LEFT) . ":";
  2274. $time .= str_pad($parsed['minute'], 2, '0', STR_PAD_LEFT) . ":";
  2275. $time .= str_pad($parsed['second'], 2, '0', STR_PAD_LEFT);
  2276. }
  2277. $return = $this->_calcdetail($calc, $time, self::TIMES, 'de');
  2278. if ($calc != 'cmp') {
  2279. return $this;
  2280. }
  2281. return $return;
  2282. }
  2283. /**
  2284. * Sets a new time for the date object. Format defines how to parse the time string.
  2285. * Also a complete date can be given, but only the time is used for setting.
  2286. * For example: dd.MMMM.yyTHH:mm' and 'ss sec'-> 10.May.07T25:11 and 44 sec => 1h11min44sec + 1 day
  2287. * Returned is the new date object and the existing date is left as it was before
  2288. *
  2289. * @param string|integer|array|Zend_Date $time Time to set
  2290. * @param string $format OPTIONAL Timeformat for parsing input
  2291. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2292. * @return Zend_Date new time
  2293. * @throws Zend_Date_Exception
  2294. */
  2295. public function setTime($time, $format = null, $locale = null)
  2296. {
  2297. return $this->_time('set', $time, $format, $locale);
  2298. }
  2299. /**
  2300. * Adds a time to the existing date. Format defines how to parse the time string.
  2301. * If only parts are given the other parts are set to 0.
  2302. * If no format is given, the standardformat of this locale is used.
  2303. * For example: HH:mm:ss -> 10 -> +10 hours
  2304. *
  2305. * @param string|integer|array|Zend_Date $time Time to add
  2306. * @param string $format OPTIONAL Timeformat for parsing input
  2307. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2308. * @return Zend_Date new time
  2309. * @throws Zend_Date_Exception
  2310. */
  2311. public function addTime($time, $format = null, $locale = null)
  2312. {
  2313. return $this->_time('add', $time, $format, $locale);
  2314. }
  2315. /**
  2316. * Subtracts a time from the existing date. Format defines how to parse the time string.
  2317. * If only parts are given the other parts are set to 0.
  2318. * If no format is given, the standardformat of this locale is used.
  2319. * For example: HH:mm:ss -> 10 -> -10 hours
  2320. *
  2321. * @param string|integer|array|Zend_Date $time Time to sub
  2322. * @param string $format OPTIONAL Timeformat for parsing input
  2323. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2324. * @return Zend_Date new time
  2325. * @throws Zend_Date_Exception
  2326. */
  2327. public function subTime($time, $format = null, $locale = null)
  2328. {
  2329. return $this->_time('sub', $time, $format, $locale);
  2330. }
  2331. /**
  2332. * Compares the time from the existing date. Format defines how to parse the time string.
  2333. * If only parts are given the other parts are set to default.
  2334. * If no format us given, the standardformat of this locale is used.
  2335. * For example: HH:mm:ss -> 10 -> 10 hours
  2336. *
  2337. * @param string|integer|array|Zend_Date $time Time to compare
  2338. * @param string $format OPTIONAL Timeformat for parsing input
  2339. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2340. * @return integer 0 = equal, 1 = later, -1 = earlier
  2341. * @throws Zend_Date_Exception
  2342. */
  2343. public function compareTime($time, $format = null, $locale = null)
  2344. {
  2345. return $this->_time('cmp', $time, $format, $locale);
  2346. }
  2347. /**
  2348. * Returns a clone of $this, with the time part set to 00:00:00.
  2349. *
  2350. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2351. * @return Zend_Date
  2352. */
  2353. public function getDate($locale = null)
  2354. {
  2355. $date = $this->copyPart(self::DATE_MEDIUM, $locale);
  2356. $date->addTimestamp($this->getGmtOffset());
  2357. return $date;
  2358. }
  2359. /**
  2360. * Returns the calculated date
  2361. *
  2362. * @param string $calc Calculation to make
  2363. * @param string|integer|array|Zend_Date $date Date to calculate with, if null the actual date is taken
  2364. * @param string $format Date format for parsing
  2365. * @param string|Zend_Locale $locale Locale for parsing input
  2366. * @return integer|Zend_Date new date
  2367. * @throws Zend_Date_Exception
  2368. */
  2369. private function _date($calc, $date, $format, $locale)
  2370. {
  2371. if ($date === null) {
  2372. require_once 'Zend/Date/Exception.php';
  2373. throw new Zend_Date_Exception('parameter $date must be set, null is not allowed');
  2374. }
  2375. if ($date instanceof Zend_Date) {
  2376. // extract date from object
  2377. $date = $date->get('d.M.Y');
  2378. } else {
  2379. if (is_array($date)) {
  2380. if ((isset($time['year']) === true) or (isset($time['month']) === true) or
  2381. (isset($time['day']) === true)) {
  2382. $parsed = $time;
  2383. } else {
  2384. require_once 'Zend/Date/Exception.php';
  2385. throw new Zend_Date_Exception("no day,month or year given in array");
  2386. }
  2387. } else {
  2388. if (self::$_options['format_type'] == 'php') {
  2389. $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
  2390. }
  2391. try {
  2392. if ($locale === null) {
  2393. $locale = $this->getLocale();
  2394. }
  2395. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'locale' => $locale, 'format_type' => 'iso'));
  2396. if ((strpos(strtoupper($format), 'YY') !== false) and (strpos(strtoupper($format), 'YYYY') === false)) {
  2397. $parsed['year'] = self::getFullYear($parsed['year']);
  2398. }
  2399. } catch (Zend_Locale_Exception $e) {
  2400. require_once 'Zend/Date/Exception.php';
  2401. throw new Zend_Date_Exception($e->getMessage());
  2402. }
  2403. }
  2404. $date = $parsed['day'] . "." . $parsed['month'] . "." . $parsed['year'];
  2405. }
  2406. $return = $this->_calcdetail($calc, $date, self::DATE_MEDIUM, 'de');
  2407. if ($calc != 'cmp') {
  2408. return $this;
  2409. }
  2410. return $return;
  2411. }
  2412. /**
  2413. * Sets a new date for the date object. Format defines how to parse the date string.
  2414. * Also a complete date with time can be given, but only the date is used for setting.
  2415. * For example: MMMM.yy HH:mm-> May.07 22:11 => 01.May.07 00:00
  2416. * Returned is the new date object and the existing time is left as it was before
  2417. *
  2418. * @param string|integer|array|Zend_Date $date Date to set
  2419. * @param string $format OPTIONAL Date format for parsing
  2420. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2421. * @return integer|Zend_Date new date
  2422. * @throws Zend_Date_Exception
  2423. */
  2424. public function setDate($date, $format = null, $locale = null)
  2425. {
  2426. return $this->_date('set', $date, $format, $locale);
  2427. }
  2428. /**
  2429. * Adds a date to the existing date object. Format defines how to parse the date string.
  2430. * If only parts are given the other parts are set to 0.
  2431. * If no format is given, the standardformat of this locale is used.
  2432. * For example: MM.dd.YYYY -> 10 -> +10 months
  2433. *
  2434. * @param string|integer|array|Zend_Date $date Date to add
  2435. * @param string $format OPTIONAL Date format for parsing input
  2436. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2437. * @return Zend_Date new date
  2438. * @throws Zend_Date_Exception
  2439. */
  2440. public function addDate($date, $format = null, $locale = null)
  2441. {
  2442. return $this->_date('add', $date, $format, $locale);
  2443. }
  2444. /**
  2445. * Subtracts a date from the existing date object. Format defines how to parse the date string.
  2446. * If only parts are given the other parts are set to 0.
  2447. * If no format is given, the standardformat of this locale is used.
  2448. * For example: MM.dd.YYYY -> 10 -> -10 months
  2449. * Be aware: Subtracting 2 months is not equal to Adding -2 months !!!
  2450. *
  2451. * @param string|integer|array|Zend_Date $date Date to sub
  2452. * @param string $format OPTIONAL Date format for parsing input
  2453. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2454. * @return Zend_Date new date
  2455. * @throws Zend_Date_Exception
  2456. */
  2457. public function subDate($date, $format = null, $locale = null)
  2458. {
  2459. return $this->_date('sub', $date, $format, $locale);
  2460. }
  2461. /**
  2462. * Compares the date from the existing date object, ignoring the time.
  2463. * Format defines how to parse the date string.
  2464. * If only parts are given the other parts are set to 0.
  2465. * If no format is given, the standardformat of this locale is used.
  2466. * For example: 10.01.2000 => 10.02.1999 -> false
  2467. *
  2468. * @param string|integer|array|Zend_Date $date Date to compare
  2469. * @param string $format OPTIONAL Date format for parsing input
  2470. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2471. * @return Zend_Date new date
  2472. * @throws Zend_Date_Exception
  2473. */
  2474. public function compareDate($date, $format = null, $locale = null)
  2475. {
  2476. return $this->_date('cmp', $date, $format, $locale);
  2477. }
  2478. /**
  2479. * Returns the full ISO 8601 date from the date object.
  2480. * Always the complete ISO 8601 specifiction is used. If an other ISO date is needed
  2481. * (ISO 8601 defines several formats) use toString() instead.
  2482. * This function does not return the ISO date as object. Use copy() instead.
  2483. *
  2484. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2485. * @return string
  2486. */
  2487. public function getIso($locale = null)
  2488. {
  2489. return $this->get(self::ISO_8601, $locale);
  2490. }
  2491. /**
  2492. * Sets a new date for the date object. Not given parts are set to default.
  2493. * Only supported ISO 8601 formats are accepted.
  2494. * For example: 050901 -> 01.Sept.2005 00:00:00, 20050201T10:00:30 -> 01.Feb.2005 10h00m30s
  2495. * Returned is the new date object
  2496. *
  2497. * @param string|integer|Zend_Date $date ISO Date to set
  2498. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2499. * @return integer|Zend_Date new date
  2500. * @throws Zend_Date_Exception
  2501. */
  2502. public function setIso($date, $locale = null)
  2503. {
  2504. return $this->_calcvalue('set', $date, 'iso', self::ISO_8601, $locale);
  2505. }
  2506. /**
  2507. * Adds a ISO date to the date object. Not given parts are set to default.
  2508. * Only supported ISO 8601 formats are accepted.
  2509. * For example: 050901 -> + 01.Sept.2005 00:00:00, 10:00:00 -> +10h
  2510. * Returned is the new date object
  2511. *
  2512. * @param string|integer|Zend_Date $date ISO Date to add
  2513. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2514. * @return integer|Zend_Date new date
  2515. * @throws Zend_Date_Exception
  2516. */
  2517. public function addIso($date, $locale = null)
  2518. {
  2519. return $this->_calcvalue('add', $date, 'iso', self::ISO_8601, $locale);
  2520. }
  2521. /**
  2522. * Subtracts a ISO date from the date object. Not given parts are set to default.
  2523. * Only supported ISO 8601 formats are accepted.
  2524. * For example: 050901 -> - 01.Sept.2005 00:00:00, 10:00:00 -> -10h
  2525. * Returned is the new date object
  2526. *
  2527. * @param string|integer|Zend_Date $date ISO Date to sub
  2528. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2529. * @return integer|Zend_Date new date
  2530. * @throws Zend_Date_Exception
  2531. */
  2532. public function subIso($date, $locale = null)
  2533. {
  2534. return $this->_calcvalue('sub', $date, 'iso', self::ISO_8601, $locale);
  2535. }
  2536. /**
  2537. * Compares a ISO date with the date object. Not given parts are set to default.
  2538. * Only supported ISO 8601 formats are accepted.
  2539. * For example: 050901 -> - 01.Sept.2005 00:00:00, 10:00:00 -> -10h
  2540. * Returns if equal, earlier or later
  2541. *
  2542. * @param string|integer|Zend_Date $date ISO Date to sub
  2543. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2544. * @return integer 0 = equal, 1 = later, -1 = earlier
  2545. * @throws Zend_Date_Exception
  2546. */
  2547. public function compareIso($date, $locale = null)
  2548. {
  2549. return $this->_calcvalue('cmp', $date, 'iso', self::ISO_8601, $locale);
  2550. }
  2551. /**
  2552. * Returns a RFC 822 compilant datestring from the date object.
  2553. * This function does not return the RFC date as object. Use copy() instead.
  2554. *
  2555. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2556. * @return string
  2557. */
  2558. public function getArpa($locale = null)
  2559. {
  2560. return $this->get(self::RFC_822, $locale);
  2561. }
  2562. /**
  2563. * Sets a RFC 822 date as new date for the date object.
  2564. * Only RFC 822 compilant date strings are accepted.
  2565. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2566. * Returned is the new date object
  2567. *
  2568. * @param string|integer|Zend_Date $date RFC 822 to set
  2569. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2570. * @return integer|Zend_Date new date
  2571. * @throws Zend_Date_Exception
  2572. */
  2573. public function setArpa($date, $locale = null)
  2574. {
  2575. return $this->_calcvalue('set', $date, 'arpa', self::RFC_822, $locale);
  2576. }
  2577. /**
  2578. * Adds a RFC 822 date to the date object.
  2579. * ARPA messages are used in emails or HTTP Headers.
  2580. * Only RFC 822 compilant date strings are accepted.
  2581. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2582. * Returned is the new date object
  2583. *
  2584. * @param string|integer|Zend_Date $date RFC 822 Date to add
  2585. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2586. * @return integer|Zend_Date new date
  2587. * @throws Zend_Date_Exception
  2588. */
  2589. public function addArpa($date, $locale = null)
  2590. {
  2591. return $this->_calcvalue('add', $date, 'arpa', self::RFC_822, $locale);
  2592. }
  2593. /**
  2594. * Subtracts a RFC 822 date from the date object.
  2595. * ARPA messages are used in emails or HTTP Headers.
  2596. * Only RFC 822 compilant date strings are accepted.
  2597. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2598. * Returned is the new date object
  2599. *
  2600. * @param string|integer|Zend_Date $date RFC 822 Date to sub
  2601. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2602. * @return integer|Zend_Date new date
  2603. * @throws Zend_Date_Exception
  2604. */
  2605. public function subArpa($date, $locale = null)
  2606. {
  2607. return $this->_calcvalue('sub', $date, 'arpa', self::RFC_822, $locale);
  2608. }
  2609. /**
  2610. * Compares a RFC 822 compilant date with the date object.
  2611. * ARPA messages are used in emails or HTTP Headers.
  2612. * Only RFC 822 compilant date strings are accepted.
  2613. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2614. * Returns if equal, earlier or later
  2615. *
  2616. * @param string|integer|Zend_Date $date RFC 822 Date to sub
  2617. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2618. * @return integer 0 = equal, 1 = later, -1 = earlier
  2619. * @throws Zend_Date_Exception
  2620. */
  2621. public function compareArpa($date, $locale = null)
  2622. {
  2623. return $this->_calcvalue('cmp', $date, 'arpa', self::RFC_822, $locale);
  2624. }
  2625. /**
  2626. * Check if location is supported
  2627. *
  2628. * @param $location array - locations array
  2629. * @return $horizon float
  2630. */
  2631. private function _checkLocation($location)
  2632. {
  2633. if (!isset($location['longitude']) or !isset($location['latitude'])) {
  2634. require_once 'Zend/Date/Exception.php';
  2635. throw new Zend_Date_Exception('Location must include \'longitude\' and \'latitude\'', $location);
  2636. }
  2637. if (($location['longitude'] > 180) or ($location['longitude'] < -180)) {
  2638. require_once 'Zend/Date/Exception.php';
  2639. throw new Zend_Date_Exception('Longitude must be between -180 and 180', $location);
  2640. }
  2641. if (($location['latitude'] > 90) or ($location['latitude'] < -90)) {
  2642. require_once 'Zend/Date/Exception.php';
  2643. throw new Zend_Date_Exception('Latitude must be between -90 and 90', $location);
  2644. }
  2645. if (!isset($location['horizon'])){
  2646. $location['horizon'] = 'effective';
  2647. }
  2648. switch ($location['horizon']) {
  2649. case 'civil' :
  2650. return -0.104528;
  2651. break;
  2652. case 'nautic' :
  2653. return -0.207912;
  2654. break;
  2655. case 'astronomic' :
  2656. return -0.309017;
  2657. break;
  2658. default :
  2659. return -0.0145439;
  2660. break;
  2661. }
  2662. }
  2663. /**
  2664. * Returns the time of sunrise for this date and a given location as new date object
  2665. * For a list of cities and correct locations use the class Zend_Date_Cities
  2666. *
  2667. * @param $location array - location of sunrise
  2668. * ['horizon'] -> civil, nautic, astronomical, effective (default)
  2669. * ['longitude'] -> longitude of location
  2670. * ['latitude'] -> latitude of location
  2671. * @return Zend_Date
  2672. * @throws Zend_Date_Exception
  2673. */
  2674. public function getSunrise($location)
  2675. {
  2676. $horizon = $this->_checkLocation($location);
  2677. $result = clone $this;
  2678. $result->set($this->calcSun($location, $horizon, true), self::TIMESTAMP);
  2679. return $result;
  2680. }
  2681. /**
  2682. * Returns the time of sunset for this date and a given location as new date object
  2683. * For a list of cities and correct locations use the class Zend_Date_Cities
  2684. *
  2685. * @param $location array - location of sunset
  2686. * ['horizon'] -> civil, nautic, astronomical, effective (default)
  2687. * ['longitude'] -> longitude of location
  2688. * ['latitude'] -> latitude of location
  2689. * @return Zend_Date
  2690. * @throws Zend_Date_Exception
  2691. */
  2692. public function getSunset($location)
  2693. {
  2694. $horizon = $this->_checkLocation($location);
  2695. $result = clone $this;
  2696. $result->set($this->calcSun($location, $horizon, false), self::TIMESTAMP);
  2697. return $result;
  2698. }
  2699. /**
  2700. * Returns an array with the sunset and sunrise dates for all horizon types
  2701. * For a list of cities and correct locations use the class Zend_Date_Cities
  2702. *
  2703. * @param $location array - location of suninfo
  2704. * ['horizon'] -> civil, nautic, astronomical, effective (default)
  2705. * ['longitude'] -> longitude of location
  2706. * ['latitude'] -> latitude of location
  2707. * @return array - [sunset|sunrise][effective|civil|nautic|astronomic]
  2708. * @throws Zend_Date_Exception
  2709. */
  2710. public function getSunInfo($location)
  2711. {
  2712. $suninfo = array();
  2713. for ($i = 0; $i < 4; ++$i) {
  2714. switch ($i) {
  2715. case 0 :
  2716. $location['horizon'] = 'effective';
  2717. break;
  2718. case 1 :
  2719. $location['horizon'] = 'civil';
  2720. break;
  2721. case 2 :
  2722. $location['horizon'] = 'nautic';
  2723. break;
  2724. case 3 :
  2725. $location['horizon'] = 'astronomic';
  2726. break;
  2727. }
  2728. $horizon = $this->_checkLocation($location);
  2729. $result = clone $this;
  2730. $result->set($this->calcSun($location, $horizon, true), self::TIMESTAMP);
  2731. $suninfo['sunrise'][$location['horizon']] = $result;
  2732. $result = clone $this;
  2733. $result->set($this->calcSun($location, $horizon, false), self::TIMESTAMP);
  2734. $suninfo['sunset'][$location['horizon']] = $result;
  2735. }
  2736. return $suninfo;
  2737. }
  2738. /**
  2739. * Check a given year for leap year.
  2740. *
  2741. * @param integer|array|Zend_Date $year Year to check
  2742. * @return boolean
  2743. */
  2744. public static function checkLeapYear($year)
  2745. {
  2746. if ($year instanceof Zend_Date) {
  2747. $year = (int) $year->get(self::YEAR);
  2748. }
  2749. if (is_array($year)) {
  2750. if (isset($year['year']) === true) {
  2751. $year = $year['year'];
  2752. } else {
  2753. require_once 'Zend/Date/Exception.php';
  2754. throw new Zend_Date_Exception("no year given in array");
  2755. }
  2756. }
  2757. if (!is_numeric($year)) {
  2758. require_once 'Zend/Date/Exception.php';
  2759. throw new Zend_Date_Exception("year ($year) has to be integer for checkLeapYear()", $year);
  2760. }
  2761. return (bool) parent::isYearLeapYear($year);
  2762. }
  2763. /**
  2764. * Returns true, if the year is a leap year.
  2765. *
  2766. * @return boolean
  2767. */
  2768. public function isLeapYear()
  2769. {
  2770. return self::checkLeapYear($this);
  2771. }
  2772. /**
  2773. * Returns if the set date is todays date
  2774. *
  2775. * @return boolean
  2776. */
  2777. public function isToday()
  2778. {
  2779. $today = $this->date('Ymd', $this->_getTime());
  2780. $day = $this->date('Ymd', $this->getUnixTimestamp());
  2781. return ($today == $day);
  2782. }
  2783. /**
  2784. * Returns if the set date is yesterdays date
  2785. *
  2786. * @return boolean
  2787. */
  2788. public function isYesterday()
  2789. {
  2790. list($year, $month, $day) = explode('-', $this->date('Y-m-d', $this->_getTime()));
  2791. // adjusts for leap days and DST changes that are timezone specific
  2792. $yesterday = $this->date('Ymd', $this->mktime(0, 0, 0, $month, $day -1, $year));
  2793. $day = $this->date('Ymd', $this->getUnixTimestamp());
  2794. return $day == $yesterday;
  2795. }
  2796. /**
  2797. * Returns if the set date is tomorrows date
  2798. *
  2799. * @return boolean
  2800. */
  2801. public function isTomorrow()
  2802. {
  2803. list($year, $month, $day) = explode('-', $this->date('Y-m-d', $this->_getTime()));
  2804. // adjusts for leap days and DST changes that are timezone specific
  2805. $tomorrow = $this->date('Ymd', $this->mktime(0, 0, 0, $month, $day +1, $year));
  2806. $day = $this->date('Ymd', $this->getUnixTimestamp());
  2807. return $day == $tomorrow;
  2808. }
  2809. /**
  2810. * Returns the actual date as new date object
  2811. *
  2812. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2813. * @return Zend_Date
  2814. */
  2815. public static function now($locale = null)
  2816. {
  2817. return new Zend_Date(time(), self::TIMESTAMP, $locale);
  2818. }
  2819. /**
  2820. * Calculate date details
  2821. *
  2822. * @param string $calc Calculation to make
  2823. * @param string|integer|array|Zend_Date $date Date or Part to calculate
  2824. * @param string $part Datepart for Calculation
  2825. * @param string|Zend_Locale $locale Locale for parsing input
  2826. * @return integer|string new date
  2827. * @throws Zend_Date_Exception
  2828. */
  2829. private function _calcdetail($calc, $date, $type, $locale)
  2830. {
  2831. switch($calc) {
  2832. case 'set' :
  2833. return $this->set($date, $type, $locale);
  2834. break;
  2835. case 'add' :
  2836. return $this->add($date, $type, $locale);
  2837. break;
  2838. case 'sub' :
  2839. return $this->sub($date, $type, $locale);
  2840. break;
  2841. }
  2842. return $this->compare($date, $type, $locale);
  2843. }
  2844. /**
  2845. * Internal calculation, returns the requested date type
  2846. *
  2847. * @param string $calc Calculation to make
  2848. * @param string|integer|Zend_Date $value Datevalue to calculate with, if null the actual value is taken
  2849. * @param string|Zend_Locale $locale Locale for parsing input
  2850. * @return integer|Zend_Date new date
  2851. * @throws Zend_Date_Exception
  2852. */
  2853. private function _calcvalue($calc, $value, $type, $parameter, $locale)
  2854. {
  2855. if ($value === null) {
  2856. require_once 'Zend/Date/Exception.php';
  2857. throw new Zend_Date_Exception("parameter $type must be set, null is not allowed");
  2858. }
  2859. if ($locale === null) {
  2860. $locale = $this->getLocale();
  2861. }
  2862. if ($value instanceof Zend_Date) {
  2863. // extract value from object
  2864. $value = $value->get($parameter, $locale);
  2865. } else if (!is_array($value) && !is_numeric($value) && ($type != 'iso') && ($type != 'arpa')) {
  2866. require_once 'Zend/Date/Exception.php';
  2867. throw new Zend_Date_Exception("invalid $type ($value) operand", $value);
  2868. }
  2869. $return = $this->_calcdetail($calc, $value, $parameter, $locale);
  2870. if ($calc != 'cmp') {
  2871. return $this;
  2872. }
  2873. return $return;
  2874. }
  2875. /**
  2876. * Returns only the year from the date object as new object.
  2877. * For example: 10.May.2000 10:30:00 -> 01.Jan.2000 00:00:00
  2878. *
  2879. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2880. * @return Zend_Date
  2881. */
  2882. public function getYear($locale = null)
  2883. {
  2884. return $this->copyPart(self::YEAR, $locale);
  2885. }
  2886. /**
  2887. * Sets a new year
  2888. * If the year is between 0 and 69, 2000 will be set (2000-2069)
  2889. * If the year if between 70 and 99, 1999 will be set (1970-1999)
  2890. * 3 or 4 digit years are set as expected. If you need to set year 0-99
  2891. * use set() instead.
  2892. * Returned is the new date object
  2893. *
  2894. * @param string|integer|array|Zend_Date $date Year to set
  2895. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2896. * @return Zend_Date new date
  2897. * @throws Zend_Date_Exception
  2898. */
  2899. public function setYear($year, $locale = null)
  2900. {
  2901. return $this->_calcvalue('set', $year, 'year', self::YEAR, $locale);
  2902. }
  2903. /**
  2904. * Adds the year to the existing date object
  2905. * If the year is between 0 and 69, 2000 will be added (2000-2069)
  2906. * If the year if between 70 and 99, 1999 will be added (1970-1999)
  2907. * 3 or 4 digit years are added as expected. If you need to add years from 0-99
  2908. * use add() instead.
  2909. * Returned is the new date object
  2910. *
  2911. * @param string|integer|array|Zend_Date $date Year to add
  2912. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2913. * @return Zend_Date new date
  2914. * @throws Zend_Date_Exception
  2915. */
  2916. public function addYear($year, $locale = null)
  2917. {
  2918. return $this->_calcvalue('add', $year, 'year', self::YEAR, $locale);
  2919. }
  2920. /**
  2921. * Subs the year from the existing date object
  2922. * If the year is between 0 and 69, 2000 will be subtracted (2000-2069)
  2923. * If the year if between 70 and 99, 1999 will be subtracted (1970-1999)
  2924. * 3 or 4 digit years are subtracted as expected. If you need to subtract years from 0-99
  2925. * use sub() instead.
  2926. * Returned is the new date object
  2927. *
  2928. * @param string|integer|array|Zend_Date $date Year to sub
  2929. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2930. * @return Zend_Date new date
  2931. * @throws Zend_Date_Exception
  2932. */
  2933. public function subYear($year, $locale = null)
  2934. {
  2935. return $this->_calcvalue('sub', $year, 'year', self::YEAR, $locale);
  2936. }
  2937. /**
  2938. * Compares the year with the existing date object, ignoring other date parts.
  2939. * For example: 10.03.2000 -> 15.02.2000 -> true
  2940. * Returns if equal, earlier or later
  2941. *
  2942. * @param string|integer|array|Zend_Date $year Year to compare
  2943. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2944. * @return integer 0 = equal, 1 = later, -1 = earlier
  2945. * @throws Zend_Date_Exception
  2946. */
  2947. public function compareYear($year, $locale = null)
  2948. {
  2949. return $this->_calcvalue('cmp', $year, 'year', self::YEAR, $locale);
  2950. }
  2951. /**
  2952. * Returns only the month from the date object as new object.
  2953. * For example: 10.May.2000 10:30:00 -> 01.May.1970 00:00:00
  2954. *
  2955. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2956. * @return Zend_Date
  2957. */
  2958. public function getMonth($locale = null)
  2959. {
  2960. return $this->copyPart(self::MONTH, $locale);
  2961. }
  2962. /**
  2963. * Returns the calculated month
  2964. *
  2965. * @param string $calc Calculation to make
  2966. * @param string|integer|array|Zend_Date $month Month to calculate with, if null the actual month is taken
  2967. * @param string|Zend_Locale $locale Locale for parsing input
  2968. * @return integer|Zend_Date new time
  2969. * @throws Zend_Date_Exception
  2970. */
  2971. private function _month($calc, $month, $locale)
  2972. {
  2973. if ($month === null) {
  2974. require_once 'Zend/Date/Exception.php';
  2975. throw new Zend_Date_Exception('parameter $month must be set, null is not allowed');
  2976. }
  2977. if ($locale === null) {
  2978. $locale = $this->getLocale();
  2979. }
  2980. if ($month instanceof Zend_Date) {
  2981. // extract month from object
  2982. $found = $month->get(self::MONTH_SHORT, $locale);
  2983. } else {
  2984. if (is_numeric($month)) {
  2985. $found = $month;
  2986. } else if (is_array($month)) {
  2987. if (isset($month['month']) === true) {
  2988. $month = $month['month'];
  2989. } else {
  2990. require_once 'Zend/Date/Exception.php';
  2991. throw new Zend_Date_Exception("no month given in array");
  2992. }
  2993. } else {
  2994. $monthlist = Zend_Locale_Data::getList($locale, 'month');
  2995. $monthlist2 = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'format', 'abbreviated'));
  2996. $monthlist = array_merge($monthlist, $monthlist2);
  2997. $found = 0;
  2998. $cnt = 0;
  2999. foreach ($monthlist as $key => $value) {
  3000. if (strtoupper($value) == strtoupper($month)) {
  3001. $found = $key + 1;
  3002. break;
  3003. }
  3004. ++$cnt;
  3005. }
  3006. if ($found == 0) {
  3007. foreach ($monthlist2 as $key => $value) {
  3008. if (strtoupper(iconv_substr($value, 0, 1, 'UTF-8')) == strtoupper($month)) {
  3009. $found = $key + 1;
  3010. break;
  3011. }
  3012. ++$cnt;
  3013. }
  3014. }
  3015. if ($found == 0) {
  3016. require_once 'Zend/Date/Exception.php';
  3017. throw new Zend_Date_Exception("unknown month name ($month)", $month);
  3018. }
  3019. }
  3020. }
  3021. $return = $this->_calcdetail($calc, $found, self::MONTH_SHORT, $locale);
  3022. if ($calc != 'cmp') {
  3023. return $this;
  3024. }
  3025. return $return;
  3026. }
  3027. /**
  3028. * Sets a new month
  3029. * The month can be a number or a string. Setting months lower then 0 and greater then 12
  3030. * will result in adding or subtracting the relevant year. (12 months equal one year)
  3031. * If a localized monthname is given it will be parsed with the default locale or the optional
  3032. * set locale.
  3033. * Returned is the new date object
  3034. *
  3035. * @param string|integer|array|Zend_Date $month Month to set
  3036. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3037. * @return Zend_Date new date
  3038. * @throws Zend_Date_Exception
  3039. */
  3040. public function setMonth($month, $locale = null)
  3041. {
  3042. return $this->_month('set', $month, $locale);
  3043. }
  3044. /**
  3045. * Adds months to the existing date object.
  3046. * The month can be a number or a string. Adding months lower then 0 and greater then 12
  3047. * will result in adding or subtracting the relevant year. (12 months equal one year)
  3048. * If a localized monthname is given it will be parsed with the default locale or the optional
  3049. * set locale.
  3050. * Returned is the new date object
  3051. *
  3052. * @param string|integer|array|Zend_Date $month Month to add
  3053. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3054. * @return Zend_Date new date
  3055. * @throws Zend_Date_Exception
  3056. */
  3057. public function addMonth($month, $locale = null)
  3058. {
  3059. return $this->_month('add', $month, $locale);
  3060. }
  3061. /**
  3062. * Subtracts months from the existing date object.
  3063. * The month can be a number or a string. Subtracting months lower then 0 and greater then 12
  3064. * will result in adding or subtracting the relevant year. (12 months equal one year)
  3065. * If a localized monthname is given it will be parsed with the default locale or the optional
  3066. * set locale.
  3067. * Returned is the new date object
  3068. *
  3069. * @param string|integer|array|Zend_Date $month Month to sub
  3070. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3071. * @return Zend_Date new date
  3072. * @throws Zend_Date_Exception
  3073. */
  3074. public function subMonth($month, $locale = null)
  3075. {
  3076. return $this->_month('sub', $month, $locale);
  3077. }
  3078. /**
  3079. * Compares the month with the existing date object, ignoring other date parts.
  3080. * For example: 10.03.2000 -> 15.03.1950 -> true
  3081. * Returns if equal, earlier or later
  3082. *
  3083. * @param string|integer|array|Zend_Date $month Month to compare
  3084. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3085. * @return integer 0 = equal, 1 = later, -1 = earlier
  3086. * @throws Zend_Date_Exception
  3087. */
  3088. public function compareMonth($month, $locale = null)
  3089. {
  3090. return $this->_month('cmp', $month, $locale);
  3091. }
  3092. /**
  3093. * Returns the day as new date object
  3094. * Example: 20.May.1986 -> 20.Jan.1970 00:00:00
  3095. *
  3096. * @param $locale string|Zend_Locale OPTIONAL Locale for parsing input
  3097. * @return Zend_Date
  3098. */
  3099. public function getDay($locale = null)
  3100. {
  3101. return $this->copyPart(self::DAY_SHORT, $locale);
  3102. }
  3103. /**
  3104. * Returns the calculated day
  3105. *
  3106. * @param $calc string Type of calculation to make
  3107. * @param $day string|integer|Zend_Date Day to calculate, when null the actual day is calculated
  3108. * @param $locale string|Zend_Locale Locale for parsing input
  3109. * @return Zend_Date|integer
  3110. */
  3111. private function _day($calc, $day, $locale)
  3112. {
  3113. if ($day === null) {
  3114. require_once 'Zend/Date/Exception.php';
  3115. throw new Zend_Date_Exception('parameter $day must be set, null is not allowed');
  3116. }
  3117. if ($locale === null) {
  3118. $locale = $this->getLocale();
  3119. }
  3120. if ($day instanceof Zend_Date) {
  3121. $day = $day->get(self::DAY_SHORT, $locale);
  3122. }
  3123. if (is_numeric($day)) {
  3124. $type = self::DAY_SHORT;
  3125. } else if (is_array($day)) {
  3126. if (isset($day['day']) === true) {
  3127. $day = $day['day'];
  3128. $type = self::WEEKDAY;
  3129. } else {
  3130. require_once 'Zend/Date/Exception.php';
  3131. throw new Zend_Date_Exception("no day given in array");
  3132. }
  3133. } else {
  3134. switch (iconv_strlen($day, 'UTF-8')) {
  3135. case 1 :
  3136. $type = self::WEEKDAY_NARROW;
  3137. break;
  3138. case 2:
  3139. $type = self::WEEKDAY_NAME;
  3140. break;
  3141. case 3:
  3142. $type = self::WEEKDAY_SHORT;
  3143. break;
  3144. default:
  3145. $type = self::WEEKDAY;
  3146. break;
  3147. }
  3148. }
  3149. $return = $this->_calcdetail($calc, $day, $type, $locale);
  3150. if ($calc != 'cmp') {
  3151. return $this;
  3152. }
  3153. return $return;
  3154. }
  3155. /**
  3156. * Sets a new day
  3157. * The day can be a number or a string. Setting days lower then 0 or greater than the number of this months days
  3158. * will result in adding or subtracting the relevant month.
  3159. * If a localized dayname is given it will be parsed with the default locale or the optional
  3160. * set locale.
  3161. * Returned is the new date object
  3162. * Example: setDay('Montag', 'de_AT'); will set the monday of this week as day.
  3163. *
  3164. * @param string|integer|array|Zend_Date $month Day to set
  3165. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3166. * @return Zend_Date new date
  3167. * @throws Zend_Date_Exception
  3168. */
  3169. public function setDay($day, $locale = null)
  3170. {
  3171. return $this->_day('set', $day, $locale);
  3172. }
  3173. /**
  3174. * Adds days to the existing date object.
  3175. * The day can be a number or a string. Adding days lower then 0 or greater than the number of this months days
  3176. * will result in adding or subtracting the relevant month.
  3177. * If a localized dayname is given it will be parsed with the default locale or the optional
  3178. * set locale.
  3179. * Returned is the new date object
  3180. * Example: addDay('Montag', 'de_AT'); will add the number of days until the next monday
  3181. *
  3182. * @param string|integer|array|Zend_Date $month Day to add
  3183. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3184. * @return Zend_Date new date
  3185. * @throws Zend_Date_Exception
  3186. */
  3187. public function addDay($day, $locale = null)
  3188. {
  3189. return $this->_day('add', $day, $locale);
  3190. }
  3191. /**
  3192. * Subtracts days from the existing date object.
  3193. * The day can be a number or a string. Subtracting days lower then 0 or greater than the number of this months days
  3194. * will result in adding or subtracting the relevant month.
  3195. * If a localized dayname is given it will be parsed with the default locale or the optional
  3196. * set locale.
  3197. * Returned is the new date object
  3198. * Example: subDay('Montag', 'de_AT'); will sub the number of days until the previous monday
  3199. *
  3200. * @param string|integer|array|Zend_Date $month Day to sub
  3201. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3202. * @return Zend_Date new date
  3203. * @throws Zend_Date_Exception
  3204. */
  3205. public function subDay($day, $locale = null)
  3206. {
  3207. return $this->_day('sub', $day, $locale);
  3208. }
  3209. /**
  3210. * Compares the day with the existing date object, ignoring other date parts.
  3211. * For example: 'Monday', 'en' -> 08.Jan.2007 -> 0
  3212. * Returns if equal, earlier or later
  3213. *
  3214. * @param string|integer|array|Zend_Date $day Day to compare
  3215. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3216. * @return integer 0 = equal, 1 = later, -1 = earlier
  3217. * @throws Zend_Date_Exception
  3218. */
  3219. public function compareDay($day, $locale = null)
  3220. {
  3221. return $this->_day('cmp', $day, $locale);
  3222. }
  3223. /**
  3224. * Returns the weekday as new date object
  3225. * Weekday is always from 1-7
  3226. * Example: 09-Jan-2007 -> 2 = Tuesday -> 02-Jan-1970 (when 02.01.1970 is also Tuesday)
  3227. *
  3228. * @param $locale string|Zend_Locale OPTIONAL Locale for parsing input
  3229. * @return Zend_Date
  3230. */
  3231. public function getWeekday($locale = null)
  3232. {
  3233. return $this->copyPart(self::WEEKDAY, $locale);
  3234. }
  3235. /**
  3236. * Returns the calculated weekday
  3237. *
  3238. * @param $calc string Type of calculation to make
  3239. * @param $weekday string|integer|array|Zend_Date Weekday to calculate, when null the actual weekday is calculated
  3240. * @param $locale string|Zend_Locale Locale for parsing input
  3241. * @return Zend_Date|integer
  3242. * @throws Zend_Date_Exception
  3243. */
  3244. private function _weekday($calc, $weekday, $locale)
  3245. {
  3246. if ($weekday === null) {
  3247. require_once 'Zend/Date/Exception.php';
  3248. throw new Zend_Date_Exception('parameter $weekday must be set, null is not allowed');
  3249. }
  3250. if ($locale === null) {
  3251. $locale = $this->getLocale();
  3252. }
  3253. if ($weekday instanceof Zend_Date) {
  3254. $weekday = $weekday->get(self::WEEKDAY_8601, $locale);
  3255. }
  3256. if (is_numeric($weekday)) {
  3257. $type = self::WEEKDAY_8601;
  3258. } else if (is_array($weekday)) {
  3259. if (isset($weekday['weekday']) === true) {
  3260. $weekday = $weekday['weekday'];
  3261. $type = self::WEEKDAY;
  3262. } else {
  3263. require_once 'Zend/Date/Exception.php';
  3264. throw new Zend_Date_Exception("no weekday given in array");
  3265. }
  3266. } else {
  3267. switch(iconv_strlen($weekday, 'UTF-8')) {
  3268. case 1:
  3269. $type = self::WEEKDAY_NARROW;
  3270. break;
  3271. case 2:
  3272. $type = self::WEEKDAY_NAME;
  3273. break;
  3274. case 3:
  3275. $type = self::WEEKDAY_SHORT;
  3276. break;
  3277. default:
  3278. $type = self::WEEKDAY;
  3279. break;
  3280. }
  3281. }
  3282. $return = $this->_calcdetail($calc, $weekday, $type, $locale);
  3283. if ($calc != 'cmp') {
  3284. return $this;
  3285. }
  3286. return $return;
  3287. }
  3288. /**
  3289. * Sets a new weekday
  3290. * The weekday can be a number or a string. If a localized weekday name is given,
  3291. * then it will be parsed as a date in $locale (defaults to the same locale as $this).
  3292. * Returned is the new date object.
  3293. * Example: setWeekday(3); will set the wednesday of this week as day.
  3294. *
  3295. * @param string|integer|array|Zend_Date $month Weekday to set
  3296. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3297. * @return Zend_Date new date
  3298. * @throws Zend_Date_Exception
  3299. */
  3300. public function setWeekday($weekday, $locale = null)
  3301. {
  3302. return $this->_weekday('set', $weekday, $locale);
  3303. }
  3304. /**
  3305. * Adds weekdays to the existing date object.
  3306. * The weekday can be a number or a string.
  3307. * If a localized dayname is given it will be parsed with the default locale or the optional
  3308. * set locale.
  3309. * Returned is the new date object
  3310. * Example: addWeekday(3); will add the difference of days from the begining of the month until
  3311. * wednesday.
  3312. *
  3313. * @param string|integer|array|Zend_Date $month Weekday to add
  3314. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3315. * @return Zend_Date new date
  3316. * @throws Zend_Date_Exception
  3317. */
  3318. public function addWeekday($weekday, $locale = null)
  3319. {
  3320. return $this->_weekday('add', $weekday, $locale);
  3321. }
  3322. /**
  3323. * Subtracts weekdays from the existing date object.
  3324. * The weekday can be a number or a string.
  3325. * If a localized dayname is given it will be parsed with the default locale or the optional
  3326. * set locale.
  3327. * Returned is the new date object
  3328. * Example: subWeekday(3); will subtract the difference of days from the begining of the month until
  3329. * wednesday.
  3330. *
  3331. * @param string|integer|array|Zend_Date $month Weekday to sub
  3332. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3333. * @return Zend_Date new date
  3334. * @throws Zend_Date_Exception
  3335. */
  3336. public function subWeekday($weekday, $locale = null)
  3337. {
  3338. return $this->_weekday('sub', $weekday, $locale);
  3339. }
  3340. /**
  3341. * Compares the weekday with the existing date object, ignoring other date parts.
  3342. * For example: 'Monday', 'en' -> 08.Jan.2007 -> 0
  3343. * Returns if equal, earlier or later
  3344. *
  3345. * @param string|integer|array|Zend_Date $weekday Weekday to compare
  3346. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3347. * @return integer 0 = equal, 1 = later, -1 = earlier
  3348. * @throws Zend_Date_Exception
  3349. */
  3350. public function compareWeekday($weekday, $locale = null)
  3351. {
  3352. return $this->_weekday('cmp', $weekday, $locale);
  3353. }
  3354. /**
  3355. * Returns the day of year as new date object
  3356. * Example: 02.Feb.1986 10:00:00 -> 02.Feb.1970 00:00:00
  3357. *
  3358. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3359. * @return Zend_Date
  3360. */
  3361. public function getDayOfYear($locale = null)
  3362. {
  3363. return $this->copyPart(self::DAY_OF_YEAR, $locale);
  3364. }
  3365. /**
  3366. * Sets a new day of year
  3367. * The day of year is always a number.
  3368. * Returned is the new date object
  3369. * Example: 04.May.2004 -> setDayOfYear(10) -> 10.Jan.2004
  3370. *
  3371. * @param string|integer|array|Zend_Date $day Day of Year to set
  3372. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3373. * @return Zend_Date new date
  3374. * @throws Zend_Date_Exception
  3375. */
  3376. public function setDayOfYear($day, $locale = null)
  3377. {
  3378. return $this->_calcvalue('set', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3379. }
  3380. /**
  3381. * Adds a day of year to the existing date object.
  3382. * The day of year is always a number.
  3383. * Returned is the new date object
  3384. * Example: addDayOfYear(10); will add 10 days to the existing date object.
  3385. *
  3386. * @param string|integer|array|Zend_Date $day Day of Year to add
  3387. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3388. * @return Zend_Date new date
  3389. * @throws Zend_Date_Exception
  3390. */
  3391. public function addDayOfYear($day, $locale = null)
  3392. {
  3393. return $this->_calcvalue('add', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3394. }
  3395. /**
  3396. * Subtracts a day of year from the existing date object.
  3397. * The day of year is always a number.
  3398. * Returned is the new date object
  3399. * Example: subDayOfYear(10); will subtract 10 days from the existing date object.
  3400. *
  3401. * @param string|integer|array|Zend_Date $day Day of Year to sub
  3402. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3403. * @return Zend_Date new date
  3404. * @throws Zend_Date_Exception
  3405. */
  3406. public function subDayOfYear($day, $locale = null)
  3407. {
  3408. return $this->_calcvalue('sub', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3409. }
  3410. /**
  3411. * Compares the day of year with the existing date object.
  3412. * For example: compareDayOfYear(33) -> 02.Feb.2007 -> 0
  3413. * Returns if equal, earlier or later
  3414. *
  3415. * @param string|integer|array|Zend_Date $day Day of Year to compare
  3416. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3417. * @return integer 0 = equal, 1 = later, -1 = earlier
  3418. * @throws Zend_Date_Exception
  3419. */
  3420. public function compareDayOfYear($day, $locale = null)
  3421. {
  3422. return $this->_calcvalue('cmp', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3423. }
  3424. /**
  3425. * Returns the hour as new date object
  3426. * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 10:00:00
  3427. *
  3428. * @param $locale string|Zend_Locale OPTIONAL Locale for parsing input
  3429. * @return Zend_Date
  3430. */
  3431. public function getHour($locale = null)
  3432. {
  3433. return $this->copyPart(self::HOUR, $locale);
  3434. }
  3435. /**
  3436. * Sets a new hour
  3437. * The hour is always a number.
  3438. * Returned is the new date object
  3439. * Example: 04.May.1993 13:07:25 -> setHour(7); -> 04.May.1993 07:07:25
  3440. *
  3441. * @param string|integer|array|Zend_Date $hour Hour to set
  3442. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3443. * @return Zend_Date new date
  3444. * @throws Zend_Date_Exception
  3445. */
  3446. public function setHour($hour, $locale = null)
  3447. {
  3448. return $this->_calcvalue('set', $hour, 'hour', self::HOUR_SHORT, $locale);
  3449. }
  3450. /**
  3451. * Adds hours to the existing date object.
  3452. * The hour is always a number.
  3453. * Returned is the new date object
  3454. * Example: 04.May.1993 13:07:25 -> addHour(12); -> 05.May.1993 01:07:25
  3455. *
  3456. * @param string|integer|array|Zend_Date $hour Hour to add
  3457. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3458. * @return Zend_Date new date
  3459. * @throws Zend_Date_Exception
  3460. */
  3461. public function addHour($hour, $locale = null)
  3462. {
  3463. return $this->_calcvalue('add', $hour, 'hour', self::HOUR_SHORT, $locale);
  3464. }
  3465. /**
  3466. * Subtracts hours from the existing date object.
  3467. * The hour is always a number.
  3468. * Returned is the new date object
  3469. * Example: 04.May.1993 13:07:25 -> subHour(6); -> 05.May.1993 07:07:25
  3470. *
  3471. * @param string|integer|array|Zend_Date $hour Hour to sub
  3472. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3473. * @return Zend_Date new date
  3474. * @throws Zend_Date_Exception
  3475. */
  3476. public function subHour($hour, $locale = null)
  3477. {
  3478. return $this->_calcvalue('sub', $hour, 'hour', self::HOUR_SHORT, $locale);
  3479. }
  3480. /**
  3481. * Compares the hour with the existing date object.
  3482. * For example: 10:30:25 -> compareHour(10) -> 0
  3483. * Returns if equal, earlier or later
  3484. *
  3485. * @param string|integer|array|Zend_Date $hour Hour to compare
  3486. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3487. * @return integer 0 = equal, 1 = later, -1 = earlier
  3488. * @throws Zend_Date_Exception
  3489. */
  3490. public function compareHour($hour, $locale = null)
  3491. {
  3492. return $this->_calcvalue('cmp', $hour, 'hour', self::HOUR_SHORT, $locale);
  3493. }
  3494. /**
  3495. * Returns the minute as new date object
  3496. * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 00:30:00
  3497. *
  3498. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3499. * @return Zend_Date
  3500. */
  3501. public function getMinute($locale = null)
  3502. {
  3503. return $this->copyPart(self::MINUTE, $locale);
  3504. }
  3505. /**
  3506. * Sets a new minute
  3507. * The minute is always a number.
  3508. * Returned is the new date object
  3509. * Example: 04.May.1993 13:07:25 -> setMinute(29); -> 04.May.1993 13:29:25
  3510. *
  3511. * @param string|integer|array|Zend_Date $minute Minute to set
  3512. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3513. * @return Zend_Date new date
  3514. * @throws Zend_Date_Exception
  3515. */
  3516. public function setMinute($minute, $locale = null)
  3517. {
  3518. return $this->_calcvalue('set', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3519. }
  3520. /**
  3521. * Adds minutes to the existing date object.
  3522. * The minute is always a number.
  3523. * Returned is the new date object
  3524. * Example: 04.May.1993 13:07:25 -> addMinute(65); -> 04.May.1993 13:12:25
  3525. *
  3526. * @param string|integer|array|Zend_Date $minute Minute to add
  3527. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3528. * @return Zend_Date new date
  3529. * @throws Zend_Date_Exception
  3530. */
  3531. public function addMinute($minute, $locale = null)
  3532. {
  3533. return $this->_calcvalue('add', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3534. }
  3535. /**
  3536. * Subtracts minutes from the existing date object.
  3537. * The minute is always a number.
  3538. * Returned is the new date object
  3539. * Example: 04.May.1993 13:07:25 -> subMinute(9); -> 04.May.1993 12:58:25
  3540. *
  3541. * @param string|integer|array|Zend_Date $minute Minute to sub
  3542. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3543. * @return Zend_Date new date
  3544. * @throws Zend_Date_Exception
  3545. */
  3546. public function subMinute($minute, $locale = null)
  3547. {
  3548. return $this->_calcvalue('sub', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3549. }
  3550. /**
  3551. * Compares the minute with the existing date object.
  3552. * For example: 10:30:25 -> compareMinute(30) -> 0
  3553. * Returns if equal, earlier or later
  3554. *
  3555. * @param string|integer|array|Zend_Date $minute Hour to compare
  3556. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3557. * @return integer 0 = equal, 1 = later, -1 = earlier
  3558. * @throws Zend_Date_Exception
  3559. */
  3560. public function compareMinute($minute, $locale = null)
  3561. {
  3562. return $this->_calcvalue('cmp', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3563. }
  3564. /**
  3565. * Returns the second as new date object
  3566. * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 00:00:25
  3567. *
  3568. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3569. * @return Zend_Date
  3570. */
  3571. public function getSecond($locale = null)
  3572. {
  3573. return $this->copyPart(self::SECOND, $locale);
  3574. }
  3575. /**
  3576. * Sets new seconds to the existing date object.
  3577. * The second is always a number.
  3578. * Returned is the new date object
  3579. * Example: 04.May.1993 13:07:25 -> setSecond(100); -> 04.May.1993 13:08:40
  3580. *
  3581. * @param string|integer|array|Zend_Date $second Second to set
  3582. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3583. * @return Zend_Date new date
  3584. * @throws Zend_Date_Exception
  3585. */
  3586. public function setSecond($second, $locale = null)
  3587. {
  3588. return $this->_calcvalue('set', $second, 'second', self::SECOND_SHORT, $locale);
  3589. }
  3590. /**
  3591. * Adds seconds to the existing date object.
  3592. * The second is always a number.
  3593. * Returned is the new date object
  3594. * Example: 04.May.1993 13:07:25 -> addSecond(65); -> 04.May.1993 13:08:30
  3595. *
  3596. * @param string|integer|array|Zend_Date $second Second to add
  3597. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3598. * @return Zend_Date new date
  3599. * @throws Zend_Date_Exception
  3600. */
  3601. public function addSecond($second, $locale = null)
  3602. {
  3603. return $this->_calcvalue('add', $second, 'second', self::SECOND_SHORT, $locale);
  3604. }
  3605. /**
  3606. * Subtracts seconds from the existing date object.
  3607. * The second is always a number.
  3608. * Returned is the new date object
  3609. * Example: 04.May.1993 13:07:25 -> subSecond(10); -> 04.May.1993 13:07:15
  3610. *
  3611. * @param string|integer|array|Zend_Date $second Second to sub
  3612. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3613. * @return Zend_Date new date
  3614. * @throws Zend_Date_Exception
  3615. */
  3616. public function subSecond($second, $locale = null)
  3617. {
  3618. return $this->_calcvalue('sub', $second, 'second', self::SECOND_SHORT, $locale);
  3619. }
  3620. /**
  3621. * Compares the second with the existing date object.
  3622. * For example: 10:30:25 -> compareSecond(25) -> 0
  3623. * Returns if equal, earlier or later
  3624. *
  3625. * @param string|integer|array|Zend_Date $second Second to compare
  3626. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3627. * @return integer 0 = equal, 1 = later, -1 = earlier
  3628. * @throws Zend_Date_Exception
  3629. */
  3630. public function compareSecond($second, $locale = null)
  3631. {
  3632. return $this->_calcvalue('cmp', $second, 'second', self::SECOND_SHORT, $locale);
  3633. }
  3634. /**
  3635. * Returns the precision for fractional seconds
  3636. *
  3637. * @return integer
  3638. */
  3639. public function getFractionalPrecision()
  3640. {
  3641. return $this->_precision;
  3642. }
  3643. /**
  3644. * Sets a new precision for fractional seconds
  3645. *
  3646. * @param integer $precision Precision for the fractional datepart 3 = milliseconds
  3647. * @throws Zend_Date_Exception
  3648. * @return void
  3649. */
  3650. public function setFractionalPrecision($precision)
  3651. {
  3652. if (!intval($precision) or ($precision < 0) or ($precision > 9)) {
  3653. require_once 'Zend/Date/Exception.php';
  3654. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", $precision);
  3655. }
  3656. $this->_precision = (int) $precision;
  3657. }
  3658. /**
  3659. * Returns the milliseconds of the date object
  3660. *
  3661. * @return integer
  3662. */
  3663. public function getMilliSecond()
  3664. {
  3665. return $this->_fractional;
  3666. }
  3667. /**
  3668. * Sets new milliseconds for the date object
  3669. * Example: setMilliSecond(550, 2) -> equals +5 Sec +50 MilliSec
  3670. *
  3671. * @param integer|Zend_Date $milli (Optional) Millisecond to set, when null the actual millisecond is set
  3672. * @param integer $precision (Optional) Fraction precision of the given milliseconds
  3673. * @return integer|string
  3674. */
  3675. public function setMilliSecond($milli = null, $precision = null)
  3676. {
  3677. if ($milli === null) {
  3678. list($milli, $time) = explode(" ", microtime());
  3679. $milli = intval($milli);
  3680. $precision = 6;
  3681. } else if (!is_numeric($milli)) {
  3682. require_once 'Zend/Date/Exception.php';
  3683. throw new Zend_Date_Exception("invalid milli second ($milli) operand", $milli);
  3684. }
  3685. if ($precision === null) {
  3686. $precision = $this->_precision;
  3687. } else if (!is_int($precision) || $precision < 1 || $precision > 9) {
  3688. require_once 'Zend/Date/Exception.php';
  3689. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", $precision);
  3690. }
  3691. $this->_fractional = 0;
  3692. $this->addMilliSecond($milli, $precision);
  3693. return $this->_fractional;
  3694. }
  3695. /**
  3696. * Adds milliseconds to the date object
  3697. *
  3698. * @param integer|Zend_Date $milli (Optional) Millisecond to add, when null the actual millisecond is added
  3699. * @param integer $precision (Optional) Fractional precision for the given milliseconds
  3700. * @return integer|string
  3701. */
  3702. public function addMilliSecond($milli = null, $precision = null)
  3703. {
  3704. if ($milli === null) {
  3705. list($milli, $time) = explode(" ", microtime());
  3706. $milli = intval($milli);
  3707. } else if (!is_numeric($milli)) {
  3708. require_once 'Zend/Date/Exception.php';
  3709. throw new Zend_Date_Exception("invalid milli second ($milli) operand", $milli);
  3710. }
  3711. if ($precision === null) {
  3712. $precision = $this->_precision;
  3713. } else if (!is_int($precision) || $precision < 1 || $precision > 9) {
  3714. require_once 'Zend/Date/Exception.php';
  3715. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", $precision);
  3716. }
  3717. if ($precision != $this->_precision) {
  3718. if ($precision > $this->_precision) {
  3719. $diff = $precision - $this->_precision;
  3720. $milli = (int) ($milli / (10 * $diff));
  3721. } else {
  3722. $diff = $this->_precision - $precision;
  3723. $milli = (int) ($milli * (10 * $diff));
  3724. }
  3725. }
  3726. $this->_fractional += $milli;
  3727. // Add/sub milliseconds + add/sub seconds
  3728. $max = pow(10, $this->_precision);
  3729. // Milli includes seconds
  3730. if ($this->_fractional >= $max) {
  3731. while ($this->_fractional >= $max) {
  3732. $this->addSecond(1);
  3733. $this->_fractional -= $max;
  3734. }
  3735. }
  3736. if ($this->_fractional < 0) {
  3737. while ($this->_fractional < 0) {
  3738. $this->subSecond(1);
  3739. $this->_fractional += $max;
  3740. }
  3741. }
  3742. return $this->_fractional;
  3743. }
  3744. /**
  3745. * Subtracts a millisecond
  3746. *
  3747. * @param integer|Zend_Date $milli (Optional) Millisecond to sub, when null the actual millisecond is subtracted
  3748. * @param integer $precision (Optional) Fractional precision for the given milliseconds
  3749. * @return integer
  3750. */
  3751. public function subMilliSecond($milli = null, $precision = null)
  3752. {
  3753. return $this->addMilliSecond(0 - $milli);
  3754. }
  3755. /**
  3756. * Compares only the millisecond part, returning the difference
  3757. *
  3758. * @param integer|Zend_Date $milli OPTIONAL Millisecond to compare, when null the actual millisecond is compared
  3759. * @param integer $precision OPTIONAL Fractional precision for the given milliseconds
  3760. * @return integer
  3761. */
  3762. public function compareMilliSecond($milli = null, $precision = null)
  3763. {
  3764. if ($milli === null) {
  3765. list($milli, $time) = explode(" ", microtime());
  3766. $milli = intval($milli);
  3767. } else if (is_numeric($milli) === false) {
  3768. require_once 'Zend/Date/Exception.php';
  3769. throw new Zend_Date_Exception("invalid milli second ($milli) operand", $milli);
  3770. }
  3771. if ($precision === null) {
  3772. $precision = $this->_precision;
  3773. } else if (!is_int($precision) || $precision < 1 || $precision > 9) {
  3774. require_once 'Zend/Date/Exception.php';
  3775. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", $precision);
  3776. }
  3777. if ($precision === 0) {
  3778. require_once 'Zend/Date/Exception.php';
  3779. throw new Zend_Date_Exception('precision is 0');
  3780. }
  3781. if ($precision != $this->_precision) {
  3782. if ($precision > $this->_precision) {
  3783. $diff = $precision - $this->_precision;
  3784. $milli = (int) ($milli / (10 * $diff));
  3785. } else {
  3786. $diff = $this->_precision - $precision;
  3787. $milli = (int) ($milli * (10 * $diff));
  3788. }
  3789. }
  3790. $comp = $this->_fractional - $milli;
  3791. if ($comp < 0) {
  3792. return -1;
  3793. } else if ($comp > 0) {
  3794. return 1;
  3795. }
  3796. return 0;
  3797. }
  3798. /**
  3799. * Returns the week as new date object using monday as begining of the week
  3800. * Example: 12.Jan.2007 -> 08.Jan.1970 00:00:00
  3801. *
  3802. * @param $locale string|Zend_Locale OPTIONAL Locale for parsing input
  3803. * @return Zend_Date
  3804. */
  3805. public function getWeek($locale = null)
  3806. {
  3807. return $this->copyPart(self::WEEK, $locale);
  3808. }
  3809. /**
  3810. * Sets a new week. The week is always a number. The day of week is not changed.
  3811. * Returned is the new date object
  3812. * Example: 09.Jan.2007 13:07:25 -> setWeek(1); -> 02.Jan.2007 13:07:25
  3813. *
  3814. * @param string|integer|array|Zend_Date $week Week to set
  3815. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3816. * @return Zend_Date
  3817. * @throws Zend_Date_Exception
  3818. */
  3819. public function setWeek($week, $locale = null)
  3820. {
  3821. return $this->_calcvalue('set', $week, 'week', self::WEEK, $locale);
  3822. }
  3823. /**
  3824. * Adds a week. The week is always a number. The day of week is not changed.
  3825. * Returned is the new date object
  3826. * Example: 09.Jan.2007 13:07:25 -> addWeek(1); -> 16.Jan.2007 13:07:25
  3827. *
  3828. * @param string|integer|array|Zend_Date $week Week to add
  3829. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3830. * @return Zend_Date
  3831. * @throws Zend_Date_Exception
  3832. */
  3833. public function addWeek($week, $locale = null)
  3834. {
  3835. return $this->_calcvalue('add', $week, 'week', self::WEEK, $locale);
  3836. }
  3837. /**
  3838. * Subtracts a week. The week is always a number. The day of week is not changed.
  3839. * Returned is the new date object
  3840. * Example: 09.Jan.2007 13:07:25 -> subWeek(1); -> 02.Jan.2007 13:07:25
  3841. *
  3842. * @param string|integer|array|Zend_Date $week Week to sub
  3843. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3844. * @return Zend_Date
  3845. * @throws Zend_Date_Exception
  3846. */
  3847. public function subWeek($week, $locale = null)
  3848. {
  3849. return $this->_calcvalue('sub', $week, 'week', self::WEEK, $locale);
  3850. }
  3851. /**
  3852. * Compares only the week part, returning the difference
  3853. * Returned is the new date object
  3854. * Returns if equal, earlier or later
  3855. * Example: 09.Jan.2007 13:07:25 -> compareWeek(2); -> 0
  3856. *
  3857. * @param string|integer|array|Zend_Date $week Week to compare
  3858. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3859. * @return integer 0 = equal, 1 = later, -1 = earlier
  3860. */
  3861. public function compareWeek($week, $locale = null)
  3862. {
  3863. return $this->_calcvalue('cmp', $week, 'week', self::WEEK, $locale);
  3864. }
  3865. /**
  3866. * Sets a new standard locale for the date object.
  3867. * This locale will be used for all functions
  3868. * Returned is the really set locale.
  3869. * Example: 'de_XX' will be set to 'de' because 'de_XX' does not exist
  3870. * 'xx_YY' will be set to 'root' because 'xx' does not exist
  3871. *
  3872. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3873. * @throws Zend_Date_Exception When the given locale does not exist
  3874. * @return Zend_Date Provides fluent interface
  3875. */
  3876. public function setLocale($locale = null)
  3877. {
  3878. try {
  3879. $this->_locale = Zend_Locale::findLocale($locale);
  3880. } catch (Zend_Locale_Exception $e) {
  3881. require_once 'Zend/Date/Exception.php';
  3882. throw new Zend_Date_Exception($e->getMessage());
  3883. }
  3884. return $this;
  3885. }
  3886. /**
  3887. * Returns the actual set locale
  3888. *
  3889. * @return string
  3890. */
  3891. public function getLocale()
  3892. {
  3893. return $this->_locale;
  3894. }
  3895. /**
  3896. * Checks if the given date is a real date or datepart.
  3897. * Returns false if a expected datepart is missing or a datepart exceeds its possible border.
  3898. * But the check will only be done for the expected dateparts which are given by format.
  3899. * If no format is given the standard dateformat for the actual locale is used.
  3900. * f.e. 30.February.2007 will return false if format is 'dd.MMMM.YYYY'
  3901. *
  3902. * @param string $date Date to parse for correctness
  3903. * @param string $format (Optional) Format for parsing the date string
  3904. * @param string|Zend_Locale $locale (Optional) Locale for parsing date parts
  3905. * @return boolean True when all date parts are correct
  3906. */
  3907. public static function isDate($date, $format = null, $locale = null)
  3908. {
  3909. if (!is_string($date) and !is_numeric($date) and !($date instanceof Zend_Date)) {
  3910. return false;
  3911. }
  3912. if (($format !== null) and (Zend_Locale::isLocale($format, null, false))) {
  3913. $locale = $format;
  3914. $format = null;
  3915. }
  3916. $locale = Zend_Locale::findLocale($locale);
  3917. if ($format === null) {
  3918. $format = Zend_Locale_Format::getDateFormat($locale);
  3919. } else if (self::$_options['format_type'] == 'php') {
  3920. $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
  3921. }
  3922. $format = self::_getLocalizedToken($format, $locale);
  3923. try {
  3924. $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale,
  3925. 'date_format' => $format, 'format_type' => 'iso',
  3926. 'fix_date' => false));
  3927. } catch (Zend_Locale_Exception $e) {
  3928. // Date can not be parsed
  3929. return false;
  3930. }
  3931. if (((strpos($format, 'Y') !== false) or (strpos($format, 'y') !== false)) and
  3932. (!isset($parsed['year']))) {
  3933. // Year expected but not found
  3934. return false;
  3935. }
  3936. if ((strpos($format, 'M') !== false) and (!isset($parsed['month']))) {
  3937. // Month expected but not found
  3938. return false;
  3939. }
  3940. if ((strpos($format, 'd') !== false) and (!isset($parsed['day']))) {
  3941. // Day expected but not found
  3942. return false;
  3943. }
  3944. if (((strpos($format, 'H') !== false) or (strpos($format, 'h') !== false)) and
  3945. (!isset($parsed['hour']))) {
  3946. // Hour expected but not found
  3947. return false;
  3948. }
  3949. if ((strpos($format, 'm') !== false) and (!isset($parsed['minute']))) {
  3950. // Minute expected but not found
  3951. return false;
  3952. }
  3953. if ((strpos($format, 's') !== false) and (!isset($parsed['second']))) {
  3954. // Second expected but not found
  3955. return false;
  3956. }
  3957. // Set not given dateparts
  3958. if (isset($parsed['hour']) === false) {
  3959. $parsed['hour'] = 0;
  3960. }
  3961. if (isset($parsed['minute']) === false) {
  3962. $parsed['minute'] = 0;
  3963. }
  3964. if (isset($parsed['second']) === false) {
  3965. $parsed['second'] = 0;
  3966. }
  3967. if (isset($parsed['month']) === false) {
  3968. $parsed['month'] = 1;
  3969. }
  3970. if (isset($parsed['day']) === false) {
  3971. $parsed['day'] = 1;
  3972. }
  3973. if (isset($parsed['year']) === false) {
  3974. $parsed['year'] = 1970;
  3975. }
  3976. if (self::isYearLeapYear($parsed['year'])) {
  3977. $parsed['year'] = 1972;
  3978. } else {
  3979. $parsed['year'] = 1971;
  3980. }
  3981. $date = new self($parsed, null, $locale);
  3982. $timestamp = $date->mktime($parsed['hour'], $parsed['minute'], $parsed['second'],
  3983. $parsed['month'], $parsed['day'], $parsed['year']);
  3984. if ($parsed['year'] != $date->date('Y', $timestamp)) {
  3985. // Given year differs from parsed year
  3986. return false;
  3987. }
  3988. if ($parsed['month'] != $date->date('n', $timestamp)) {
  3989. // Given month differs from parsed month
  3990. return false;
  3991. }
  3992. if ($parsed['day'] != $date->date('j', $timestamp)) {
  3993. // Given day differs from parsed day
  3994. return false;
  3995. }
  3996. if ($parsed['hour'] != $date->date('G', $timestamp)) {
  3997. // Given hour differs from parsed hour
  3998. return false;
  3999. }
  4000. if ($parsed['minute'] != $date->date('i', $timestamp)) {
  4001. // Given minute differs from parsed minute
  4002. return false;
  4003. }
  4004. if ($parsed['second'] != $date->date('s', $timestamp)) {
  4005. // Given second differs from parsed second
  4006. return false;
  4007. }
  4008. return true;
  4009. }
  4010. /**
  4011. * Returns the ISO Token for all localized constants
  4012. *
  4013. * @param string $token Token to normalize
  4014. * @param string $locale Locale to search
  4015. * @return string
  4016. */
  4017. protected static function _getLocalizedToken($token, $locale)
  4018. {
  4019. switch($token) {
  4020. case self::ISO_8601 :
  4021. return "dd mm yy";
  4022. break;
  4023. case self::RFC_2822 :
  4024. return "EEE, dd MMM yyyy HH:mm:ss";
  4025. break;
  4026. case self::DATES :
  4027. return Zend_Locale_Data::getContent($locale, 'date');
  4028. break;
  4029. case self::DATE_FULL :
  4030. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full'));
  4031. break;
  4032. case self::DATE_LONG :
  4033. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long'));
  4034. break;
  4035. case self::DATE_MEDIUM :
  4036. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium'));
  4037. break;
  4038. case self::DATE_SHORT :
  4039. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short'));
  4040. break;
  4041. case self::TIMES :
  4042. return Zend_Locale_Data::getContent($locale, 'date');
  4043. break;
  4044. case self::TIME_FULL :
  4045. return Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'full'));
  4046. break;
  4047. case self::TIME_LONG :
  4048. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long'));
  4049. break;
  4050. case self::TIME_MEDIUM :
  4051. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium'));
  4052. break;
  4053. case self::TIME_SHORT :
  4054. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short'));
  4055. break;
  4056. case self::ATOM :
  4057. case self::RFC_3339 :
  4058. case self::W3C :
  4059. return "yyyy-MM-DD HH:mm:ss";
  4060. break;
  4061. case self::COOKIE :
  4062. case self::RFC_850 :
  4063. return "EEEE, dd-MM-yyyy HH:mm:ss";
  4064. break;
  4065. case self::RFC_822 :
  4066. case self::RFC_1036 :
  4067. case self::RFC_1123 :
  4068. case self::RSS :
  4069. return "EEE, dd MM yyyy HH:mm:ss";
  4070. break;
  4071. }
  4072. return $token;
  4073. }
  4074. }