Giter Club home page Giter Club logo

strftime's Introduction

GitHub Workflow GitHub license GitHub release Packagist Packagist Downloads GitHub issues GitHub forks GitHub stars

strftime

Locale-formatted strftime using IntlDateFormatter (PHP 8.1 compatible)

This provides a cross-platform alternative to strftime() for when it will be removed from PHP.

Note that output can be slightly different between libc sprintf and this function as it is using ICU.

Original code: https://gist.github.com/bohwaz/42fc223031e2b2dd2585aab159a20f30

Original autor: BohwaZ

Table of contents

Requirements

TOC

Installation

Composer install

You can install this plugin into your application using composer:

  • Add php81_bc/strftime package to your project:

      composer require php81_bc/strftime
  • Load the function PHP81_BC\strftime in your project

    <?php
      require 'vendor/autoload.php';
      use function PHP81_BC\strftime;

TOC

Manual install

  • Download php-8.1-strftime.php and save it to an accessible path of your project.
  • Load the function PHP81_BC\strftime in your project
    <?php
      require 'php-8.1-strftime.php';
      use function PHP81_BC\strftime;

TOC

Usage

  use function PHP81_BC\strftime;
  echo strftime('%A %e %B %Y %X', new \DateTime('2021-09-28 00:00:00'), 'fr_FR');

TOC

Original use

  \setlocale(LC_TIME, 'fr_FR.UTF-8');
  echo \strftime('%A %e %B %Y %X', strtotime('2021-09-28 00:00:00'));

TOC

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)

TOC

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, starting with the first Monday as the first week 46 (for the 46th week of the year beginning with a Monday)

NOTE: All week formats are two-digit, with leading zeros.

TOC

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)

TOC

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

TOC

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

TOC

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

TOC

Miscellaneous

Format Description
%n A newline character ("\n")
%t A Tab character ("\t")
%% A literal percentage character ("%")

TOC

strftime's People

Contributors

alexgit2k avatar alphp avatar asmecher avatar imsop avatar maqc1 avatar mkortleven-emg avatar splitbrain avatar the-mars avatar tiberiud avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

strftime's Issues

doesn't work with LC_TIME=nb_NO.UTF8 ?

my LC_TIME is "nb_NO.UTF8", and $format is "%A %d. %B" and $timestamp is 1655935200 and strftime() gives me "torsdag 23. juni", but \PHP81_BC\strftime gives me "Thursday 23. June"

.. any idea what went wrong here? reproduce script:

<?php
require_once('vendor/autoload.php');
if(false === setlocale(LC_TIME, "nb_NO.UTF8")){
 die("error: locale nb_NO.UTF8 is not installed");
}
$stamp = 1655935200;
$format = "%A %d. %B";
var_dump(\PHP81_BC\strftime($format, $stamp));
var_dump(strftime($format, $stamp));

outputs on my system (PHP7.4.29):

root@75cee9b6f638:/var/www# php test.php
string(17) "Thursday 23. June"
string(16) "torsdag 23. juni"

what went wrong here?

Behaves differently from stock `strftime` for the Italian default year format

Hi, it seems that \PHP81_BC\strftime behaves differently from stock strftime for the Italian default year format

<?php
require_once "vendor/autoload.php";

$dt = new \DateTime("2022-01-01 00:00:00");

setlocale(LC_TIME, 'it_IT.utf8');
var_dump(strftime("%x %X", $dt->getTimestamp()));
var_dump(\PHP81_BC\strftime("%x %X", $dt->getTimestamp()));

outputs

string(19) "01/01/2022 00:00:00"
string(17) "01/01/22 00:00:00"

Fatal error when using the default PHP locale

The code (new IntlDateFormatter('C'))->format(new DateTime()) generates a fatal error on the latest PHP 8.x releases, see a reproduction case here and the exception below:

PHP Fatal error: Uncaught Error: Found unconstructed IntlDateFormatter in /var/www/html/vendor/php81_bc/strftime/src/php-8.1-strftime.php:100

"Stand-alone" versions of "month in year" and "day of week" formats more closely mimic strftime in German locale

Currently this library uses EEE and MMM for %a and %b. However I noticed with my own project's test suite that in the German locale that the "stand-alone" formats ccc and LLL more closely mimic what strftime does for %a and %b.

In particular EEE and MMM formats do not always truncate to 3 characters in the German locale (they sometimes generate 2 or 4 characters). They also add a period . after the truncated part which strftime does not do.

Switching to ccc and LLL has no effect on the English and Spanish locales in my test suite.

Looking at what "stand-alone" is for (https://unicode-org.github.io/icu/userguide/format_parse/datetime/#datetime-format-syntax) I think it is more correct to use the stand-alone formats in a strftime polyfill. While using EEE and MMM would potentially improve the output grammar when the format string combines multiple date parts, you'd have to look at the format string as a whole to know if you need to use the stand-alone versions or not. And at least my libc's strftime does not do this.

PSR-12 compability?

I noticed the code is currently using a somewhat uncommon formatting (partly enforced via editor config). Would it make sense to adopt PSR-12? I'm happy to provide a pull request if wanted.

Replace "is_int" check with "is_numeric"?

Hi!

I believe that this code:

 $timestamp = is_int($timestamp) ? '@' . $timestamp : (string) $timestamp;

should better be:

$timestamp = is_numeric($timestamp) ? '@' . $timestamp : (string) $timestamp;

because there might be places where a "string" like "168023423" is passed (typed as a string), but DateTime constructor will then complain if it's passed as a string. is_numeric() would be the better choice to see if a variable only contains numbers...?

Regards,
Garvin

Minimum PHP version in composer.json is incorrect

Currently, the composer.json lists a minimum PHP version of 5.6, but the actual code includes many features which require later versions, such as a type declaration of ?string, which is only valid since PHP 7.1

Either the documented minimum version should be updated, or the code changed to run on older versions.

PHP fatal error "Call to undefined method IntlCalendar::setGregorianChange()" with non-Gregorian locales

Try the following test script. (You may have to generate the fa_IR locale on your system first.)

<?php

setlocale(LC_ALL, 'fa_IR') || die("Could not set locale!\n");
require_once('autoload.php');

echo PHP81_BC\strftime('%b', time());

You will get a fatal error message:

PHP Fatal error:  Uncaught Error: Call to undefined method IntlCalendar::setGregorianChange() in /path/to/test.php:5
Stack trace:
#0 {main}
  thrown in /path/to/test.php on line 5

This is because IntlGregorianCalendar::createInstance() will not return an IntlGregorianCalendar when the locale is set to something non-Gregorian. (Note that the error message says it's an IntlCalendar.)

Pull request: #16

(Source: https://forum.pkp.sfu.ca/t/submission-files-is-not-showing-in-ojs-3-3-0-11/83976/10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.