0

Ok Can you guys help me here. This is the function. It gets 3 aurguments, and it returns an array. it converts gregorian date to persian date. It's in Includes/dateConvert.php

    public static function toJalali($g_y, $g_m, $g_d)
{
    $g_days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    $j_days_in_month = array(31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29);
    $gy = $g_y-1600;
    $gm = $g_m-1;
    $gd = $g_d-1;
    $g_day_no = 365*$gy+self::div($gy+3, 4)-self::div($gy+99, 100)+self::div($gy+399, 400);
    for ($i=0; $i < $gm; ++$i)
        $g_day_no += $g_days_in_month[$i];
    if ($gm>1 && (($gy%4==0 && $gy%100!=0) || ($gy%400==0)))
        $g_day_no++;
    $g_day_no += $gd;
    $j_day_no = $g_day_no-79;
    $j_np = self::div($j_day_no, 12053);
    $j_day_no = $j_day_no % 12053;
    $jy = 979+33*$j_np+4*self::div($j_day_no, 1461);
    $j_day_no %= 1461;
    if ($j_day_no >= 366) {
        $jy += self::div($j_day_no-1, 365);
        $j_day_no = ($j_day_no-1)%365;
    }
    for ($i = 0; $i < 11 && $j_day_no >= $j_days_in_month[$i]; ++$i)
        $j_day_no -= $j_days_in_month[$i];
    $jm = $i+1;
    $jd = $j_day_no+1;
    return array($jy, $jm, $jd);
}

and div is a simple dividing function

private static function div($a, $b)
{
    return (int) ($a / $b);
}

now When I write something like below the code doesn't do anything.

<?php
require ("Includes/dateConvert.php");
$yy = 1989;
$mm = 8;
$dd = 31;
echo $yy.$mm.$dd;
echo "<br><br>";
$jj = toJalali($yy, $mm, $dd);
echo $jj[0].$jj[1].$jj[2];
?>

What am I doing wrong? thank you.

6
  • public static function is at least a member of class. Calling $jj = toJalali means that you call some other function. Commented Oct 17, 2015 at 9:29
  • Oh you are right. So I have to exclude the function individually and use it right? Commented Oct 17, 2015 at 9:32
  • Calling static function is like ClassName::MethodName Commented Oct 17, 2015 at 9:32
  • Thank you. Actually I don't need the whole code to work, just the function is enough, so if copy the code and include it in my php file will it work? I guess I have to change the self parts as well as it is referring to the class. Commented Oct 17, 2015 at 9:37
  • Yes it did work, Thank you just post your answer down so I can choose it as solution. Commented Oct 17, 2015 at 9:40

1 Answer 1

2

As I see your function is defined like

public static function toJalali

It means that this function as a method of some class. I don't know name of, let's call it ClassName. As this function is static it can be called without creating class instance like:

ClassName::toJalali()

Removing public static as @EliasNicolas advised will not work as function will still be a method of class.

If you want to use function as standalone function - then you should cut/copy function code out of class and, yes, remove public static. So it will be just

function toJalali()

outside of any class codes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.