Date.php 186 KB

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