function that get gregorian date and return jalali date in php

You can use the jdate library to convert a Gregorian date to a Jalali date in PHP. Here's an example function:

main.php
function gregorianToJalali($date) {
    require_once 'jdate.php'; // import the library
    $timestamp = strtotime($date); // convert to a Unix timestamp
    $jdate = new jDateTime(true, true, 'Asia/Tehran'); // initialize the jDateTime object
    return $jdate->date('Y/m/d', $timestamp); // convert the date and return it
}
326 chars
7 lines

You can use this function like this:

main.php
$date = '2021-05-20';
echo gregorianToJalali($date); // will output '1400/02/31'
81 chars
3 lines

Please note that in order to use the jdate library, you need to download it from the author's website and include its source code in your project.

related categories

gistlibby LogSnag