1

I am wondering how to the change date from this

Db -> holiday -> holidayDate (type date) = 2015-01-01, 2015-01-03, 2015-02-19, 2015-03-21, 2015-04-03, 2015-05-01, 2015-05-14, 2015-05-16, 2015-06-02, 2015-07-17, 2015-07-18, 2015-08-17, 2015-09-24, 2015-10-14, 2015-12-25

Here is the code

$sql = "select * from holiday order by holidayDate ";
//echo $sql;
$ambil_data = mysql_query($sql);
if ($data = mysql_fetch_array($ambil_data))
{
     $tglLibur2 = $data['holidayDate'];
}
else
        {
            echo mysql_error();
        }

function selisihHari($tglAwal, $tglAkhir) 

{
    $tglLibur = array("'".$tglLibur2."'"); <= i just want to get this array from db
    $pecah1 = explode("-", $tglAwal);
    $date1 = $pecah1[2];
    $month1 = $pecah1[1];
    $year1 = $pecah1[0];


    $pecah2 = explode("-", $tglAkhir);
    $date2 = $pecah2[2];
    $month2 = $pecah2[1];
    $year2 =  $pecah2[0];

    $jd1 = GregorianToJD($month1, $date1, $year1);
    $jd2 = GregorianToJD($month2, $date2, $year2);

    $selisih = ($jd2 - $jd1);
    $libur1 = 0;
    $libur2 = 0;
    $libur3 = 0;
    for($i=1; $i<=$selisih; $i++)
    {
        $tanggal = mktime(0, 0, 0, $month1, $date1+$i, $year1);
        $tglstr = date("Y-m-d", $tanggal);

        if (in_array($tglstr, $tglLibur))
        {
            $libur1++;
        }
        if ((date("N", $tanggal) == 7))
        {
            $libur2++;
        }
        if ((date("N", $tanggal) == 6))
        {
            $libur3++;
        }

    }
    return $selisih-$libur1-$libur2-$libur3;


}

into this

 $tglLibur = array("2015-01-01","2015-01-03","2015-02-19",
 "2015-03-21","2015-04-03","2015-05-01","2015-05-14","2015-05-16",
 "2015-06-02","2015-07-17","2015-07-18","2015-08-17","2015-09-24",
 "2015-10-14","2015-12-25");

2 Answers 2

1

First of all your function selisihHari doesn't have access to the variable tglLibur2 you're using inside it. So I'm thinking you didn't post your full code here. But what you're looking for can be done with the following code:

$tglLibur = array()
foreach($tglLibur2 as $date){
    $tglLibur[] = $date;
}

But what you're doing in your fetch code doesn't make sense. You keep overwriting the same variable. To change that do the following:

$tglLibur2 = array();
if ($data = mysql_fetch_array($ambil_data))
{
     $tglLibur2[] = $data['holidayDate'];
}

This should give you the array you're looking for. That way you can get rid of your function all together.

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

2 Comments

ehm.. i post full code now.. so sorry i'm not post full code before
@terenzadvent answer remains the same ;)
0

Assuming that you aren't looping through each row and that $data['holidayDate'] is a string of dates, comma delimited, just change:

$tglLibur2 = $data['holidayDate'];

to

$tglLibur[] = explode(', ', $data['holidayDate']);

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.