Date.php 188 KB

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