Comments (4)
Hello,
I'm sorry for that but there is no way to support that.
First, there are legit cases where people doing Carbon::createFromDate($gregorianYear)->addYears($number)
want to add $number
years and expect to obtain a Gregorian date. And so it has to be a valid Gregorian date (either Feb 28th without overflow, or March 1st with overflow), but not February 29th because it does not exist in Gregorian year 2567.
Even if we would like to support that Carbon
would not be able to as it's based on native PHP DateTime
object which is always representing a single point in time with the constraint to be a valid Gregorian and valid in the given timezone.
You're recommended to split units not to handle Buddhist Era values using a Carbon
or DateTime
object:
use Carbon\Carbon;
$gregorianDate = '2024-02-29';
$carbonObject = Carbon::createFromDate($gregorianDate);
$buddhistDate = $carbonObject->addYears(543)->year . $carbonObject->format('-m-d');
echo "AD (Anno Domini): $gregorianDate\n";
echo "BE (Buddhist Era): $buddhistDate\n";
Thanks.
from carbon.
Thank you for your answer.
What do you think ?
Add my method "formatBuddhist()" for Buddhist Era year format
nesbot/carbon/src/Carbon/Traits/Converter.php
public function formatBuddhist(string $format): string
{
if (strpos($format, 'o') !== false) {
$format = str_replace('o', '[{1}]', $format);
}
if (strpos($format, 'Y') !== false) {
$format = str_replace('Y', '[{2}]', $format);
}
if (strpos($format, 'y') !== false) {
$format = str_replace('y', '[{3}]', $format);
}
$function = $this->localFormatFunction ?: static::$formatFunction;
if (!$function) {
$format = $this->rawFormat($format);
}
else if (\is_string($function) && method_exists($this, $function)) {
$format = [$this, $function];
$format = $function(...\func_get_args());
}
$timestamp = $this->getTimestamp();
if (strpos($format, '[{1}]') !== false) {
$format = str_replace('[{1}]', (date('o', $timestamp) + 543), $format);
}
if (strpos($format, '[{2}]') !== false) {
$format = str_replace('[{2}]', (date('Y', $timestamp) + 543), $format);
}
if (strpos($format, '[{3}]') !== false) {
$year = (date('y', $timestamp) + 43) % 100;
$format = str_replace('[{3}]', $year, $format);
}
return $format;
}
$parse = Carbon::parse('2024-02-29 10.55.32');
echo $parse->formatBuddhist("Y-m-d H:i:s"); // 2567-02-29 10:55:32
echo $parse->formatBuddhist("d/m/y H:i:s"); // 29/02/67 10:55:32
If you agree, please add both version 2.x , 3.x versions.
from carbon.
Hello,
I'm not sure yet if this should be included into Carbon itself, go into a separate package, or simply be documented as a macro.
I also see few little possible code changes, for instance, it's not useful to step by the timestamp and involve date()
such as in date('o', $timestamp)
you can simply do $this->format('o')
. You can also use the method str_contains
(even if you don't have PHP 8 yet as nesbot/carbon includes the symfony polyfill for it). And maybe all the if
are not even worth it because if you call $format = str_replace('o', '[{1}]', $format);
and that the string does not contain o
it will just do nothing.
Be careful of $year = (date('y', $timestamp) + 43) % 100;
, when it's < 10, you won't have a trailing zero. You'd get 29/02/7
while this format is supposed to give: 29/02/07
, str_pad can be used to add the trailing 0
.
Unfortunately, 2.x is now only receiving bugfixes. New features only comes into 3.x. On the other side, using macro, it would work on any object v2 or v3:
Carbon::macro('formatBuddhist', static function (string $format): string {
$self = self::this();
$format = strtr($format, [
'o' => '[{1}]',
'Y' => '[{2}]',
'y' => '[{3}]',
]);
$function = $self->localFormatFunction ?: static::$formatFunction;
if (!$function) {
$format = $self->rawFormat($format);
} else if (\is_string($function) && method_exists($self, $function)) {
$format = [$self, $function];
$format = $function(...\func_get_args());
}
$buddhistYear = $self->year + 543;
return strtr($format, [
'[{1}]' => $self->format('o') + 543,
'[{2}]' => $buddhistYear,
'[{3}]' => str_pad($buddhistYear % 100, 2, '0', STR_PAD_LEFT),
]);
});
from carbon.
Hello
Thank you so much for everything, suggestion and for your "Carbon" i love it.
The Buddhist calendar is a set of lunisolar calendars primarily used in Tibet, Cambodia, Laos, Myanmar, India, Sri Lanka, Thailand and Vietnam as well as in Malaysia and Singapore and by Chinese populations for religious or official occasions.
https://en.wikipedia.org/wiki/Buddhist_calendar
Actually, I think adding to carbon is also possible. Just a few lines more, or we can separate it into a separate package.
Have a good day, Thanks
from carbon.
Related Issues (20)
- Unserialize from Carbon v2 objects HOT 1
- Carbon createFromTimestampMs no default time zone if it is set in env variable of Laravel app HOT 1
- `CarbonInterval`'s `days` property return false HOT 2
- [Translation tool] sr_RS@latin
- Date difference problem HOT 5
- isSameDay: Too few arguments HOT 1
- Singular methods without parameters also work with parameters. It should not be as per the documentation. HOT 1
- Inconsistent Date Calculation When Adding Months Across February HOT 5
- Carbon\Carbon::rawAddUnit(): Argument #3 ($value) must be of type int|float, string given HOT 5
- Failed to parse time dd/mm/yyyy using parseFromLocale with Portuguese HOT 1
- Check month issue HOT 1
- PHP Version < 7.4 broken HOT 2
- Psalm reports MissingDependency on CarbonPeriod HOT 1
- Syntax Supported Only After PHP 7.4 Causes Errors in Versions Prior to 7.4 HOT 1
- Undocumented breaking change: isSameMonth() HOT 3
- CarbonPeriod loses timezone when accessing days
- Using "Depends on the following methods" @method in traits causes PHPStan to lose knowledge of type resulting in errors
- 2.7.4 is not support php7.3 HOT 1
- PHPSandbox is not working properly HOT 2
- `age` function does not calculate the age correctly for Carbon >= 3 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from carbon.