Code Monkey home page Code Monkey logo

international-date-format's Introduction

International Date Formatter Helper

By Amburi Roy

IntlDateFormatterHelper is a PHP class designed to parse and reformat dates from various regions and locales. This helper allows you to handle dates in different languages and formats, making it easier to work with international date formats.

Prerequisites

Ensure you have the following installed on your system:

  • PHP 7.4.33
  • Composer version 2.4.2
  • PHPUnit 9.5.10

Setup

Step 1: Install Dependencies

First, you need to install all required dependencies using Composer. Open your terminal and navigate to the project directory, then run:

composer install

Usage

Example 1: Convert Dates from German to English

Let's convert a date written in German to English. For instance, the date in German is 'So., 01.01.2023', and we want to convert it to 'Sunday 1st of January 2023'.

  1. Define the date, locale, input date format, and output date format:

    $date = 'So., 01.01.2023';
    $locale = Locales::DE_LOCALE;
    $inputDateFormat = 'EEEEEE., d.m.y';
    $outputDateFormat = 'l jS \of F Y';
  2. Use IntlDateFormatterHelper to convert the date:

    $intlDateFormat = new IntlDateFormatterHelper($locale);
    $enDate = $intlDateFormat->formatStrToStr($date, $inputDateFormat, $outputDateFormat);
    echo 'From '. $date .' to '. $enDate . "<br />";

Example 2: Convert Dates from French to English

Now, let's convert a date from French to English. For instance, the date in French is 'Lundi, 08 Juillet 2013 09:09', and we want to convert it to 'Monday 8th of July 2013 09:09:00 AM'.

  1. Define the date, locale, input date format, and output date format:

    $date = 'Lundi, 08 Juillet 2013 09:09';
    $locale = Locales::FR_LOCALE;
    $inputDateFormat = 'EEEE, dd MMMM y hh:mm';
    $outputDateFormat = 'l jS \of F Y h:i:s A';
  2. Use IntlDateFormatterHelper to convert the date:

    $intlDateFormat = new IntlDateFormatterHelper($locale);
    $enDate = $intlDateFormat->formatStrToStr($date, $inputDateFormat, $outputDateFormat);
    echo 'From '. $date .' to '. $enDate . "<br />";

Output

When you run the above code, you should see the following output:

Output

Additional Methods

The helper also includes the parseTimestamp() and parseDate() methods. Use these methods as needed to handle other date parsing and formatting tasks. You can extend the Locales interface to add more locales.

Running Tests

To execute all tests, run the following command:

phpunit src/tests

Alternatively, you can use:

./vendor/bin/phpunit src/tests

Running the Project

To execute index.php locally on port 8080, run:

php -S localhost:8080 -t .

You should see a message indicating that the PHP development server has started:

[Fri Dec 30 01:41:07 2022] PHP 7.4.33 Development Server (http://localhost:8080) started

Technologies Used

  • PHP 7.4.33
  • Composer version 2.4.2
  • PHPUnit 9.5.10

This project uses simple PHP functions, making it easy to integrate into various PHP frameworks.

Conclusion

By following this tutorial, you should be able to set up and use the IntlDateFormatterHelper to handle international date formats effectively. Feel free to customize and extend the functionality to suit your specific needs.

international-date-format's People

Contributors

amburi avatar

Watchers

 avatar

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.