How to set default date to Persian date in PHP?
Asked Answered
E

2

7

How to set default date to Persian date in PHP?

Say, if I echo this function date('Y-m-d') then it will show 2018-03-05 but I want 1396-12-14 the Persian date

Enchant answered 5/3, 2018 at 12:46 Comment(3)
If it can be done by timezone then please suggest.Enchant
Try this php class : phpclasses.org/package/…Sequestration
This might help you? codeproject.com/articles/28380/persian-calendar-in-phpDodwell
S
18

Please check: http://php.net/manual/en/intldateformatter.create.php

<?php

// date_default_timezone_set('Asia/Tehran');

$now = new DateTime();

$formatter = new IntlDateFormatter(
                "fa_IR@calendar=persian", 
                IntlDateFormatter::FULL, 
                    IntlDateFormatter::FULL, 
                'Asia/Tehran', 
                IntlDateFormatter::TRADITIONAL, 
                "yyyy-MM-dd");

// It is now: 1396-12-14
echo 'It is now: ' . convert_to_number($formatter->format($now)) ."<br />"; 

// It is now: ۱۳۹۶-۱۲-۱۴
echo 'It is now: ' . $formatter->format($now) ."<br />"; 


// convert to number
function convert_to_number($string) {
    $persian = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
    $arabic = ['٩', '٨', '٧', '٦', '٥', '٤', '٣', '٢', '١','٠'];

    $num = range(0, 9);
    $convertedPersianNums = str_replace($persian, $num, $string);
    $englishNumbersOnly = str_replace($arabic, $num, $convertedPersianNums);

    return $englishNumbersOnly;
}
Shoulders answered 5/3, 2018 at 13:39 Comment(5)
Thank's to all, but it will not help. What I want is, if I echo date('Y-m-d') then it should return 1396-12-15 instead of 2018-03-06.Enchant
Isn't there any better way to convert numbers to English ones ?Rabbitry
Use "en_US@calendar=persian", to get English numbering.Rabbitry
If use en_US instead of fa_IR then you don't need to use convert_to_number function.Adalai
this function just convert date and it doesnt return time!,i want both date and time "Y-m-d h:i"Gallagher
R
1

Finglish (Persian) date format in php :

<?php

// date_default_timezone_set('Asia/Tehran');

$now = new DateTime();

$formatter = new IntlDateFormatter(
                "en_US@calendar=persian", 
                IntlDateFormatter::FULL, 
                    IntlDateFormatter::FULL, 
                'Asia/Tehran', 
                IntlDateFormatter::TRADITIONAL, 
                "yyyy-MMMM-dd");

// It is now: 1401-Bahman-20
echo 'It is now: ' . $formatter->format($now) ."<br />"; 
?>

And If you want to change the date format, see the following link :

https://unicode-org.github.io/icu/userguide/format_parse/datetime/#date-field-symbol-table

Roehm answered 9/2, 2023 at 9:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.