function that recive gregorian date and return jalali date in php

There are a couple of ways to achieve this, but one common method is to use the jDateTime library.

You can install it using Composer:

main.php
composer require sallar/jdatetime
34 chars
2 lines

Then, you can use it like this:

main.php
use \Sallar\JDateTime\JDateTime;

function gregorian_to_jalali($date) {
    $jd = new JDateTime();
    $jd->setGregorianDate(date('Y', $date), date('m', $date), date('d', $date));
    return $jd->date("Y/m/d");
}

// Usage example
echo gregorian_to_jalali(time()); // will output something like "1400/10/26"
308 chars
11 lines

This converts a given timestamp (in Gregorian calendar) to a string representing the same date in Jalali calendar.

Note that jDateTime library includes many other features, such as formatting, localization, timezone handling, etc. You can check its documentation for more information.

related categories

gistlibby LogSnag