dateformat_format.phpt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. --TEST--
  2. datefmt_format_code()
  3. --SKIPIF--
  4. <?php if( !extension_loaded( 'intl' ) ) print 'skip'; ?>
  5. <?php if (version_compare(INTL_ICU_VERSION, '51.2') >= 0) die('skip for ICU < 51.2'); ?>
  6. --FILE--
  7. <?php
  8. //ini_set("intl.error_level", E_WARNING);
  9. /*
  10. * Test for the datefmt_format function
  11. */
  12. function ut_main()
  13. {
  14. $timezone = 'GMT-10:00';
  15. $locale_arr = array (
  16. 'en_US'
  17. );
  18. $datetype_arr = array (
  19. IntlDateFormatter::FULL,
  20. IntlDateFormatter::LONG,
  21. IntlDateFormatter::MEDIUM,
  22. IntlDateFormatter::SHORT,
  23. IntlDateFormatter::NONE
  24. );
  25. $res_str = '';
  26. $time_arr = array (
  27. 0,
  28. -1200000,
  29. 1200000,
  30. 2200000000.0,
  31. -2200000000.0,
  32. 90099999,
  33. 3600,
  34. -3600
  35. );
  36. $localtime_arr1 = array (
  37. 'tm_sec' => 24 ,
  38. 'tm_min' => 3,
  39. 'tm_hour' => 19,
  40. 'tm_mday' => 3,
  41. 'tm_mon' => 3,
  42. 'tm_year' => 105,
  43. );
  44. $localtime_arr2 = array (
  45. 'tm_sec' => 21,
  46. 'tm_min' => 5,
  47. 'tm_hour' => 7,
  48. 'tm_mday' => 13,
  49. 'tm_mon' => 4,
  50. 'tm_year' => 205,
  51. );
  52. $localtime_arr3 = array (
  53. 'tm_sec' => 11,
  54. 'tm_min' => 13,
  55. 'tm_hour' => 0,
  56. 'tm_mday' => 17,
  57. 'tm_mon' => 11,
  58. 'tm_year' => -5
  59. );
  60. $localtime_arr = array (
  61. $localtime_arr1,
  62. $localtime_arr2,
  63. $localtime_arr3
  64. );
  65. $d1 = new DateTime("2010-01-01 01:02:03", new DateTimeZone("UTC"));
  66. $d2 = new DateTime("2000-12-31 03:04:05", new DateTimeZone("UTC"));
  67. $d2->setTimezone(new DateTimeZone("PDT"));
  68. $dates = array(
  69. $d1,
  70. $d2,
  71. new StdClass(),
  72. );
  73. //Test format with input as a timestamp : integer
  74. foreach( $time_arr as $timestamp_entry){
  75. $res_str .= "\n------------\n";
  76. $res_str .= "\nInput timestamp is : $timestamp_entry";
  77. $res_str .= "\n------------\n";
  78. foreach( $locale_arr as $locale_entry ){
  79. foreach( $datetype_arr as $datetype_entry )
  80. {
  81. $res_str .= "\nIntlDateFormatter locale= $locale_entry ,datetype = $datetype_entry ,timetype =$datetype_entry ";
  82. $fmt = ut_datefmt_create( $locale_entry , $datetype_entry ,$datetype_entry, $timezone, IntlDateFormatter::GREGORIAN);
  83. $formatted = ut_datefmt_format( $fmt , $timestamp_entry);
  84. $res_str .= "\nFormatted timestamp is : $formatted";
  85. }
  86. }
  87. }
  88. //Test format with input as a localtime :array
  89. foreach( $localtime_arr as $localtime_entry){
  90. $res_str .= "\n------------\n";
  91. $res_str .= "\nInput localtime is : ";
  92. foreach( $localtime_entry as $key => $value){
  93. $res_str .= "$key : '$value' , ";
  94. }
  95. $res_str .= "\n------------\n";
  96. foreach( $locale_arr as $locale_entry ){
  97. foreach( $datetype_arr as $datetype_entry )
  98. {
  99. $res_str .= "\nIntlDateFormatter locale= $locale_entry ,datetype = $datetype_entry ,timetype =$datetype_entry ";
  100. $fmt = ut_datefmt_create( $locale_entry , $datetype_entry ,$datetype_entry, $timezone, IntlDateFormatter::GREGORIAN );
  101. $formatted1 = ut_datefmt_format( $fmt , $localtime_entry);
  102. if( intl_get_error_code() == U_ZERO_ERROR){
  103. $res_str .= "\nFormatted localtime_array is : $formatted1";
  104. }else{
  105. $res_str .= "\nError while formatting as: '".intl_get_error_message()."'";
  106. }
  107. }
  108. }
  109. }
  110. foreach($dates as $date_entry) {
  111. foreach( $locale_arr as $locale_entry ){
  112. foreach( $datetype_arr as $datetype_entry ) {
  113. $res_str .= "\n------------";
  114. $res_str .= "\nDate is: ".var_export($date_entry, true);
  115. $res_str .= "\n------------";
  116. $fmt = ut_datefmt_create( $locale_entry , $datetype_entry ,$datetype_entry, $timezone, IntlDateFormatter::GREGORIAN );
  117. $formatted1 = ut_datefmt_format( $fmt , $date_entry);
  118. if( intl_get_error_code() == U_ZERO_ERROR){
  119. $res_str .= "\nFormatted DateTime is : $formatted1";
  120. }else{
  121. $res_str .= "\nError while formatting as: '".intl_get_error_message()."'";
  122. }
  123. }
  124. }
  125. }
  126. return $res_str;
  127. }
  128. include_once( 'ut_common.inc' );
  129. // Run the test
  130. ut_run();
  131. ?>
  132. --EXPECT--
  133. ------------
  134. Input timestamp is : 0
  135. ------------
  136. IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
  137. Formatted timestamp is : Wednesday, December 31, 1969 2:00:00 PM GMT-10:00
  138. IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
  139. Formatted timestamp is : December 31, 1969 2:00:00 PM GMT-10:00
  140. IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
  141. Formatted timestamp is : Dec 31, 1969 2:00:00 PM
  142. IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
  143. Formatted timestamp is : 12/31/69 2:00 PM
  144. IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
  145. Formatted timestamp is : 19691231 02:00 PM
  146. ------------
  147. Input timestamp is : -1200000
  148. ------------
  149. IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
  150. Formatted timestamp is : Wednesday, December 17, 1969 4:40:00 PM GMT-10:00
  151. IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
  152. Formatted timestamp is : December 17, 1969 4:40:00 PM GMT-10:00
  153. IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
  154. Formatted timestamp is : Dec 17, 1969 4:40:00 PM
  155. IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
  156. Formatted timestamp is : 12/17/69 4:40 PM
  157. IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
  158. Formatted timestamp is : 19691217 04:40 PM
  159. ------------
  160. Input timestamp is : 1200000
  161. ------------
  162. IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
  163. Formatted timestamp is : Wednesday, January 14, 1970 11:20:00 AM GMT-10:00
  164. IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
  165. Formatted timestamp is : January 14, 1970 11:20:00 AM GMT-10:00
  166. IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
  167. Formatted timestamp is : Jan 14, 1970 11:20:00 AM
  168. IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
  169. Formatted timestamp is : 1/14/70 11:20 AM
  170. IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
  171. Formatted timestamp is : 19700114 11:20 AM
  172. ------------
  173. Input timestamp is : 2200000000
  174. ------------
  175. IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
  176. Formatted timestamp is : Sunday, September 18, 2039 1:06:40 PM GMT-10:00
  177. IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
  178. Formatted timestamp is : September 18, 2039 1:06:40 PM GMT-10:00
  179. IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
  180. Formatted timestamp is : Sep 18, 2039 1:06:40 PM
  181. IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
  182. Formatted timestamp is : 9/18/39 1:06 PM
  183. IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
  184. Formatted timestamp is : 20390918 01:06 PM
  185. ------------
  186. Input timestamp is : -2200000000
  187. ------------
  188. IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
  189. Formatted timestamp is : Saturday, April 14, 1900 2:53:20 PM GMT-10:00
  190. IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
  191. Formatted timestamp is : April 14, 1900 2:53:20 PM GMT-10:00
  192. IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
  193. Formatted timestamp is : Apr 14, 1900 2:53:20 PM
  194. IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
  195. Formatted timestamp is : 4/14/00 2:53 PM
  196. IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
  197. Formatted timestamp is : 19000414 02:53 PM
  198. ------------
  199. Input timestamp is : 90099999
  200. ------------
  201. IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
  202. Formatted timestamp is : Wednesday, November 8, 1972 9:46:39 AM GMT-10:00
  203. IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
  204. Formatted timestamp is : November 8, 1972 9:46:39 AM GMT-10:00
  205. IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
  206. Formatted timestamp is : Nov 8, 1972 9:46:39 AM
  207. IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
  208. Formatted timestamp is : 11/8/72 9:46 AM
  209. IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
  210. Formatted timestamp is : 19721108 09:46 AM
  211. ------------
  212. Input timestamp is : 3600
  213. ------------
  214. IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
  215. Formatted timestamp is : Wednesday, December 31, 1969 3:00:00 PM GMT-10:00
  216. IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
  217. Formatted timestamp is : December 31, 1969 3:00:00 PM GMT-10:00
  218. IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
  219. Formatted timestamp is : Dec 31, 1969 3:00:00 PM
  220. IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
  221. Formatted timestamp is : 12/31/69 3:00 PM
  222. IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
  223. Formatted timestamp is : 19691231 03:00 PM
  224. ------------
  225. Input timestamp is : -3600
  226. ------------
  227. IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
  228. Formatted timestamp is : Wednesday, December 31, 1969 1:00:00 PM GMT-10:00
  229. IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
  230. Formatted timestamp is : December 31, 1969 1:00:00 PM GMT-10:00
  231. IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
  232. Formatted timestamp is : Dec 31, 1969 1:00:00 PM
  233. IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
  234. Formatted timestamp is : 12/31/69 1:00 PM
  235. IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
  236. Formatted timestamp is : 19691231 01:00 PM
  237. ------------
  238. Input localtime is : tm_sec : '24' , tm_min : '3' , tm_hour : '19' , tm_mday : '3' , tm_mon : '3' , tm_year : '105' ,
  239. ------------
  240. IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
  241. Formatted localtime_array is : Sunday, April 3, 2005 7:03:24 PM GMT-10:00
  242. IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
  243. Formatted localtime_array is : April 3, 2005 7:03:24 PM GMT-10:00
  244. IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
  245. Formatted localtime_array is : Apr 3, 2005 7:03:24 PM
  246. IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
  247. Formatted localtime_array is : 4/3/05 7:03 PM
  248. IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
  249. Formatted localtime_array is : 20050403 07:03 PM
  250. ------------
  251. Input localtime is : tm_sec : '21' , tm_min : '5' , tm_hour : '7' , tm_mday : '13' , tm_mon : '4' , tm_year : '205' ,
  252. ------------
  253. IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
  254. Formatted localtime_array is : Wednesday, May 13, 2105 7:05:21 AM GMT-10:00
  255. IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
  256. Formatted localtime_array is : May 13, 2105 7:05:21 AM GMT-10:00
  257. IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
  258. Formatted localtime_array is : May 13, 2105 7:05:21 AM
  259. IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
  260. Formatted localtime_array is : 5/13/05 7:05 AM
  261. IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
  262. Formatted localtime_array is : 21050513 07:05 AM
  263. ------------
  264. Input localtime is : tm_sec : '11' , tm_min : '13' , tm_hour : '0' , tm_mday : '17' , tm_mon : '11' , tm_year : '-5' ,
  265. ------------
  266. IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
  267. Formatted localtime_array is : Tuesday, December 17, 1895 12:13:11 AM GMT-10:00
  268. IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
  269. Formatted localtime_array is : December 17, 1895 12:13:11 AM GMT-10:00
  270. IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
  271. Formatted localtime_array is : Dec 17, 1895 12:13:11 AM
  272. IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
  273. Formatted localtime_array is : 12/17/95 12:13 AM
  274. IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
  275. Formatted localtime_array is : 18951217 12:13 AM
  276. ------------
  277. Date is: DateTime::__set_state(array(
  278. 'date' => '2010-01-01 01:02:03.000000',
  279. 'timezone_type' => 3,
  280. 'timezone' => 'UTC',
  281. ))
  282. ------------
  283. Formatted DateTime is : Thursday, December 31, 2009 3:02:03 PM GMT-10:00
  284. ------------
  285. Date is: DateTime::__set_state(array(
  286. 'date' => '2010-01-01 01:02:03.000000',
  287. 'timezone_type' => 3,
  288. 'timezone' => 'UTC',
  289. ))
  290. ------------
  291. Formatted DateTime is : December 31, 2009 3:02:03 PM GMT-10:00
  292. ------------
  293. Date is: DateTime::__set_state(array(
  294. 'date' => '2010-01-01 01:02:03.000000',
  295. 'timezone_type' => 3,
  296. 'timezone' => 'UTC',
  297. ))
  298. ------------
  299. Formatted DateTime is : Dec 31, 2009 3:02:03 PM
  300. ------------
  301. Date is: DateTime::__set_state(array(
  302. 'date' => '2010-01-01 01:02:03.000000',
  303. 'timezone_type' => 3,
  304. 'timezone' => 'UTC',
  305. ))
  306. ------------
  307. Formatted DateTime is : 12/31/09 3:02 PM
  308. ------------
  309. Date is: DateTime::__set_state(array(
  310. 'date' => '2010-01-01 01:02:03.000000',
  311. 'timezone_type' => 3,
  312. 'timezone' => 'UTC',
  313. ))
  314. ------------
  315. Formatted DateTime is : 20091231 03:02 PM
  316. ------------
  317. Date is: DateTime::__set_state(array(
  318. 'date' => '2000-12-30 19:04:05.000000',
  319. 'timezone_type' => 2,
  320. 'timezone' => 'PDT',
  321. ))
  322. ------------
  323. Formatted DateTime is : Saturday, December 30, 2000 5:04:05 PM GMT-10:00
  324. ------------
  325. Date is: DateTime::__set_state(array(
  326. 'date' => '2000-12-30 19:04:05.000000',
  327. 'timezone_type' => 2,
  328. 'timezone' => 'PDT',
  329. ))
  330. ------------
  331. Formatted DateTime is : December 30, 2000 5:04:05 PM GMT-10:00
  332. ------------
  333. Date is: DateTime::__set_state(array(
  334. 'date' => '2000-12-30 19:04:05.000000',
  335. 'timezone_type' => 2,
  336. 'timezone' => 'PDT',
  337. ))
  338. ------------
  339. Formatted DateTime is : Dec 30, 2000 5:04:05 PM
  340. ------------
  341. Date is: DateTime::__set_state(array(
  342. 'date' => '2000-12-30 19:04:05.000000',
  343. 'timezone_type' => 2,
  344. 'timezone' => 'PDT',
  345. ))
  346. ------------
  347. Formatted DateTime is : 12/30/00 5:04 PM
  348. ------------
  349. Date is: DateTime::__set_state(array(
  350. 'date' => '2000-12-30 19:04:05.000000',
  351. 'timezone_type' => 2,
  352. 'timezone' => 'PDT',
  353. ))
  354. ------------
  355. Formatted DateTime is : 20001230 05:04 PM
  356. ------------
  357. Date is: stdClass::__set_state(array(
  358. ))
  359. ------------
  360. Error while formatting as: 'datefmt_format: invalid object type for date/time (only IntlCalendar and DateTime permitted): U_ILLEGAL_ARGUMENT_ERROR'
  361. ------------
  362. Date is: stdClass::__set_state(array(
  363. ))
  364. ------------
  365. Error while formatting as: 'datefmt_format: invalid object type for date/time (only IntlCalendar and DateTime permitted): U_ILLEGAL_ARGUMENT_ERROR'
  366. ------------
  367. Date is: stdClass::__set_state(array(
  368. ))
  369. ------------
  370. Error while formatting as: 'datefmt_format: invalid object type for date/time (only IntlCalendar and DateTime permitted): U_ILLEGAL_ARGUMENT_ERROR'
  371. ------------
  372. Date is: stdClass::__set_state(array(
  373. ))
  374. ------------
  375. Error while formatting as: 'datefmt_format: invalid object type for date/time (only IntlCalendar and DateTime permitted): U_ILLEGAL_ARGUMENT_ERROR'
  376. ------------
  377. Date is: stdClass::__set_state(array(
  378. ))
  379. ------------
  380. Error while formatting as: 'datefmt_format: invalid object type for date/time (only IntlCalendar and DateTime permitted): U_ILLEGAL_ARGUMENT_ERROR'