|
4 | 4 |
|
5 | 5 | use Safe\Exceptions\DatetimeException;
|
6 | 6 |
|
| 7 | +/** |
| 8 | + * Returns associative array with detailed info about given date. |
| 9 | + * |
| 10 | + * @param string $format Format accepted by DateTime::createFromFormat. |
| 11 | + * @param string $date String representing the date. |
| 12 | + * @return array Returns associative array with detailed info about given date. |
| 13 | + * @throws DatetimeException |
| 14 | + * |
| 15 | + */ |
| 16 | +function date_parse_from_format(string $format, string $date): array |
| 17 | +{ |
| 18 | + error_clear_last(); |
| 19 | + $result = \date_parse_from_format($format, $date); |
| 20 | + if ($result === false) { |
| 21 | + throw DatetimeException::createFromPhpError(); |
| 22 | + } |
| 23 | + return $result; |
| 24 | +} |
| 25 | + |
| 26 | + |
7 | 27 | /**
|
8 | 28 | *
|
9 | 29 | *
|
@@ -383,6 +403,115 @@ function mktime(int $hour = null, int $minute = null, int $second = null, int $m
|
383 | 403 | }
|
384 | 404 |
|
385 | 405 |
|
| 406 | +/** |
| 407 | + * strptime returns an array with the |
| 408 | + * date parsed, . |
| 409 | + * |
| 410 | + * Month and weekday names and other language dependent strings respect the |
| 411 | + * current locale set with setlocale (LC_TIME). |
| 412 | + * |
| 413 | + * @param string $date The string to parse (e.g. returned from strftime). |
| 414 | + * @param string $format The format used in date (e.g. the same as |
| 415 | + * used in strftime). Note that some of the format |
| 416 | + * options available to strftime may not have any |
| 417 | + * effect within strptime; the exact subset that are |
| 418 | + * supported will vary based on the operating system and C library in |
| 419 | + * use. |
| 420 | + * |
| 421 | + * For more information about the format options, read the |
| 422 | + * strftime page. |
| 423 | + * @return array Returns an array . |
| 424 | + * |
| 425 | + * |
| 426 | + * The following parameters are returned in the array |
| 427 | + * |
| 428 | + * |
| 429 | + * |
| 430 | + * parameters |
| 431 | + * Description |
| 432 | + * |
| 433 | + * |
| 434 | + * |
| 435 | + * |
| 436 | + * "tm_sec" |
| 437 | + * Seconds after the minute (0-61) |
| 438 | + * |
| 439 | + * |
| 440 | + * "tm_min" |
| 441 | + * Minutes after the hour (0-59) |
| 442 | + * |
| 443 | + * |
| 444 | + * "tm_hour" |
| 445 | + * Hour since midnight (0-23) |
| 446 | + * |
| 447 | + * |
| 448 | + * "tm_mday" |
| 449 | + * Day of the month (1-31) |
| 450 | + * |
| 451 | + * |
| 452 | + * "tm_mon" |
| 453 | + * Months since January (0-11) |
| 454 | + * |
| 455 | + * |
| 456 | + * "tm_year" |
| 457 | + * Years since 1900 |
| 458 | + * |
| 459 | + * |
| 460 | + * "tm_wday" |
| 461 | + * Days since Sunday (0-6) |
| 462 | + * |
| 463 | + * |
| 464 | + * "tm_yday" |
| 465 | + * Days since January 1 (0-365) |
| 466 | + * |
| 467 | + * |
| 468 | + * "unparsed" |
| 469 | + * the date part which was not |
| 470 | + * recognized using the specified format |
| 471 | + * |
| 472 | + * |
| 473 | + * |
| 474 | + * |
| 475 | + * @throws DatetimeException |
| 476 | + * |
| 477 | + */ |
| 478 | +function strptime(string $date, string $format): array |
| 479 | +{ |
| 480 | + error_clear_last(); |
| 481 | + $result = \strptime($date, $format); |
| 482 | + if ($result === false) { |
| 483 | + throw DatetimeException::createFromPhpError(); |
| 484 | + } |
| 485 | + return $result; |
| 486 | +} |
| 487 | + |
| 488 | + |
| 489 | +/** |
| 490 | + * Each parameter of this function uses the default time zone unless a |
| 491 | + * time zone is specified in that parameter. Be careful not to use |
| 492 | + * different time zones in each parameter unless that is intended. |
| 493 | + * See date_default_timezone_get on the various |
| 494 | + * ways to define the default time zone. |
| 495 | + * |
| 496 | + * @param string $time A date/time string. Valid formats are explained in Date and Time Formats. |
| 497 | + * @param int $now The timestamp which is used as a base for the calculation of relative |
| 498 | + * dates. |
| 499 | + * @return int Returns a timestamp on success, FALSE otherwise. Previous to PHP 5.1.0, |
| 500 | + * this function would return -1 on failure. |
| 501 | + * @throws DatetimeException |
| 502 | + * |
| 503 | + */ |
| 504 | +function strtotime(string $time, int $now = null): int |
| 505 | +{ |
| 506 | + error_clear_last(); |
| 507 | + $result = \strtotime($time, $now); |
| 508 | + if ($result === false) { |
| 509 | + throw DatetimeException::createFromPhpError(); |
| 510 | + } |
| 511 | + return $result; |
| 512 | +} |
| 513 | + |
| 514 | + |
386 | 515 | /**
|
387 | 516 | *
|
388 | 517 | *
|
|
0 commit comments