Skip to content

Commit

Permalink
Fixed some formats
Browse files Browse the repository at this point in the history
  • Loading branch information
alphp committed Apr 2, 2022
1 parent 2fd690e commit 81956c3
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 19 deletions.
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,67 @@ You can install this plugin into your application using [composer](https://getco
\setlocale(LC_TIME, 'fr_FR.UTF-8');
echo \strftime('%A %e %B %Y %X', strtotime('2021-09-28 00:00:00'));
```

# Formats
## Day
| Format | Description | Example returned values |
|:------:|---------------------------------------------------------|-------------------------------------------------|
| `%a` | An abbreviated textual representation of the day | `Sun` through `Sat` |
| `%A` | A full textual representation of the day | `Sunday` through `Saturday` |
| `%d` | Two-digit day of the month (with leading zeros) | `01` to `31` |
| `%e` | Day of the month, with a space preceding single digits. | ` 1` to `31` |
| `%j` | Day of the year, 3 digits with leading zeros | `001` to `366` |
| `%u` | ISO-8601 numeric representation of the day of the week | `1` (for `Monday`) through `7` (for `Sunday`) |
| `%w` | Numeric representation of the day of the week | `0` (for `Sunday`) through `6` (for `Saturday`) |
## Week
| Format | Description | Example returned values |
|:------:|--------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------|
| `%U` | Week number of the given year, starting with the first Sunday as the first week | `13` (for the `13th full week of the year`) |
| `%V` | ISO-8601:1988 week number of the given year, starting withthe first week of the year with at least 4 weekdays, with Monday being the start of the week | `01` through `53` (where 53 accounts for an overlapping week) |
| `%W` | A numeric representation of the week of the year, startingwith the first Monday as the first week | `46` (for the `46th week of the year` beginning with a Monday) |
## Month
| Format | Description | Example returned values |
|:------:|----------------------------------------------------------------|----------------------------------------------------|
| `%b` | Abbreviated month name, based on the locale | `Jan` through `Dec` |
| `%B` | Full month name, based on the locale | `January` through `December` |
| `%h` | Abbreviated month name, based on the locale (an alias of `%b`) | `Jan` through `Dec` |
| `%m` | Two digit representation of the month | `01` (for `January`) through `12` (for `December`) |
## Year
| Format | Description | Example returned values |
|:------:|----------------------------------------------------------------------------------------|-------------------------------------------------|
| `%C` | Two digit representation of the century (year divided by 100, truncated to an integer) | `19` for the `20th Century` |
| `%g` | Two digit representation of the year going by ISO-8601:1988 standards (see %V) | Example: `09` for the week of `January 6, 2009` |
| `%G` | The full four-digit version of %g | Example: `2009` for the `January 3, 2009` |
| `%y` | Two digit representation of the year | Example: `09` for `2009`, `79` for `1979` |
| `%Y` | Four digit representation for the year | Example: `2038` |
## Time
| Format | Description | Example returned values |
|:------:|-------------------------------------------------------------------------------|--------------------------------------------------------|
| `%H` | Two digit representation of the hour in 24-hour format | `00` through `23` |
| `%k` | Hour in 24-hour format, with a space preceding single digits | `0` through `23` |
| `%I` | Two digit representation of the hour in 12-hour format | `01` through `12` |
| `%l` | (lower-case 'L') Hour in 12-hour format, with a space preceding single digits | ` 1` through `12` |
| `%M` | Two digit representation of the minute | `00` through `59` |
| `%p` | UPPER-CASE '`AM`' or '`PM`' based on the given time | Example: `AM` for `00:31`, `PM` for `22:23` |
| `%P` | lower-case '`am`' or '`pm`' based on the given time | Example: `am` for `00:31`, `pm` for `22:23` |
| `%r` | Same as "`%I:%M:%S %p`" | Example: `09:34:17 PM` for `21:34:17` |
| `%R` | Same as "`%H:%M`" | Example: `00:35` for `12:35 AM`, `16:44` for `4:44 PM` |
| `%S` | Two digit representation of the second | `00` through `59` |
| `%T` | Same as "`%H:%M:%S`" | Example: `21:34:17` for `09:34:17 PM` |
| `%X` | Preferred time representation based on locale, without the date | Example: `03:59:16` or `15:59:16` |
| `%z` | The time zone offset. | Example: `-0500` for `US Eastern Time` |
| `%Z` | The time zone abbreviation. | Example: `EST` for `Eastern Time` |
## Time and Date Stamps
| Format | Description | Example returned values |
|:------:|-----------------------------------------------------------------|--------------------------------------------------------------------------|
| `%c` | Preferred date and time stamp based on locale | Example: `Tue Feb 5 00:45:10 2009` for `February 5, 2009 at 12:45:10 AM` |
| `%D` | Same as "`%m/%d/%y`" | Example: `02/05/09` for `February 5, 2009` |
| `%F` | Same as "`%Y-%m-%d`" (commonly used in database datestamps) | Example: `2009-02-05` for `February 5, 2009` |
| `%s` | Unix Epoch Time timestamp (same as the `time()` function) | Example: `305815200` for `September 10, 1979 08:40:00 AM` |
| `%x` | Preferred date representation based on locale, without the time | Example: `02/05/09` for `February 5, 2009` |
## Miscellaneous
| Format | Description |
|:------:|--------------------------------------|
| `%n` | A newline character ("\n") |
| `%t` | A Tab character ("\t") |
| `%%` | A literal percentage character ("%") |
20 changes: 11 additions & 9 deletions src/php-8.1-strftime.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ function strftime (string $format, $timestamp = null, ?string $locale = null) :
'%b' => 'MMM', // Abbreviated month name, based on the locale Jan through Dec
'%B' => 'MMMM', // Full month name, based on the locale January through December
'%h' => 'MMM', // Abbreviated month name, based on the locale (an alias of %b) Jan through Dec
'%p' => 'aa', // UPPER-CASE 'AM' or 'PM' based on the given time Example: AM for 00:31, PM for 22:23
'%P' => 'aa', // lower-case 'am' or 'pm' based on the given time Example: am for 00:31, pm for 22:23
];

$intl_formatter = function (DateTimeInterface $timestamp, string $format) use ($intl_formats, $locale) {
Expand Down Expand Up @@ -96,7 +94,9 @@ function strftime (string $format, $timestamp = null, ?string $locale = null) :
'%a' => $intl_formatter,
'%A' => $intl_formatter,
'%d' => 'd',
'%e' => 'j',
'%e' => function ($timestamp) {
return sprintf('% 2u', $timestamp->format('j'));
},
'%j' => function ($timestamp) {
// Day number in year, 001 to 366
return sprintf('%03d', $timestamp->format('z')+1);
Expand Down Expand Up @@ -126,7 +126,7 @@ function strftime (string $format, $timestamp = null, ?string $locale = null) :
// Year
'%C' => function ($timestamp) {
// Century (-1): 19 for 20th century
return (int) $timestamp->format('Y') / 100;
return floor($timestamp->format('Y') / 100);
},
'%g' => function ($timestamp) {
return substr($timestamp->format('o'), -2);
Expand All @@ -139,15 +139,17 @@ function strftime (string $format, $timestamp = null, ?string $locale = null) :
'%H' => 'H',
'%k' => 'G',
'%I' => 'h',
'%l' => 'g',
'%l' => function ($timestamp) {
return sprintf('% 2u', $timestamp->format('g'));
},
'%M' => 'i',
'%p' => $intl_formatter, // AM PM (this is reversed on purpose!)
'%P' => $intl_formatter, // am pm
'%r' => 'G:i:s A', // %I:%M:%S %p
'%p' => 'A', // AM PM (this is reversed on purpose!)
'%P' => 'a', // am pm
'%r' => 'h:i:s A', // %I:%M:%S %p
'%R' => 'H:i', // %H:%M
'%S' => 's',
'%T' => 'H:i:s', // %H:%M:%S
'%X' => $intl_formatter,// Preferred time representation based on locale, without the date
'%X' => $intl_formatter, // Preferred time representation based on locale, without the date

// Timezone
'%z' => 'O',
Expand Down
158 changes: 148 additions & 10 deletions tests/strftimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,168 @@
use function PHP81_BC\strftime;

class strftimeTest extends TestCase {
public $str_date = '20220312 01:02:03';
public $int_date = null;

public function setUp () : void {
$this->int_date = strtotime($this->str_date);
setlocale(LC_TIME, 'en');
date_default_timezone_set('Europe/Madrid');
}

public function test_int_timestamp () {
$result = strftime('%Y-%m-%d %H:%M:%S', $this->int_date);
$this->assertEquals('2022-03-12 01:02:03', $result);
$result = strftime('%Y-%m-%d %H:%M:%S', strtotime('20220306 01:02:03'));
$this->assertEquals('2022-03-06 01:02:03', $result);
}

public function test_string_timestamp () {
$result = strftime('%Y-%m-%d %H:%M:%S', $this->str_date);
$this->assertEquals('2022-03-12 01:02:03', $result);
$result = strftime('%Y-%m-%d %H:%M:%S', '20220306 01:02:03');
$this->assertEquals('2022-03-06 01:02:03', $result);
}

public function test_datetime_timestamp () {
$result = strftime('%Y-%m-%d %H:%M:%S', new DateTime($this->str_date));
$this->assertEquals('2022-03-12 01:02:03', $result);
$result = strftime('%Y-%m-%d %H:%M:%S', new DateTime('20220306 01:02:03'));
$this->assertEquals('2022-03-06 01:02:03', $result);
}

public function test_exception () {
$this->expectException(InvalidArgumentException::class);
$result = strftime('%Y-%m-%d %H:%M:%S', 'InvalidArgumentException');

$this->expectException(InvalidArgumentException::class);
$result = strftime('', '20220306 13:02:03');
}

public function test_formats_day () {
$result = strftime('%a', '20220306 13:02:03');
$this->assertEquals('Sun', $result);

$result = strftime('%A', '20220306 13:02:03');
$this->assertEquals('Sunday', $result);

$result = strftime('%d', '20220306 13:02:03');
$this->assertEquals('06', $result);

$result = strftime('%e', '20220306 13:02:03');
$this->assertEquals(' 6', $result);

$result = strftime('%j', '20220306 13:02:03');
$this->assertEquals('065', $result);

$result = strftime('%u', '20220306 13:02:03');
$this->assertEquals('7', $result);

$result = strftime('%w', '20220306 13:02:03');
$this->assertEquals('0', $result);
}

public function test_formats_week () {
$result = strftime('%U', '20220306 13:02:03');
$this->assertEquals('9', $result);

$result = strftime('%V', '20220306 13:02:03');
$this->assertEquals('09', $result);

$result = strftime('%W', '20220306 13:02:03');
$this->assertEquals('8', $result);
}

public function test_formats_month () {
$result = strftime('%b', '20220306 13:02:03');
$this->assertEquals('Mar', $result);

$result = strftime('%B', '20220306 13:02:03');
$this->assertEquals('March', $result);

$result = strftime('%h', '20220306 13:02:03');
$this->assertEquals('Mar', $result);

$result = strftime('%m', '20220306 13:02:03');
$this->assertEquals('03', $result);
}

public function test_formats_year () {
$result = strftime('%C', '20220306 13:02:03');
$this->assertEquals('20', $result);

$result = strftime('%g', '20220306 13:02:03');
$this->assertEquals('22', $result);

$result = strftime('%G', '20220306 13:02:03');
$this->assertEquals('2022', $result);

$result = strftime('%y', '20220306 13:02:03');
$this->assertEquals('22', $result);

$result = strftime('%Y', '20220306 13:02:03');
$this->assertEquals('2022', $result);
}

public function test_formats_time () {
$result = strftime('%H', '20220306 13:02:03');
$this->assertEquals('13', $result);

$result = strftime('%k', '20220306 01:02:03');
$this->assertEquals('1', $result);

$result = strftime('%I', '20220306 13:02:03');
$this->assertEquals('01', $result);

$result = strftime('%l', '20220306 13:02:03');
$this->assertEquals(' 1', $result);

$result = strftime('%M', '20220306 13:02:03');
$this->assertEquals('02', $result);

$result = strftime('%p', '20220306 13:02:03');
$this->assertEquals('PM', $result);

$result = strftime('%P', '20220306 13:02:03');
$this->assertEquals('pm', $result);

$result = strftime('%r', '20220306 13:02:03');
$this->assertEquals('01:02:03 PM', $result);

$result = strftime('%R', '20220306 13:02:03');
$this->assertEquals('13:02', $result);

$result = strftime('%S', '20220306 13:02:03');
$this->assertEquals('03', $result);

$result = strftime('%T', '20220306 13:02:03');
$this->assertEquals('13:02:03', $result);

$result = strftime('%X', '20220306 13:02:03');
$this->assertEquals('1:02:03 PM', $result);

$result = strftime('%z', '20220306 13:02:03');
$this->assertEquals('+0100', $result);

$result = strftime('%Z', '20220306 13:02:03');
$this->assertEquals('CET', $result);
}

public function test_formats_stamps () {
$result = strftime('%c', '20220306 13:02:03');
$this->assertEquals('March 6, 2022 at 1:02 PM', $result);

$result = strftime('%D', '20220306 13:02:03');
$this->assertEquals('03/06/2022', $result);

$result = strftime('%F', '20220306 13:02:03');
$this->assertEquals('2022-03-06', $result);

$result = strftime('%s', '20220306 13:02:03');
$this->assertEquals('1646568123', $result);

$result = strftime('%x', '20220306 13:02:03');
$this->assertEquals('3/6/22', $result);
}

public function test_formats_miscellaneous () {
$result = strftime('%n', '20220306 13:02:03');
$this->assertEquals("\n", $result);

$result = strftime('%t', '20220306 13:02:03');
$this->assertEquals("\t", $result);

$result = strftime('%%', '20220306 13:02:03');
$this->assertEquals('%', $result);
}
}

0 comments on commit 81956c3

Please sign in to comment.