|
| 1 | + |
| 2 | + |
| 3 | + # Date and Time Conversion |
| 4 | + |
| 5 | + **Phanisana** provides robust support for converting PHP `DateTime` objects or strings into their full **Malagasy word representations**, suitable for natural language outputs, localization, or educational tools. |
| 6 | + |
| 7 | + --- |
| 8 | + |
| 9 | + ## 📅 Date Conversion |
| 10 | + |
| 11 | + The `DateTimeConverter` class provides multiple formatting levels for converting dates into readable Malagasy text. |
| 12 | + |
| 13 | + ### ✅ Available Date Formats |
| 14 | + |
| 15 | + | Constant | Description | Example Output | |
| 16 | + |----------------------------------|----------------------------------------|---------------------------------------------------------------------------| |
| 17 | + | `FORMAT_DATE_LONG_TEXT` | Full text with day of the week | `alakamisy fito amby folo aprily taona dimy amby roapolo sy roa arivo` | |
| 18 | + | `FORMAT_DATE_MEDIUM_TEXT` | Date with month and year | `fito amby folo aprily taona dimy amby roapolo sy roa arivo` | |
| 19 | + | `FORMAT_DATE_TEXT` | Shorter form without “taona” | `fito amby folo aprily dimy amby roapolo sy roa arivo` | |
| 20 | + |
| 21 | + You can also use custom PHP-style date format strings: |
| 22 | + |
| 23 | + ```php |
| 24 | + echo $converter->convertDate($date, 'd F Y'); |
| 25 | + // Output: 17 Aprily 2025 |
| 26 | + |
| 27 | + echo $converter->convertDate($date, 'l d F Y'); |
| 28 | + // Output: Alakamisy 17 Aprily 2025 |
| 29 | + |
| 30 | + echo $converter->convertDate($date, 'D d F Y'); |
| 31 | + // Output: Alak 17 Aprily 2025 |
| 32 | + ``` |
| 33 | + |
| 34 | + --- |
| 35 | + |
| 36 | + ## 🕰️ Time Conversion |
| 37 | + |
| 38 | + Time strings can be converted to natural Malagasy expressions. Input should be in `HH:mm` or `HH:mm:ss` format (24-hour clock). |
| 39 | + |
| 40 | + ### ✅ Examples |
| 41 | + |
| 42 | + ```php |
| 43 | + echo $converter->convertTime('08:10', DateTimeConverter::FORMAT_TIME_LONG_TEXT); |
| 44 | + // Output: valo ora maraina sy folo minitra |
| 45 | + |
| 46 | + echo $converter->convertTime('00:00', DateTimeConverter::FORMAT_TIME_LONG_TEXT); |
| 47 | + // Output: roa amby folo ora alina |
| 48 | + |
| 49 | + echo $converter->convertTime('12:00', DateTimeConverter::FORMAT_TIME_LONG_TEXT); |
| 50 | + // Output: roa amby folo ora atoandro |
| 51 | + |
| 52 | + echo $converter->convertTime('13:39', DateTimeConverter::FORMAT_TIME_LONG_TEXT); |
| 53 | + // Output: iray ora atoandro sy sivy amby telopolo minitra |
| 54 | + |
| 55 | + echo $converter->convertTime('18:14:56', DateTimeConverter::FORMAT_TIME_LONG_TEXT); |
| 56 | + // Output: enina ora hariva sy efatra amby folo minitra sy enina amby dimampolo segondra |
| 57 | + ``` |
| 58 | + |
| 59 | + > ⚠️ **Note:** Invalid time format (e.g. `08:90`) will throw an `InvalidTimeFormatException`. |
| 60 | + |
| 61 | + --- |
| 62 | + |
| 63 | + ## 🌅 Malagasy Day Parts |
| 64 | + |
| 65 | + Depending on the time of day, Malagasy uses different words to indicate the period: |
| 66 | + |
| 67 | + | Malagasy | English | |
| 68 | + |----------------|------------------| |
| 69 | + | **maraina** | morning | |
| 70 | + | **atoandro** | noon / midday | |
| 71 | + | **tolakandro** | afternoon | |
| 72 | + | **hariva** | evening | |
| 73 | + | **alina** | night | |
| 74 | + |
| 75 | + --- |
| 76 | + |
| 77 | + ## 📆 Full DateTime Conversion |
| 78 | + |
| 79 | + Combine both `convertDate` and `convertTime` to represent a full Malagasy datetime expression. |
| 80 | + |
| 81 | + ```php |
| 82 | + $date = new DateTime('2025-04-17 18:14:56'); |
| 83 | + |
| 84 | + echo $converter->convertDate($date, DateTimeConverter::FORMAT_DATE_LONG_TEXT); |
| 85 | + // Output: alakamisy fito amby folo aprily taona dimy amby roapolo sy roa arivo |
| 86 | + |
| 87 | + echo $converter->convertTime($date->format('H:i:s'), DateTimeConverter::FORMAT_TIME_LONG_TEXT); |
| 88 | + // Output: enina ora hariva sy efatra amby folo minitra sy enina amby dimampolo segondra |
| 89 | +``` |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +## 🎁 Bonus Feature: Extended DateTime Class |
| 94 | + |
| 95 | +Phanisana also includes a convenient `DateTime` class that extends PHP’s native `DateTime` class. This extended class provides all standard datetime manipulation capabilities while integrating full support for **Malagasy localization**. |
| 96 | + |
| 97 | +You can use it anywhere in your application to seamlessly manipulate and output Malagasy-friendly date and time strings. |
| 98 | + |
| 99 | +```php |
| 100 | +use Phanisana\DateTime; |
| 101 | + |
| 102 | +$datetime = new DateTime('2025-04-17 18:14:56'); |
| 103 | + |
| 104 | +echo $datetime->format('l d F Y'); |
| 105 | +// Output: Alakamisy 17 Aprily 2025 |
| 106 | +``` |
| 107 | + |
| 108 | +> 📝 **Note:** All regular PHP `DateTime` format strings are fully supported. |
| 109 | +
|
| 110 | +### 📖 Common PHP DateTime Format Characters |
| 111 | + |
| 112 | +| Format | Description | Example Output | |
| 113 | +|--------|-------------------------|-----------------------| |
| 114 | +| `Y` | Full numeric year | `2025` | |
| 115 | +| `y` | Two-digit year | `25` | |
| 116 | +| `m` | Numeric month (01–12) | `04` | |
| 117 | +| `F` | Full month name | `Aprily` | |
| 118 | +| `d` | Day of the month | `17` | |
| 119 | +| `l` | Full weekday name | `Alakamisy` | |
| 120 | +| `D` | Short weekday name | `Alak` | |
| 121 | +| `H` | Hour (00–23) | `18` | |
| 122 | +| `i` | Minutes (00–59) | `14` | |
| 123 | +| `s` | Seconds (00–59) | `56` | |
| 124 | + |
| 125 | +These can be combined to format output however you need: |
| 126 | +```php |
| 127 | +echo $datetime->format('l d F Y H:i:s'); |
| 128 | +// Output: Alakamisy 17 Aprily 2025 18:14:56 |
| 129 | +``` |
| 130 | + |
0 commit comments