Date.php 187 KB

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