Date.php 188 KB

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