php - How to get specific result from my IntlDateFormatter() function.?
Get the solution ↓↓↓i have this function to convert gregorian date to Jalali(Persian) date :
function toJalali ($date)
{
$fmt = new IntlDateFormatter("fa_IR@calendar=persian", IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Asia/Tehran', IntlDateFormatter::TRADITIONAL);
return $fmt->format($date);
}
when i call ittoJalali(time())
it's give me full date(1397-8-17 Щ‡.Шґ ...)
how do i get my specific date format from above function.?
i know that format options can be set in constructor :
$fmt = new IntlDateFormatter("fa_IR@calendar=persian", IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Asia/Tehran', IntlDateFormatter::TRADITIONAL, 'MM/dd/yyyy');
but what about when i want other time formats.?
like onlyyear : (yyyy)
or(EEEE/yyyy/MM)
or etc. so i must write individual function for each date format.?
Thanks.
Answer
Solution:
To change the pattern you can use setPattern method like this:
$fmt = new IntlDateFormatter("fa_IR@calendar=persian", IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Asia/Tehran', IntlDateFormatter::TRADITIONAL, 'MM/dd/yyyy');
$fmt->setPattern('EEEE/yyyy/MM');
echo $fmt->format(0);
And your function would be look like this:
function toJalali ($date, $pattern = null)
{
$fmt = new IntlDateFormatter("fa_IR@calendar=persian", IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Asia/Tehran', IntlDateFormatter::TRADITIONAL);
if ($pattern){
$fmt->setPattern($pattern);
}
return $fmt->format($date);
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: dompdf image not found or type unknown
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.