2

PHP intl can convert a Gregorian date to other calendar type.

For example Gregorian to Hijri, 2017/04/11 to 1396/01/22

Or we must use external library to convert dates?

msoa
  • 1,339
  • 3
  • 14
  • 33

3 Answers3

6

You Can Use This Method:

function getDateTimeFromCalendarToFormat($format = 'yyyy-MM-dd HH:mm:ss',     $time = null, $locale = 'fa_IR', $calendar = 'persian', $timeZone = 'Asia/Tehran')
{
    if (empty($time)) {
        $time = new \DateTime();
    }

    $formatter = new \IntlDateFormatter("{$locale}@calendar={$calendar}", \IntlDateFormatter::FULL, \IntlDateFormatter::FULL, $timeZone, \IntlDateFormatter::TRADITIONAL);
    $dateArray = [];

    $formatter->setPattern($format);

    return $formatter->format($time);
}

if you call getDateTimeFromCalendarToFormat('yyyy-MM-dd HH:mm:ss',null,'en_US')
Return
1396-06-27 13:50:21

if you call getDateTimeFromCalendarToFormat('yyyy-MM-dd HH:mm:ss',null,'fa_IR')
Return
۱۳۹۶-۰۶-۲۷ ۱۳:۴۹:۵۱

New pattern string to use. Possible patterns are documented at Formatting Dates and Times

Honarkhah
  • 553
  • 7
  • 19
  • why? @mohammad-salehi – Honarkhah May 09 '18 at 10:04
  • sorry @honarkhah , it was my mistake, or maybe it's a PHP bug. I used 'Y-m-d H:i:s' for $format and it returned a wrong month number every time. but it works fine with 'Y-M-d H:i:s' (capital M). what is the difference between yyyy and Y, or i and mm and other multi letters with single letters? – Mohammad Salehi May 09 '18 at 16:37
  • 1
    @MohammadSalehi You can check http://userguide.icu-project.org/formatparse/datetime , this is a fully example of pattern you can use. – Honarkhah May 12 '18 at 16:10
0

Yes it can. This is an example which converts a time object to its Persian formatted string using intl:

function c2persian($time, $toCalender = 'persian', $timezone = 'Asia/Tehran', $locale = 'fa_IR') {
    $formatter = IntlDateFormatter::create($locale, NULL, NULL, $timezone, IntlCalendar::createInstance($timezone, "$locale@calendar=$toCalender"));
    return $formatter->format($time);
}
$time = strtotime("2089-08-09 00:00:00 UTC");
echo c2persian($time);

You can find more info at php intlCalendar documentation.

Mojtaba Rezaeian
  • 8,268
  • 8
  • 31
  • 54
-3

PHP Already has date format functions, you need to convert first with strtotime() function and get the desired values with date()

<?php
$originalDate = "2017/04/11";

$theDate = strtotime($originalDate);

$theDay = date('d',$theDate);
$theMonth = date('m',$theDate);
$theYear = date('Y',$theDate);

$customFormat = date('Y-m-d',$theDate);
$customFormat2 = date('d/m/Y',$theDate);
$customFormat3 = date('F j, Y, g:i a',$theDate);
?>

Example working here: https://eval.in/772416

You can get more info about date in php here: http://php.net/manual/en/function.date.php

TriForce
  • 415
  • 2
  • 8
  • 1
    Folks are down voting but not giving any context. The OP was asking about how to convert to a different calendar entirely, not just how to convert the format of the date/time within it's original calendar. Your answer is absolutely correct, just not for this question. – joshtronic May 25 '19 at 05:11