[ Index ] |
|
Code source de Serendipity 1.2 |
1 <?php # $Id: functions.inc.php 114 2005-05-22 15:37:11Z garvinhicking $ 2 # Copyright (c) 2003-2005, Jannis Hermanns (on behalf the Serendipity Developer Team) 3 # All rights reserved. See LICENSE file for licensing details 4 5 if (IN_serendipity !== true) { 6 die ("Don't hack!"); 7 } 8 9 if (defined('S9Y_FRAMEWORK_CALENDARS')) { 10 return; 11 } 12 @define('S9Y_FRAMEWORK_CALENDARS', true); 13 14 /** 15 * Gregorian to Persian Convertor 16 * 17 * @author farsiweb.info 18 * @access public 19 * @param int year 20 * @param int month 21 * @param int day 22 * @return array converted time 23 */ 24 function g2p($g_y, $g_m, $g_d){ 25 $g_days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 26 $j_days_in_month = array(31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29); 27 28 $gy = $g_y-1600; 29 $gm = $g_m-1; 30 $gd = $g_d-1; 31 32 $g_day_no = 365*$gy+floor(($gy+3)/4)-floor(($gy+99)/100)+floor(($gy+399)/400); 33 34 for ($i=0; $i < $gm; ++$i){ 35 $g_day_no += $g_days_in_month[$i]; 36 } 37 38 if ($gm>1 && (($gy%4==0 && $gy%100!=0) || ($gy%400==0))){ 39 /* leap and after Feb */ 40 ++$g_day_no; 41 } 42 43 $g_day_no += $gd; 44 $j_day_no = $g_day_no-79; 45 $j_np = floor($j_day_no/12053); 46 $j_day_no %= 12053; 47 $jy = 979+33*$j_np+4*floor($j_day_no/1461); 48 $j_day_no %= 1461; 49 50 if ($j_day_no >= 366) { 51 $jy += floor(($j_day_no-1)/365); 52 $j_day_no = ($j_day_no-1)%365; 53 } 54 $j_all_days = $j_day_no+1; 55 56 for ($i = 0; $i < 11 && $j_day_no >= $j_days_in_month[$i]; ++$i) { 57 $j_day_no -= $j_days_in_month[$i]; 58 } 59 60 $jm = $i+1; 61 $jd = $j_day_no+1; 62 63 return array($jy, $jm, $jd, $j_all_days); 64 } 65 66 /** 67 * Persian to Gregorian Convertor 68 * 69 * @author farsiweb.info 70 * @access public 71 * @param int year 72 * @param int month 73 * @param int day 74 * @return array converted time 75 */ 76 function p2g($j_y, $j_m, $j_d){ 77 $g_days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 78 $j_days_in_month = array(31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29); 79 $jy = $j_y-979; 80 $jm = $j_m-1; 81 $jd = $j_d-1; 82 $j_day_no = 365*$jy + floor($jy/33)*8 + floor(($jy%33+3)/4); 83 for ($i=0; $i < $jm; ++$i){ 84 $j_day_no += $j_days_in_month[$i]; 85 } 86 $j_day_no += $jd; 87 $g_day_no = $j_day_no+79; 88 $gy = 1600 + 400*floor($g_day_no/146097); 89 $g_day_no = $g_day_no % 146097; 90 $leap = true; 91 if ($g_day_no >= 36525){ 92 $g_day_no--; 93 $gy += 100*floor($g_day_no/36524); 94 $g_day_no = $g_day_no % 36524; 95 if ($g_day_no >= 365){ 96 $g_day_no++; 97 }else{ 98 $leap = false; 99 } 100 } 101 $gy += 4*floor($g_day_no/1461); 102 $g_day_no %= 1461; 103 if ($g_day_no >= 366){ 104 $leap = false; 105 $g_day_no--; 106 $gy += floor($g_day_no/365); 107 $g_day_no = $g_day_no % 365; 108 } 109 for ($i = 0; $g_day_no >= $g_days_in_month[$i] + ($i == 1 && $leap); $i++){ 110 $g_day_no -= $g_days_in_month[$i] + ($i == 1 && $leap); 111 } 112 $gm = $i+1; 113 $gd = $g_day_no+1; 114 115 return array($gy, $gm, $gd); 116 } 117 118 /** 119 * Format a string according to Persian calendar (UTF) 120 * 121 * @author Omid Mottaghi 122 * @access public 123 * @param string Formatting string 124 * @param int Timestamp to format 125 * @return string Formatted local time/date according to locale settings 126 */ 127 function persian_strftime_utf($format, $timestamp='') { 128 129 if($timestamp==''){ 130 $timestamp = mktime(); 131 } 132 133 $g_d=date('j', $timestamp); 134 $g_m=date('n', $timestamp); 135 $g_y=date('Y', $timestamp); 136 137 list($jy, $jm, $jd, $j_all_days) = g2p($g_y, $g_m, $g_d); 138 139 $j_month_name = array('', 'فروردین', 'اردیبهشت', 'خرداد', 'تیر', 140 'مرداد', 'شهریور', 'مهر', 'آبان', 'آذر', 'دی', 'بهمن', 'اسفند'); 141 $j_week_name = array('Saturday' => 'شنبه', 142 'Sunday' => 'یک شنبه', 143 'Monday' => 'دوشنبه', 144 'Tuesday' => 'سه شنبه', 145 'Wednesday' => 'چهارشنبه', 146 'Thursday' => 'پنج شنبه', 147 'Friday' => 'جمعه', 148 'Sat' => 'ش', 149 'Sun' => 'ی', 150 'Mon' => 'د', 151 'Tue' => 'س', 152 'Wed' => 'چ', 153 'Thu' => 'پ', 154 'Fri' => 'ج'); 155 $j_week_number = array('Sat' => '1', 156 'Sun' => '2', 157 'Mon' => '3', 158 'Tue' => '4', 159 'Wed' => '5', 160 'Thu' => '6', 161 'Fri' => '7'); 162 163 // calculate string 164 $output_str=''; 165 166 for ($i=0; $i<strlen($format); $i++){ 167 168 if($format[$i]=='%'){ 169 $i++; 170 switch($format[$i]){ 171 case 'a': 172 $output_str.=$j_week_name[date('D', $timestamp)]; 173 break; 174 case 'A': 175 $output_str.=$j_week_name[date('l', $timestamp)]; 176 break; 177 case 'b': 178 case 'B': 179 case 'h': 180 $output_str.=$j_month_name[$jm]; 181 break; 182 case 'c': 183 $output_str.=persian_strftime_utf('%y/%m/%d %I:%M:%S', $timestamp); 184 break; 185 case 'C': 186 $output_str.=floor($jy/100); 187 break; 188 case 'd': 189 if($jd<10) $output_str.='0'.$jd; else $output_str.=$jd; 190 break; 191 case 'D': 192 $output_str.=$jy.'/'.$jm.'/'.$jd; 193 break; 194 case 'e': 195 if($jd<10) $output_str.=' '.$jd; else $output_str.=$jd; 196 break; 197 case 'H': 198 $output_str.=date('H', $timestamp); 199 break; 200 case 'I': 201 $output_str.=date('h', $timestamp); 202 break; 203 case 'j': 204 $output_str.=sprintf('%03d', $j_all_days); 205 break; 206 case 'm': 207 if($jm<10) $output_str.='0'.$jm; else $output_str.=$jm; 208 break; 209 case 'M': 210 $output_str.=date('i', $timestamp); 211 break; 212 case 'n': 213 $output_str.="\n"; 214 break; 215 case 'r': 216 case 'p': 217 if(date('a',$timestamp)=='pm') $output_str.='بعد از ظهر'; else $output_str.='قبل از ظهر'; 218 break; 219 case 'R': 220 $output_str.=strftime('%R', $timestamp); 221 break; 222 case 'S': 223 $output_str.=date('s', $timestamp); 224 break; 225 case 't': 226 $output_str.="\t"; 227 break; 228 case 'U': 229 case 'V': 230 case 'W': 231 $output_str.=sprintf('%02d', floor(($j_all_days+1)/7)); 232 break; 233 case 'u': 234 case 'w': 235 $output_str.=$j_week_number[date('D', $timestamp)]; 236 break; 237 case 'x': 238 $output_str.=persian_strftime_utf('%y/%m/%d', $timestamp); 239 break; 240 case 'X': 241 $output_str.=persian_strftime_utf('%I:%M:%S', $timestamp); 242 break; 243 case 'g': 244 case 'y': 245 $output_str.=$jy-(floor($jy/100)*100); 246 break; 247 case 'G': 248 case 'Y': 249 $output_str.=$jy; 250 break; 251 case 'z': 252 case 'Z': 253 $output_str.=strftime('%z', $timestamp); 254 break; 255 case '%': 256 $output_str.='%'; 257 break; 258 } 259 }else{ 260 $output_str.=$format[$i]; 261 } 262 } 263 264 return $output_str; 265 } 266 267 /** 268 * Format a string according to Persian calendar (UTF) 269 * 270 * @author Omid Mottaghi 271 * @access public 272 * @param string Formatting string 273 * @param int Timestamp to format 274 * @return string Formatted local time/date 275 */ 276 function persian_date_utf($format, $timestamp='') { 277 278 if($timestamp==''){ 279 $timestamp = mktime(); 280 } 281 282 $g_d=date('j', $timestamp); 283 $g_m=date('n', $timestamp); 284 $g_y=date('Y', $timestamp); 285 286 list($jy, $jm, $jd, $j_all_days) = g2p($g_y, $g_m, $g_d); 287 288 $j_days_in_month = array(0, 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29); 289 $leap = 0; 290 if ($g_m>1 && (($g_y%4==0 && $g_y%100!=0) || ($g_y%400==0))){ 291 $j_days_in_month[12]++; 292 $leap = 1; 293 } 294 295 $j_month_name = array('', 'فروردین', 'اردیبهشت', 'خرداد', 'تیر', 296 'مرداد', 'شهریور', 'مهر', 'آبان', 'آذر', 'دی', 'بهمن', 'اسفند'); 297 $j_week_name = array('Saturday' => 'شنبه', 298 'Sunday' => 'یک شنبه', 299 'Monday' => 'دوشنبه', 300 'Tuesday' => 'سه شنبه', 301 'Wednesday' => 'چهارشنبه', 302 'Thursday' => 'پنج شنبه', 303 'Friday' => 'جمعه', 304 'Sat' => 'ش', 305 'Sun' => 'ی', 306 'Mon' => 'د', 307 'Tue' => 'س', 308 'Wed' => 'چ', 309 'Thu' => 'پ', 310 'Fri' => 'ج'); 311 $j_week_number = array('Sat' => '1', 312 'Sun' => '2', 313 'Mon' => '3', 314 'Tue' => '4', 315 'Wed' => '5', 316 'Thu' => '6', 317 'Fri' => '7'); 318 319 // calculate string 320 $output_str=''; 321 322 for ($i=0; $i<strlen($format); $i++){ 323 324 if($format[$i]!='\\'){ 325 switch($format[$i]){ 326 case 'd': 327 if($jd<10) $output_str.='0'.$jd; else $output_str.=$jd; 328 break; 329 case 'j': 330 $output_str.=$jd; 331 break; 332 case 'D': 333 case 'S': 334 $output_str.=$j_week_name[date('D', $timestamp)]; 335 break; 336 case 'l': 337 $output_str.=$j_week_name[date('l', $timestamp)]; 338 break; 339 case 'w': 340 case 'N': 341 $output_str.=$j_week_number[date('D', $timestamp)]; 342 break; 343 case 'z': 344 $output_str.=sprintf('%03d', $j_all_days); 345 break; 346 case 'W': 347 $output_str.=floor(($j_all_days+1)/7); 348 break; 349 case 'F': 350 case 'M': 351 $output_str.=$j_month_name[$jm]; 352 break; 353 case 'm': 354 if($jm<10) $output_str.='0'.$jm; else $output_str.=$jm; 355 break; 356 case 'n': 357 $output_str.=$jm; 358 break; 359 case 't': 360 $output_str.=$j_days_in_month[$jm]; 361 break; 362 case 'L': 363 $output_str.=$leap; 364 break; 365 case 'o': 366 case 'Y': 367 $output_str.=$jy; 368 break; 369 case 'y': 370 $output_str.=$jy-(floor($jy/100)*100); 371 break; 372 case 'a': 373 case 'A': 374 if(date('a', $timestamp)=='pm') $output_str.='بعد از ظهر'; else $output_str.='قبل از ظهر'; 375 break; 376 case 'B': 377 $output_str.=date('B', $timestamp); 378 break; 379 case 'g': 380 $output_str.=date('g', $timestamp); 381 break; 382 case 'G': 383 $output_str.=date('G', $timestamp); 384 break; 385 case 'h': 386 $output_str.=date('h', $timestamp); 387 break; 388 case 'H': 389 $output_str.=date('H', $timestamp); 390 break; 391 case 'i': 392 $output_str.=date('i', $timestamp); 393 break; 394 case 's': 395 $output_str.=date('s', $timestamp); 396 break; 397 case 'e': 398 $output_str.=date('e', $timestamp); 399 break; 400 case 'I': 401 $output_str.=date('I', $timestamp); 402 break; 403 case 'O': 404 $output_str.=date('O', $timestamp); 405 break; 406 case 'Z': 407 $output_str.=date('Z', $timestamp); 408 break; 409 case 'c': 410 $output_str.=persian_date_utf('d-m-Y\TH:i:sO', $timestamp); 411 break; 412 case 'r': 413 $output_str.=persian_date_utf('D، j F Y H:i:s O', $timestamp); 414 break; 415 case 'U': 416 $output_str.=date('U', $timestamp); 417 break; 418 default: 419 $output_str.=$format[$i]; 420 break; 421 } 422 }else{ 423 $i++; 424 $output_str.=$format[$i]; 425 } 426 } 427 428 return $output_str; 429 } 430 431 /** 432 * Create a Unix timestamp for a Persian date 433 * This function works only with day > 0 434 * 435 * @author Omid Mottaghi 436 * @access public 437 * @param int hour 438 * @param int minute 439 * @param int second 440 * @param int month 441 * @param int day 442 * @param int year 443 * @param int is daylight savings time set? 444 * @return int returned timestamp 445 */ 446 function persian_mktime($hour='', $min='', $sec='', $mon='', $day='', $year='', $is_dst=-1){ 447 $j_days_in_month = array(31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29); 448 449 if ( (string) $hour == '') { $hour = persian_date_utf('H'); } 450 if ( (string) $min == '') { $min = persian_date_utf('i'); } 451 if ( (string) $sec == '') { $sec = persian_date_utf('s'); } 452 if ( (string) $day == '') { $day = persian_date_utf('j'); } 453 if ( (string) $mon == '') { $mon = persian_date_utf('n'); } 454 if ( (string) $year == '') { $year = persian_date_utf('Y'); } 455 456 /* 457 an ugly, beta code snippet to support days <= zero! 458 it should work, but days in one or more months should calculate! 459 */ 460 461 /* 462 if($day <= 0){ 463 // change sign 464 $day = abs($day); 465 466 // calculate months and days that shall decrease 467 // this do-while has a lot of errors!!! 468 do{ 469 // $month_days = $j_days_in_month[$mon] 470 $months = floor($day/30); 471 $days = $day % 30; 472 }while(); 473 474 $mon -= $months; 475 $day -= $days; 476 if ($day < 1) { 477 $mon--; 478 } 479 } 480 */ 481 482 if($mon <= 0){ 483 // change sign 484 $mon = abs($mon); 485 486 // calculate years and months that shall decrease 487 $years = floor($mon/12); 488 $months = $mon % 12; 489 490 $year -= $years; 491 $mon -= $months; 492 if ($mon < 1) { 493 $year--; 494 $mon += 12; 495 } 496 } 497 498 if ($day < 1) { 499 $temp_month = $mon-1; 500 $temp_year = $year; 501 if($temp_month <= 0){ 502 $temp_month = 12; 503 $temp_year--; 504 } 505 if ($temp_month>1 && (($temp_year%4==0 && $temp_year%100!=0) || ($temp_year%400==0))){ 506 $j_days_in_month[12] = 30; 507 }else{ 508 $j_days_in_month[12] = 29; 509 } 510 $day += $j_days_in_month[$temp_month]; 511 } 512 513 list($year, $mon, $day)=p2g($year, $mon, $day); 514 return mktime($hour, $min, $sec, $mon, $day, $year, $is_dst); 515 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Gnr le : Sat Nov 24 09:00:37 2007 | par Balluche grce PHPXref 0.7 |
![]() |